|< What are the advantages of functional programming in Swift?||How to improve your functional programming skills? >|
Recorded – watch the full episode on YouTube.
Would you have any advice on how to get into a more functional programming approach?
Daniel Steinberg: Well, some of the things is to realize we do it already, that we have a map for instance in Swift, and what map says is you give me a function, I will apply it to every element of the array. You give me a function, I'll apply it. I'll reach inside an optional, apply it to that and then rewrap it. And so we're already doing some of it. Some of it is just awareness to see, “Oh, when are you doing it?”
“And so once we realize function is just another way for writing a let assignment, it's kind of cool.”
The other thing is little things like if you have a function, you can rewrite it as an assignment and so instead of a function, as I said, increase before, I could say let increase equal and have a closure that does what the function used to do. And so once we realize function is just another way for writing a let assignment, it's kind of cool.
Again, I don't want to oversell my experience. I'm still new to this. And I'm just having a ball. And so part of it is, we're at home, we've got some time, it's a great time to learn something new.
Paul Hudson: It really is. We are all indeed at home. I think map is an interesting one because it is one that folks I think can see the use of very quickly, particularly when on sequences and using things like
sorted() is a great example because we don't even think about that. We use it all the time, but you can pass it how you want things to be compared.
We use sorted out of the box and don't pass it anything. So it uses just the ordinary less than but you can say, "I want to sort it on this property," or, "I want to sort it on some other feature," or, "I want to sort it in reverse order." And so you pass it a function that says, "You give me two elements. How do I compare them?" This is also wonderful about functional programming. I don't know what sorting algorithms sorted uses. My job is only to say, "Here's how you compare two elements."
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.
SPONSORED Join a FREE crash course for iOS devs who want to become complete senior developers — from October 18th to 24th. Learn how to apply iOS app architecture patterns through a series of lectures and practical coding sessions.
Link copied to your pasteboard.