FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

What are the first steps for functional programming in Swift?

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() or sort().

Daniel Steinberg: 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.

Listen on Apple Podcasts

Hacking with Swift is sponsored by Essential Developer

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.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.