A soon-to-be launching weekly video series exploring Swift and functional programming!
For the past 3 years, since Swift’s early days, I have been a strong proponent of adopting the techniques of functional programming to improve the understandability and maintainability of Swift code. I co-organize the Functional Swift Conference, I write extensively on my site, give talks whenever I can, and open-sourced the iOS and Android code bases at Kickstarter to show how these techniques can be used in a real-life code base. However, functional programming often comes across as foreign and intimidating, and so there’s always more work that can be done to make it approachable!
We learned quite a bit while making this site! From the beginning we knew we wanted to make the site using server-side Swift, and we wanted to approach it in a functional way. We wanted the server to just be a pure function that takes a request and outputs a response. We wanted all the side-effects to be pushed to the boundaries of the application, and then be interpreted in an understandable and testable manner. And we wanted to embrace views as composable, stateless functions.
Turns out, if you accomplish all of the above, all types of fun stuff starts popping out. First, you get to easily write tests that traverse the full stack of the application and make assertions on every little thing that happened along the way. Then, because the server is just a pure function, you can easily load it up in a Swift Playground and pipe requests through, including
And finally, we developed an all-purpose snapshot testing library so that we could take full snapshots of HTML text and screenshots of pages at different browser sizes.
We are so excited with our findings that we decided to open-source the whole collection of libraries we made that aid in developing server-side Swift. We are also open-sourcing the full source code of the site itself! You can find all of the repositories on the Point-Free GitHub organization, but here are some of the interesting things you will find:
- github/pointfreeco: The source to the full www.pointfree.co site! It includes all of the routing and server middleware, HTML views, CSS styling and business logic.
- github/pointfreeco-server: A barebones Kitura application that delegates all of the request-to-response lifecycle responsibilities to pointfreeco.
- github/swift-web: A collection of types and functions that aid in server-side developing, including HTML/CSS creation and rendering, request routing and server middleware.
- github/swift-prelude: Offers a standard library for experimental functional programming in Swift.
- github/swift-snapshot-testing: A full-featured snapshot testing library to capture a data structure as text, image or anything, and make assertions against reference data.
So, this is what I’ve been up to since leaving Kickstarter a few months ago! We hope to have our first episodes live by the end of this year. Please consider signing up to show us how much interest is out there for such a thing, and to be notified when we launch!
Also, if any of this interests you and you want to collaborate on some work together, I’m available for hire.