Follow-up to “A Programming Idiom You’ve Never Heard Of”
Programming in the 21st Century - James Hague - January 05, 2012Lots of mail, lots of online discussion about A Programming Idiom You’ve Never Heard Of, so I wanted to clarify a few things.
What I was trying to do was get across the unexpected strangeness of function inverses in a programming language. In that short definition of vector magnitude, there wasn’t a visible square root function. There was only an operator for squaring a value, and another operator that involved inverting a function.
How does the J interpreter manage to determine a function inverse at runtime? For many primitives, there’s an associated inverse. The inverse of add is subtract. The inverse of increment is decrement. For some primitives there isn’t a true, mathematical inverse, but a counterpart that’s often useful. That’s why the preferred term in J isn’t inverse, but obverse.
For user-defined functions, there’s an attempt at inversion (er, obversion) that works much of the time. A function that reverses a list then adds five to each element turns into a function that subtracts five from each element then reverses the list. For cases where the automated obverse doesn’t work, or where you want the obverse to have different behavior, you can associate a user-defined obverse with any verb (J lingo for function). You could define an open_file verb which opens a file and has an obverse that closes a file. Or in actual J:
open_file =: open :. close
Well, really, that should be:
open_file =: (1!:21) :. (1!:22)
But the former, without the explicit foreign function calls, gets the point across clearer, I think.
One common use of obverses and the “under” operator is for boxing and unboxing values. In J, a list contains values of the same type. There’s no mixing of integers and strings like Lisp or Python. Instead you can “box” a value, then have a list containing only boxed values. But there’s nothing you can do with a boxed value except unbox it, so it’s common to say “[some operation] under open box,” like “increment under open box.” That means unbox the value, increment it, then put it back in a box. Or in real, eyeball-melting J:
inc_box =: >: &. >
The >: is increment. The right > means open box. That’s the “under” operation in the middle.
Now it sounds like this “open box, do something, close box” sequence would translate beautifully to the “open file, read the contents, close the file” example I gave last time, but it doesn’t. The catch is that the open / read / close verbs aren’t manipulating a single input the way inc_box is. Opening a file returns a handle, which gets passed to read. But reading a file returns the contents of the file, which is not something that can be operated on by close. So this definition won’t work:
read_file =: read &. open
If a structured data type like a dictionary was being passed around, then okay, but that’s not a pretty example like I hoped it would be.
Still, I encourage learning J, if only to make every other language seem easy.
Categories: Blogs Programming in the 21st Century
Erlang on Twitter
» awankdark (Septiawan Kurniadi): Ak ngomongi pengalaman kwan ak budak lampung don hha “@doni_erlang: Weh dak boleh ngngmngin diri dewe wang haha “”
» patrickDurusau (Patrick Durusau): RP Learn You Some Erlang for Great Good! #topicmaps #fp #erlang - http://t.co/4DqThNvPQQ
» Muh_Erlang (M. Erlangga Pangestu): Kalo sholat subuh d masjid sekarang sdh telat 10 menit ._.
» Muh_Erlang (M. Erlangga Pangestu): RT @detikcom: Bayern Kini Setara dengan Liverpool http://t.co/Zrurv6RaVU via @detiksport
» JonesDonovan1 (JonesDonovan): Pretense is erlang the main body outsourced yourselves offset up india?
» ifesdjeen (Alex P): @pavlobaron true that. ATM my erlang is just more dirt :)
» frescosecco (frescosecco): “Whether suspend time is really ACC or OWN time is more of a philosophical question”. [#Erlang documentation] #philosophical
» ifesdjeen (Alex P): TCP Proxy in Erlang is easy! Just ran Cassaforte test suite via the proxy. Now to add some socket timeouts & partitions.
» AmarSahinovic (Amar Å ahinović): Excellent and funny #Rackspace interview with Joe Armstrong and Robert Virding, co-creators of #Erlang, https://t.co/RXJ7vNGVsd
» samklr (Sam Bessalah): RT @heinz_gies: Experimenting with #CRDT’s in #Erlang: https://t.co/OyCcH0dbG2
Statistics
Number of aggregated posts: 10651
Most recent article: May 21, 2013
Latest comments
» Moraru on This is Why You Spent All that Time Learning to Program: It is true that computer science was a pain in the back at time that i’ve had to learn it…
» Commercial hand dryers on Couchbase Meetup at new HQ: Buy online from here where you will get so much of variety in Commercial hand dryers for people. If you…
» Fort McMurray Homes on Motivated Reasoning and Erlang vs Python vs Node: I don’t really understand why this post is motivational? I don’t even see a post, just a title. Fort McMurray…