Recorded – watch the full episode on YouTube.
How would you explain functional programming when speaking to junior developers? They haven't tried it before, what is it?
Daniel Steinberg: It's different things to different people, but one of the things I like to think of is, when we're building an app, it's best if we organize our behavior, our state in different ways and one way to do that is encapsulation we learned in objects. And another way is with functions. And with functions, we think about things like immutability so that things don't change out from under us when we're not expecting it. And we think about functions the way mathematicians think of functions and that is, if you give me a function and you give me the same input, you better get the same output every time.
“In functional programming, we want that repeatability, we want dependability and we want to know when things are mutating and be able to train it a little bit.”
And that's not what we do in object-oriented programming. We call
random() and we get a random number, we call
random() again, we get a different number. And we're perfectly fine with that. And so in functional programming, we want that repeatability, we want dependability and we want to know when things are mutating and be able to train it a little bit.
Now, I'm not an expert in functional programming. I am learning it. I'm learning it to the point that I feel comfortable explaining it to people but I'm only a several years in and so there are people that know it. In the old days, when we did something in Objective-C or Java, people would say, “oh, we did that in Smalltalk 20 years ago.” And now we hear the same thing. We say something clever in functional programming and the Haskell people say, “oh, we did that 20 years ago.”
“One of the things you have to get your head around in functional programming is functions are first-class objects. So the same way you can pass me a string, you can pass me a function.”
Paul Hudson: So when Daniel said he's learning functional programming, what he means is that... What? 18 months ago, I met Daniel at NSSpain and I said, "Dan, you've been doing so many great functional talks, doing such great work. When are you going to write this functional programming book?" And it just so happens, Daniel, what do you have to say about functional programming book?
Daniel Steinberg: I have just made the first chapter of the book. This first chapter is really special. I've been putting this through tech review and I had this throwaway page where I made this reference to magic. And James Dempsey gave me some feedback. He said, "I want more here." And it grew into an entire chapter. And so the first chapter is sort of using a magic metaphor and a magician doing a trick and we learned how to do a trick using a mutating deck of cards and then a non-mutating deck of cards.
"If functions can accept strings, they can also accept other functions. And this gets you ready for things that are in the Swift standard library."
To continue what you're asking about functions, so I'll finally get to the question you asked me, the second chapter I talk about functions aren't special, because that's one of the things you have to get your head around in functional programming is functions are first class objects. So the same way you can pass me a string, you can pass me a function. And the same way I can store a string in a property, you can store a function in a property.
And then in chapter three, we take a look at, well if functions can accept strings, they can also accept other functions. And this gets you ready for things that are in the Swift standard library, like map and sort and filter and all those. And then the end of the book, Luis will tell you about all of it, spoiler alert, the end of the book says we have map for arrays, we have map for optionals. You can write your own map for your own types. You can write your own flat map and it's a why and when you want to do that.
“Functions aren't special. They're just like every other type.”
And so it's my usual kickstart style. There are more comprehensive introductions to functional programming, but my idea is you're a smart programmer, you know many things, you just don't know this thing. And so that's what my books tend to do, is you don't know this thing, let me tell you how I think about it.
Paul Hudson: Fair enough. I've got a really challenging question here from Prathamesh Kowarkar, who asks, and this is a hard one, “What is the one-line definition of functional programming?” If you're asked that question in an interview, how would you respond?
Daniel Steinberg: That's sort of the point in my second chapter is I respond by saying, "It's a world in which functions aren't special. Functions are just like every other type."
Paul Hudson: Nice. There you go, folks. That's the one line right there. Functions aren't special. They're just like every other type.
This transcript was recorded as part of Swiftly Speaking. You can watch the full original episode on YouTube, or subscribe to the audio version on Apple Podcasts.
SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Link copied to your pasteboard.