I've been working on more Mac side projects lately. Here's an app I made to scratch an itch: Ghostie is just a window that shows an image. You can set the window to float on top of other windows and adjust the opacity with a slider. I use it to keep reference images in the corner of my screen so I don't have to switch back and forth between spaces.
Most of what I've read about functional programming is big on philosophy and short on pragmatism. This series of posts is my attempt to learn some of the basics of functional programming and to write about them in a way that jerks like me can understand.
filter is a higher-order function that processes a data structure (typically a list) in some order to produce a new data structure containing exactly those elements of the original data structure for which a given predicate returns the boolean value true.
What I Think That Means
We're going to take an array and run a function on each element that will return true or false to determine whether that element should be in a new array. Like our new friend map this sounds a lot like a for-in loop in disguise.
Like map, I maybe see what they're going for here. I still think the more verbose for-in is an easier read but at least the name filter makes it pretty obvious what's happening.
Most of what I've read about functional programming is big on philosophy and short on pragmatism. This series of posts is my attempt to learn some of the basics of functional programming and to write about them in a way that jerks like me can understand.
map is the name of a higher-order function that applies a given function to each element of a list, returning a list of results.
What I Think That Means
I think I can follow this one. A "higher-order" function is a function that takes a function as a variable. Most modern-day developers are familiar with that kind of thing – Objective-C calls it "blocks" and Swift calls it "closures." List is just another word for array. So map is going to take an array, run a function on each element, and return an array of the results. Sounds a lot like a for-in loop!
varnums=[1,2,3,4,5,6]nums=map(nums,{(num)->Intinnum*2})//nums is now [2, 4, 6, 8, 10, 12]
Conclusion
This is okay. It saves some lines over a for-in at the expense of some clarity. A beginner could probably work out the for-in with a little thought but I don't think I'd ever just figure out map without looking at the documentation. My understanding is that map is just the first step into more complex operations – so maybe the tradeoff will make more sense later on.