What is idempotent again ?
Closure, Memoization, Idempotence : Decoding and understanding programming terms one by one in simplest definition
All code written in this javascript, but dont worry about the language, syntax is kept super simple. For practise, you can implement them in your favourite programming language.
Lets start with first class functions
First Class Function
A programming language is said to have first class functions if it treat its functions as first class citizens
What are first class citizens : something that can be
- passed as an argument
- returned from a function
- assigned to a variable
Whatever satisfies the above 3 properties in you programming language can be called as first class citizen. Lets take a look at with examples
Assigning Function to a variable
function square(x) {
return x * x
}
// Assigned to another variable
let f = square
console.log(square(5))
console.log(f(5))
Passed as an Argument aka High order functions
// sqr the the passed square function
function my_map(sqr, args) {
let result = []
for (let i = 0; i < args.length; i++) {
// the passed function is used here
result.push(sqr(args[i]))
}
return result;
}
// square function is passed as argument
let squares = my_map(square, [1, 2, 3, 4, 5, 6, 7])
console.log(squares)
Function as Return Type
function logger(msg) {
function log_message() {
console.log("Log : " + msg)
}
// this is a function, returning from parent functions
return log_message
}
logHello = logger("hello")
logHello()
Before moving on, please read the above and try to understand the concept, it would be helpfull
Closure
They are similar to functions returned from another function but captures the internal state of parent function at the time of invoked.
- A closure is a record storing a function together with an environment, a mapping associating each free variable of the function with the value of storage location to which the name was bound when then the closure was created. ( Kinda formal , read below and look at code snippet )
- A closure, unlike a plain function, allows the function to access those captured and closed variables when the function is invoked outside the scope.
function outer_function() {
message = "hello world"
function inner_function() {
console.log (message) // Look at point 2 from definition
}
return inner_function()
}
// invoked from outside
outer_function()
Another Example of Closure
function outer_function(msg) {
message = msg
function inner_function() {
console.log (message)
}
return inner_function
}
let func = outer_function("Hello World")
func()
Immutable and Mutable
// strings in js are immutable
// they cannot be changed once initialised
let name = "uday Yadav"
name[0] = "U";
// this makes not difference
console.log(name);
// still small case 'u'
console.log(name[0]);
// array in js is mutable
// they can be changed once created
let data = [0,2,3,4];
data[0] = 1;
console.log(data);
Memoization
Some operations are expensive to preform, so we store the results of them in some form of temporary storage and when required to recalculate, first find them in the temporary storage.
let cache = {}
function expensive_compute(data) {
if (cache.hasOwnProperty(data)) {
console.log("answer cache : "+cache[data])
cache[data] = data*data
return;
}
cache[data] = data*data
console.log("answer : "+cache[data])
}
expensive_compute(4)
expensive_compute(10)
expensive_compute(4)
expensive_compute(16)
expensive_compute(10)
Idempotence
The property of certain operations in mathematics and computer science, that can be applied multiple times without changing the result without initial application
A good example of understanding an idempotent operation might be locking a car with remote key.
log(Car.state) // unlocked
Remote.lock();
log(Car.state) // locked
Remote.lock();
Remote.lock();
Remote.lock();
log(Car.state) // locked)
lock
is an idempotent operation. Even if there are some side effect each time you run lock, like blinking, the car is still in the same locked state, no matter how many times you run lock operation.NON-IDEMPOTENT: If an operation always causes a change in state, like POSTing the same message to a user over and over, resulting in a new message sent and stored in the database every time, we say that the operation is NON-IDEMPOTENT.
NULLIPOTENT: If an operation has no side effects, like purely displaying information on a web page without any change in a database (in other words you are only reading the database), we say the operation is NULLIPOTENT. All GETs should be nullipotent.
To understand idempotence more, refer this stackoverflow thread : what is idempotent operation
Ephemeral
synonyms to temporary
Anonymous Functions
Function without a name, also known as lambda function in
Python
let arr = [1, 2, 3];
let mapped = arr.map(x => Math.pow(x, 2));
// x => is a function without a name
console.log(mapped);
Predicate
Functions that return true or false depending on the input. They usually start with is
class Animal {
constructor(_type) {
this.type = _type;
}
}
function makeSound(animal) {
if (isCat(animal)) {
console.log(" MEOW ! ");
return;
}
console.log(" NOT CAT ! ");
}
function isCat(animal) {
return animal.type === 'Cat';
}
let newCat = new Animal('Cat');
makeSound(newCat);
Parsing and Stringify
- Parsing : converting string to some object
- Stringify : converting some object to string
let data = {
"name": "Uday Yadav",
"Gender": "Male"
}
let str = JSON.stringify(data)
console.log(str + "|" + typeof str)
let dataReturns = JSON.parse(str)
console.log(dataReturns + "|" + typeof dataReturns)
More about me : https://uday-yadav.web.app/
Top comments (6)
Can't agree with idempotent examples. Given number 10 you can call add_one(10) multiple times and each time add_one(10) will be 11. Changing the number to -10 is changing the input so it's valid to get different result. The same goes for abs(abs(10)). Abs is giving a result effectively changing the input. It's not about idempotence.
You could have a functio that uses a global counter (variable) and for each call it increases so calling it multiple times with same inputs it gives different result. That's non-idempotent. Then you can have a function accepting the start counter as a parameter and increasing it by 1. That's idempotent as calling it multiple times with same inputs will result in same values.
Take for example POST and PUT examples. Calling POST multiple times will create multiple different records (different ID for instance) for the exact same body. Calling PUT with ID in the body multiple times will always return same result for that same body
thank you pointing out the mistakes, I do agree that func add_one is not a suitable example for illustration and will correct it soon.
but for the abs() function, i took this example from textbooks. Its is the de-facto example given to understand idempotency. Searching more the internet, i came across this : stackoverflow.com/questions/107741...
After reading both the stack thread and your comment, i do think there are better other better example to explain it
thank you again.
Thanks for the link. It has a nice explanation on a slight difference in mathematics and programming. I didn't think of mathematical approach just assumed most people would head on to network approach where you can do multiple inputs to a program from external side. However it's good to always keep in both things. Really glad you replyed with this.
Quite a comprehensive list. Thank you !
The first example can be improved like this:
Instead of: let f = square(5)
Better: let f= square
thank you for the feedback, it makes sense to illustrate it this way, will update the article