Maybe you've heard of APL before? It's a now-somewhat-esoteric language from the mid 1960's that makes heavy use of unusual symbols and vector math. It's always been a sort of unapproachable curiosity for me. I have no problem learning weird new syntax, but a language that requires a special keyboard is a bit much! A few weeks ago I discovered that the spirit of APL lives on in languages such as J and K, both of which have shed the need for a non-standard input device.
I decided to give K a try. Fortunately there's a free implmenentation. After playing around for a while I ended up reading through the help output to see what the
& verb does in its monadic form. What caught my attention though was something I hadn't been looking for: the description for the dyadic form.
& dyadic min/and. MIN(x,y) or AND(x,y)
and. Now that's something I've never seen before. In retrospect I'm not entirely sure how. I've been programming for a good while now and until now I've never seen
min overloaded with
and. After thinking about it for a minute I realized that it makes a good deal of sense. If you're working in a language where numeric 0 is a falsey value and you only really have numeric values (i.e. definitely not Ruby) then finding the minimum of two values x and y is equivalent to the boolean and of x and y. If either value is 0 then the result is 0 (falsey) but if both values are not 0 then the result is the non-zero minimum (truthy).
I was not surprised to find that K also overloads the dyadic
| verb to mean both
| dyadic max/or. MAX(x,y) or OR(x,y)
In a way similar to
min, finding the
max of two values when both are 0 returns 0 (falsey) but when either value is non-zero the result is always truthy regardless of what the maximum actually is.
Like just about everything I post here, this is totally obvious in hindsight. I'm only writting about it because despite having dealt with boolean logic for half my life now I've never seen this property of
max pointed out explicitly before. It makes sense that a language like K, which has such a compact grammar, would overload a those functions in such a way.