Wednesday, March 21, 2018

functional programming: from confusion to warm and fuzzy feelings

When I first heard of functional programming, it was explained at "stateless." From the object-oriented perspective I was raised with, this seemed crazy. How could you make something stateless? How do you organize everything? Well, you just pass information from function to function. But still, I didn't get it. What do you mean functions always do the same thing, no matter what you pass in? You can pass in all sorts of different information as input? What?

Then, Dmitri Vagner said that functional programming is just a categorical way of thinking about programming, and it all made sense. Of course functions don't update state! They're just functions, and they have an input and an output but they don't change the input; they just give a new object (object?) that is the output. A program just has a bunch of compositions of functions to get done what you need to get done, and you can say things about the compositions of functions without talking about specific inputs since you can say things about all the inputs at once (it's a function. It inherently knows about its domain and codomain. functions are lots of times the sole focus of study because functions hold all of the information, while objects can't even tell how they relate to one another!). Of course functional programing is (maybe? sometimes?) more efficient, since you can use your functor for anything in the category you're working in (and some functors work for many categories!). Of course you don't need state for that. You set up a useful system and to do a specific example, you just plug in the input and get some output.

And the immutable data of functional programming is not so scary; it's just different. Instead of updating an object (and possibly losing track of what you have and haven't done to it) you just make another one. And immutable data is just about the opposite of a global (mutable) variable, which is the bane of debugging and one of my biggest pet peeves. Macaulay2, I will never forgive you for having definition by equals sign give a global variable. It is sacrilege! But functional programming, you're newly added to my todo list. You're kind of near the end because you're not necessary at the moment, but you're on there.

No comments:

Post a Comment