Hacker News new | past | comments | ask | show | jobs | submit login
Destructuring with Ruby (pitluga.com)
84 points by drfatbooty on Aug 10, 2011 | hide | past | favorite | 5 comments



This is how it starts. First you're passing out arrays and using Ruby's destructuring shortcut.

Then you're noticing other languages have true multiple returns. You begin hankering after neat little error handling idioms and useful state machine hacks. You begin tinkering with Lua or various Lisps.

But soon that's not enough. You find yourself huffing full blown pattern matching operators and dissing statefulness. You'd ask for an intervention from friends, but that would require I/O. You're doomed.


What really hooked me on destructuring and argument pattern matching was a sophomore year lecture on red black trees in ML. The rotation function was implemented multiple times with different patterns matching the different local tree structures that needed rotating. Much much more elegant than any other implementation of that they I've ever seen.


Get a copy of Okasaki's "Purely Functional Data Structure".

Alas, as far as I remember, deleting an element out of a purely functional red-black tree is more messy.


Hello, I'm Matthias. I'm a functional programmer.


FWIW all of those examples work in Python 3 as well. The language was extended with PEP 3132[1] to support "extended iterable unpacking" (I wish they would just call them "splats").

Python doesn't have a `values_at` method, though `map(hash.get, keys)` or `[hash[key] for key in keys]` isn't too bad.

[1] http://www.python.org/dev/peps/pep-3132/




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: