Picolab / pico-engine

An implementation of the pico-engine hosted on node.js
http://picolabs.io/
MIT License
43 stars 8 forks source link

System-provided operators could be used as functions #615

Open b1conrad opened 2 years ago

b1conrad commented 2 years ago

The operator usage is so convenient for chaining that we allowed user-defined functions to be used as operators. So, for example, these two usages are equivalent:

my_func(a,b,c)
a.my_func(b,c)

Unfortunately, the converse isn't true. These two usages (of a system-defined operator) are NOT equivalent:

append(a,b) // doesn't work as might be expected
a.append(b)

If system-provided operators could be used as functions, then we could write the flatten operation in a simpler way (as this author expected (see this commit)):

list_of_arrays.reduce(append) // note that this does NOT work now

One could extend this to sets:

list_of_arrays.reduce(union,[]) // note that this does NOT work now, but the next line does work though less simple
list_of_arrays.reduce(function(a,b){a.union(b)},[])

(Note that append works without an initial empty array, but since the first array in list_of_arrays might not be a set (might have duplicates) we need to seed the operation with an empty array.)