3 ms·
> If a word in a context is bound to a function it evaluates it, if not it returns the value bound to it. One of my favorite compositional patterns! I wish we
by jauntywundrkind 2y ago
> If a word in a context is bound to a function it evaluates it, if not it returns the value bound to it.
One of my favorite compositional patterns! I wish we saw this .ore, not just in contexts but everywhere.
It's something I saw Ansible inventories do that I absolutely loved. Point at an inventory, and Ansible will run it & use the output as inventory, or will otherwise use the contents.
I wish more languages had stuff like this. I just skeeted today that JS should have an optional function invocation syntax. myVar.??(). Run myVar if it's a function else use its value.
- danpalmer 2y agoThe next step from this is in languages like Haskell, where function currying is the same as function calling, and everything is lazy. There's effectively no difference between a function with all the arguments filled out, and the value it returns – at least most of the time you don't need to care about the difference. Partial function application is incredibly ergonomic in the language too.
- noelwelsh 2y agoHow do you then distinguish between referring to a function with no / empty arguments, and invoking that function? Functions are no arguments (aka thunks) are useful in a number of contexts (pun intended!) and it's kinda the point that they are not immediately invoked.
- layer8 2y agoYeah, this kind of automatism turns nullary functions into brittle things where you never know when or how often they get evaluated. Even if you have no side-effects, a potentially expensive computation is in itself a side-effect that you want to control when it happens. A nullary function is a different type from its return type, and should better be handled as such.
- deleted 2y ago[deleted]
- middayc 2y agoRye like Rebol uses get-words for getting the value of the word, for example a function versus evaluating it.