-
-
Notifications
You must be signed in to change notification settings - Fork 4
closure
block, code and closure are very similar concepts:
- every data with the intention of being evaluated is a block
- every block with concrete binding is code
- every code with a local-only binding is a closure
lambdas are closures are blocks with parameters/arguments:
(x,y)=>{x+y}
|x,y| {x+y}
closures are anonymous functions and functions are just names closures. Todo: is a closure bound to a variable identical to a function?
The javascript closures notation x => x*x is beautiful but a bit hard to type
square = x :: x*x
better alternative?
Are the concepts of map and closure unifyable? If so one might simply write
square = x : x*x
Is it possible to write closure as x : x*x
? so
{x:x*x}(3) == 9
?
map [1 2 3] {x:x*x} == [1 4 9]
or even
map [1 2 3] x:x*x == [1 4 9]
?
map [1 2 3] {it*it}
map [1 2 3] square
# fine but no closure
todo
potential alternative syntax for closures:
map [1 2 3] x:x*x
map [1 2 3] x::x*x
map [1 2 3] x=>x*x
map [1 2 3] x->x*x
map [1 2 3] \x x*x # lambda nice!
map [1 2 3] |x| x*x # dangerous pipe
map [1 2 3] [x] x*x # significant white space
map [1 2 3] «x» x*x # hard to type, but ok
map [1 2 3] x >> x*x # c++ gonna hate this, as are bit shifts
map [1 2 3] x :> x*x # functor fun ;)
map [1 2 3] (x){x*x} # conflicts with if, while; (x)=x would require:
map [1 2 3] x{x*x} # conflicts with setter?
map [1 2 3] {it*it} # works out of the box, but what about x,y? {it*it2} ??
map [1 2 3] it*it # no! `it` evaluated in place!
map [1 2 3] {|x| x*x} # meh ruby
map [1 2 3] {x in x*x} # swift but in is reserved
map [1 2 3] x into x*x # nice! speakable but as argument (put x into y := ...)
To understand the difference between
map [1 2 3] {it*it}
and map [1 2 3] it*it # no!
see uncharged block evaluation
The :: syntax is nice because it matches the 'block' semantic and is easy to type Special superfluous symbols 𐬺 𐬻 𐬼 𐬽 𝄈 ⣤⠶⠛⠭⣉⣒
What about two parameter?
\x \y x+y
(x y)->x+y
«x y» x+y
|x y| x+y # unstable white space with pipe
:x :y x+y # radical! unstable white space
(x y):{x+y} # potential headache
(x,y){x+y} no! conflicts with if, while ...
{$0 + $1} no! conflicts with outer locals
{#1 + #2} special locals?
note how the first forms are shorter and cleaner than the later ones.
Conclusion:
(x y)->x+y works with all kinds of lists (x,y) [x y] {x;y} and is robust!
The operator -> and => shall be the default way to denote lambdas
\x \y backslash and pipe may be additional syntactic sugar.
into
and ::
:>
could easily be added as synonym for -> but might be saved for other user cases. solvable? ::
:>
too many different variants are discouraged! arrows and into are ok though!
«x y» x+y remains hard to type, otherwise nice addition. planned for template strings though?
let sum = [1, 4, 2, 3].iter().cloned()
.filter(|x| x % 2 == 0)
.fold(0, |sum, i| sum + i);
has it's own charm because it is easier to type than => and |x| is reminiscent of (x) as parameter.
Difference between assigned lambdas and functions?
f=(x)=>x*x
lambdas are only visible in the current context
f(x)=x*x
functions are visible in the current class, file or everywhere
But for inner functions ( function declarations within other functions) these concepts could be unified (?)
https://github.com/WebAssembly/gc/blob/main/proposals/gc/Overview.md
(type $code-f64-f64 (func (param $env (ref $clos-f64-f64)) (param $y f64) (result f64)))
…