2 ms·
It's just data modelling, except instead of trying to fit our data into some arrangement of pointers (in a C-like language), or some combination of numbers/stri
by chriswarbo 6y ago
It's just data modelling, except instead of trying to fit our data into some arrangement of pointers (in a C-like language), or some combination of numbers/strings/lists/dicts (in a scripting language), or some algebraic datastructure (in an ML/Haskell-like language), instead we're trying to fit our data into some arrangement of anonymous functions.
I picked JS syntax since it's familiar to many people. If you're more comfortable with some other language, maybe try translating those examples into that language (the language must support first-class/anonymous functions; trying this with e.g. function pointers in C would be even more horrible!)
Then just play around with these functions a little; get a feel for how they behave; e.g. you could call them with strings like "foo", "bar", etc. and see what they return. Maybe write some input/output tables to see what's happening.
Then imagine that your language only has functions; nothing else. Try to find an alternative to if/else which only uses functions (hint: the 'true' and 'false' definitions above will help!). You've already seen how to write finite loops (that's how numbers are represented); try to write an infinite loop (this is easier if you're giving names to functions, like I did with 'add', 'times', etc. above). Then try writing an anonymous infinite loop, only using anonymous functions (hint: look at the URL of this site).
This is largely an academic exercise, but it can bring a deeper appreciation/understanding of what we can do with functions. Smalltalk famously uses a similar approach to this for branching (myBool ifTrue: x else: y) and looping (myNum timesRepeat: x); Smalltalk had a huge influence on OOP and scripting languages.