4 ms·
APL-family languages have for a long time encouraged similar practices, eschewing explicit loops and recursion in favor of baking iteration into operator applic
by RodgerTheGreat 12y ago
APL-family languages have for a long time encouraged similar practices, eschewing explicit loops and recursion in favor of baking iteration into operator application and a series of operators which express specific iteration patterns.
Recently I've been having a great deal of fun learning K, a modern APL variant which depends only upon standard ASCII characters for its syntax. In K, the final example:
adobe::for_each( v_T, f_T );
Would be simply:
f'v
Read, "f each of v", sssuming v is defined as a vector and f is defined as a function. There are other operators for different iteration patterns, such as "Over":
+/3 7 18
Which is equivalent to distributing the left argument of / (an operator like + or potentially a whole function) between the elements of the right vector, just as if we'd said:
3+7+18
Sometimes it's tricky to hammer an existing algorithm into the selection of patterns which is available, but it's not nearly as limiting as you might imagine, and recasting your code to use K's set of operators often simplifies problems considerably. It would certainly be difficult to be more concise.