Intro to Swift (for functional programmers) by Adrian Sergheev

June 22, 2023

In the Intro to Swift (for functional programmers) presentation by Adrian Sergheev, he provides an overview of Swift, a high-level language created by Apple for iOS development. Swift is designed to make programming simple and complex tasks possible with its progressive disclosure feature. Adrian aims to show functional programming possibilities in Swift, highlighting its benefits such as immutable data structures, first-class higher-order functions, and strong typing. The presentation covers functions and higher order functions, Swift’s implementation of lazy evaluation, product types, and the Never type. Swift’s error handling and use of protocols for extending the language are also discussed. The speaker shares his personal experience with Swift and functional programming, and answers audience questions. Throughout the video, Adrian demonstrates Swift’s features using code examples.


Intro to Swift (for functional programmers): A comprehensive overview

Getting to know Swift

Adrian Sergheev began by introducing Swift as Apple’s high-level programming language, designed to replace Objective-C. Swift aims to simplify programming, making straightforward tasks easy and complex tasks achievable through its progressive disclosure feature. For functional programmers, Swift offers a range of features like immutability, first-class higher-order functions, and strong typing, which are essential for functional programming paradigms.

 

Embracing immutability and lazy evaluation

Swift promotes the use of immutable data structures and values, making it a good fit for functional programming. Adrian discussed how Swift supports both lazy and eager evaluation, with lazy evaluation being an opt-in feature. This allows computations to be deferred until necessary, which can significantly improve performance.

He demonstrated writing a “Hello World” program in Swift using Xcode Playgrounds, emphasizing Swift’s shift from reference semantics to value semantics. This paradigm shift marks a significant departure from Objective-C, aligning Swift more closely with functional programming practices.

 

Higher-order functions and product types

Adrian delved into higher-order functions, explaining that Swift allows functions to take other functions as parameters and return them as results. He illustrated this with an example of an “apply twice” function, showcasing how functions can be passed and returned seamlessly in Swift.

The discussion then moved to product types, which combine multiple types and define the number of instances that can be created. Product types in Swift help create compile-time impossible states, enhancing code reliability and robustness. For instance, a product of two boolean values results in four possible combinations, which can be managed effectively in Swift.

 

The never type and pattern matching

Adrian introduced the concept of the Never type, corresponding to zero in mathematics. Unlike null in other languages, Never cannot be assigned to a product type, effectively nullifying it. This concept helps avoid impractical states in code.

He also covered data modeling and pattern matching in Swift. Pattern matching, a powerful feature in Swift, allows for binding values and handling different cases elegantly. Adrian demonstrated simple and complex pattern matching techniques, including matching on expressions, tuples, and ranges.

 

Error handling and protocols

Swift offers various ways to handle errors, such as embedding them in an optional type or using the `do-catch` syntax. Adrian prefers the former approach, as it maintains the code within the type system, though it does not support accumulating errors. He mentioned libraries like Validated that address this issue.

Adrian also discussed Swift’s use of protocols, which extend the language and compose functionality. Protocols behave like built-in collection types and support composition over inheritance, making them highly suitable for functional programming.

 

Data fetching, decoding, and concurrency

In his demonstration, Adrian showcased data fetching and decoding using URLSession and the decodable protocol. He highlighted Swift’s structured concurrency, which ensures compile-time error prevention. By decoding a response and creating a UIImage, Adrian illustrated practical data handling and transformation in Swift.

 

Audience engagement and functional programming perspectives

The session concluded with an engaging discussion where audience members shared their experiences with Swift and functional programming. Adrian emphasized that functional programming simplifies complex concepts and provides a robust framework for writing reliable code. He also noted that once you embrace functional programming, it fundamentally changes your approach to coding.

 

Conclusion

Adrian Sergheev’s presentation offered a valuable introduction to Swift for functional programmers. By exploring Swift’s features and demonstrating practical examples, Adrian showcased how Swift aligns with functional programming principles. For developers looking to expand their functional programming toolkit, Swift presents a compelling option with its powerful and versatile capabilities.

For further learning, Adrian recommended exploring Swift’s concurrency and collection types, providing resources and insights to deepen understanding and proficiency in Swift programming.

 

Additional resources

Check out more from the MeetUp Func Prog Sweden. Func Prog Sweden is the community for anyone interested in functional programming. At the MeetUps the community explore different functional languages like Erlang, Elixir, Haskell, Scala, Clojure, OCaml, F# and more.