Freelance iOS Developer, Rapidly Aging Punk

Ghostie

Jan 14, 2016 at 02:23PM

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.

Source on Github, do-want-you-want license.


Functional Programming for Jerks: Filter

Jul 27, 2015 at 05:14PM

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

Wikipedia says:

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.

The Old Way

var nums = [100, 1, 200, 2, 300, 3]

var newNums = [Int]()
for num in nums {
    if num > 99 {
        newNums.append(num)
    }
}
nums = newNums

The Functional Way

var nums = [100, 1, 200, 2, 300, 3]

filter(nums, { (num: Int) -> Bool in
    num > 99
})

Conclusion

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.


Functional Programming for Jerks: Map

Jul 26, 2015 at 01:29PM

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

Wikipedia says:

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!

The Old Way

var nums = [1, 2, 3, 4, 5, 6]

var newNums = [Int]()
for num in nums {
    newNums.append(num * 2)
}

nums = newNums

The Functional Way

var nums = [1, 2, 3, 4, 5, 6]

nums = map(nums, { (num) -> Int in
    num * 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.


Don't Drop the Ball

Jul 17, 2015 at 08:03PM

Here's a little time-waster I made for you:


Reddit Is Not the Internet

Jul 16, 2015 at 05:21PM

The entire Reddit/Ellen Pao situation is deplorable but this statement in her new opinion piece on the Washington Post just makes me sad:

Reddit is the Internet, and it exhibits all the good, the bad and the ugly of the Internet.

Reddit is certainly not the Internet. If hateful people want to spew their garbage thoughts onto a website, Reddit has no obligation to be that place.