Sometimes you come across something that you just find really interesting. Like making lists out of lambdas. While playing around with sets in python at work I came across map, filter, and reduce functions, I thought these were neat but at the time had no use for them. Lo and behold, I decided I needed map a few days ago.
Map
Suppose I've got a fun list like this: (In Perl)
@a = ("blarg.raw","blewy.raw","breqs.raw","blraw.raw");
suppose I want to strip .raw off each element of the array?
@a = map { s/\.raw//; $_ } @a
Guess what @a looks like now? Yea you guessed it:
@a = ("blarg", "blewy", "breqs","blraw")
I happened to need this in perl, so thus the perl syntax.
Filter
While talking to a buddy he mentioned that he was taking two lists, finding the smallest element of one list, and then kicking any elements that were smaller from the second list. Sounds like a job for Filter to me!
I haven't tested it but based on my understanding something like this should work:
lowestElement = 10
yourList = [1,2,3,10,45,6,7,8,876,543,3245]
yourList = filter((lambda x: x > lowestElement), yourList)
guess what yourList looks like now?
yourList = [10,45,876,543,3245]
Pretty awesome right?
Now I hadn't suggested it to my buddy but I'm thinking that we can somehow use reduce.
Update: (12/5/13)
If you want to "mimic" a similar functionality as filtering you can kind of use map, in this case here is an example:
@a = map { m/.*\.raw/ () : $_} @a;
so in this case if we match "raw" in the name, then we can toss it out, otherwise we can store off the value.This allows us to reduce to only a set of files that don't already contain .raw, we can also do other operations.in place of the regex match, but I wanted to keep this around for reference (I actually needed to use it today)
Reduce
I'm not even remotely sure if this could work, but reduce keeps using elements in your array.
Something like this *might* work for him
firstList = [2,3,4,5,6,7,8,10]
firstList = reduce((lambda x, y: y if x>=y else x), firstList)
So the idea here is that we compare each element, to eachother, find the smaller of the two, and then get rid of the other.
I think that's pretty neat, and I expect the result to be 2 in the above example.