Anonymous Functions as Partially Applied Values
This change will let me get rid of the function keyword in favor of ① anonymous functions ② that have a wonderfully clean syntax. It's a little different than most common languages, but that's alright.
- in this language, all values are one of three containers: Dictionary, Array, or SingleValue.
- Conveniently, these map to the common symmetric delimiters:
{ },[ ], and( ). - Functions are no longer functions in this language. Instead, they're partially applied values.
- The arrow means: apply the left side as additional scope to the right side.
{ width: 5, height: 3 } -> { area: width * height }
# => { area: 15 }
- In some cases this syntax doesn't make total sense, so an underscore represents the arrow's left-side expression
2 -> [ 1, _, 3 ]
# => [ 1, 2, 3 ]
- Because these partially applied values are themselves values, they can be assigned to variable names.
add_two = -> ( _ + 2 )
rectangle = -> { area: width * length; perimeter: width * 2 + length * 2 }
7 -> add_two -> { width: _, height: 3 } -> rectangle
# => { area: 27, perimeter: 24 }
The syntax ends up being quite beautifully symmetric between the three container types:
5 -> { result: _ * 2 } # returns {result: 10}
5 -> [ _ * 2 ] # returns [10]
5 -> ( _ * 2 ) # returns 10
If there's a better name for "partially applied values", I'd love to know it. I went with that name because this pattern reminds me of partially applied functions:
function greet({name: name, ending: ending}) {
return "hello " + name + ending
}
# partially applied greet:
greet_excited = greet(ending: "!")
greet_alice = greet(name: "alice")
# these partially applied functions still need to be passed the required arguments before they can really be invoked:
greet_excited("bob")
# => "hello bob!"
greet_alice("?")
# => "hello alice?"