XP is just a number | |
PerlMonks |
A minor epiphany with grep and mapby ase (Monk) |
on Jul 24, 2000 at 14:39 UTC ( [id://24045]=perlmeditation: print w/replies, xml ) | Need Help?? |
I thought I'd share an enlightenment I had recently with the community, hoping someone else can have a similar breakthrough. Until now, I've always shied away from map and grep. I understood how they worked (on the surface) but never seemed to find a situation where they "fit". I didn't fully understand their power. It was like having a tool in my toolbox without taking it out of the wrapper.
The other day I was playing around with a two little OO modules, I'll call one Item and one Collection for sake of argument. Item has two main attributes: Collection is just that: a collection of Item objects with added methods to do stuff with the collections. It's main attribute is ITEMS which is a referance to an array of Item objects. Now that I've set the scene and bored half the audience to tears I come to the problem that led to enlightenment.
I wanted a method in Collection to return a scalar that was a concatenation of the values (sorted numerically) of all the objects in the collection whose USABLE flag was true. I ended up with this:
The great part is, once I wrote it, it seemed 'natural'. It just fit. I used to read this kind of code and go "uhhhh, Ok that's cool but I'll never write something like that let alone be able to decipher it". I feel I've crossed some sort of learning plateau, or passed an important rite of passage.
Back to
Meditations
|
|