Today I Learned - Rocky Kev

TIL Stateful and stateless

POSTED ON:

TAGS:

Programming languages have the notion of “state”. This is a snapshot of a program’s current environment: all the variables that have been declared, functions created and what is currently being executed.

An expression in a programming language can be “stateful” or “stateless”. A stateful expression is one that changes a program’s current environment. A very simple example in Javascript would be incrementing a variable by 1:

var number = 1;
var increment = function() {
return number += 1;
};
increment();

The function increment() modifies the variable number directly.
That is a hidden intent, and should be avoided.

This stateless expression, on the other hand, is one that does not modify a program’s environment:

var number = 1;
var increment = function(n) {
return n + 1;
};
increment(number);

The function increment() has no values inside of it. It takes a parameter. It's 'pure'.

Via: https://stephen-young.me.uk/2013/01/20/functional-programming-with-javascript.html


Related TILs

Tagged:

TIL math module in SASS!

math in Sass, oh my!

TIL the Quake 3 Fast Inverse Square Root hack

Quake III Arena, a first-person shooter video game, was released in 1999 by id Software and used the algorithm.

TIL that the max size of a Map/Set is 26843544

JS Maps and Sets are implemented by OrderedHashTable. OrderedHashTables double in size when they need to grow, and their max size is 26843544. After 16777216 elements, doubling that exceeds the max, so the allocation fails.Currently these data structures are not implemented to be usable for large in-memory datasets. Also note that the JS specification itself doesn't require that Maps and Sets must scale with available memory.