Last active
October 22, 2020 15:31
-
-
Save robbiemu/86d1d2a2e45612aebaabb717dc4a6824 to your computer and use it in GitHub Desktop.
ES2022 wishes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = 1 | |
function f(x) { | |
return x + a | |
} | |
@pure function g(x) { | |
return x + a | |
} | |
c = f(b) | |
console.log(c) // 3 | |
d = g(b) // error "a is not defined" | |
// console.log(d) | |
b = 2 | |
function h(x) { | |
++x | |
} | |
@pure function i(x) { | |
return ++x | |
} | |
i(b) // 3 | |
console.log(b) // 2 | |
h(b) // 3 | |
console.log(b) // 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = ['a', 'b', 'c', 'd'] | |
console.log(a[-1]) // 'd' | |
b = a[1:2] // ['b', 'c'] -- this could be just ['b'], like python. Not sure that is very Javascript-like though | |
c = [ | |
[ 1,2,3 ], | |
[ 4,5,6 ], | |
[ 7,8,9 ] | |
] | |
c[0,1] // 2 | |
c[0:2,-1] // [3,6,9] -- this could be just [3,6], like numpy. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// these are all syntactic sugar when allowing side effects, but are not when requiring pure/immutability | |
a = [ 1,2,3 ] | |
doubles = [ v + v for(v of a) ] // 2,4,6 | |
// a.map(v => v + v) | |
a = [ 'a', 'b', 'c' ] | |
paired = { v : i for((v,i) of a) } // { a: 0, b: 1, c: 2 } | |
// a.reduce((prev, curr, index) => ({...prev, {[curr]:index} }), {}) // technically still a side-effect, consider if the final paramter were a variable | |
/* without side-effect I guess is still possible, but not readable | |
(function (x) { | |
return x.reduce((prev, curr, index) => ({...prev, {[curr]:index} }), {}) | |
})(a) | |
*/ | |
composable = { prev + curr reduce((prev, curr) of a, 0) } // 6 | |
// a.reduce((prev, curr) => prev + curr, 0) // side-effect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use side-effects | |
function f(x) { | |
return ++x | |
} | |
a = 1 | |
b = f(a) | |
console.log(a, b) // 2, 2 with use side-effects pragma. 1, 2 without | |
a = 2 // no error: a is let by default with use side-effects pragma, but const without | |
function g(x) { | |
return x + a | |
} | |
g(a) // 4 with use side-effects pragma. a is not defined error without (pure function by default) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment