Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mokagio/Swift-Functor-Introduction-Playground
A playground to introduce Functors in Swift, and their practical usage. From my Melbour CocoaHeads #85 lighting talk.
https://github.com/mokagio/Swift-Functor-Introduction-Playground
Last synced: 25 days ago
JSON representation
A playground to introduce Functors in Swift, and their practical usage. From my Melbour CocoaHeads #85 lighting talk.
- Host: GitHub
- URL: https://github.com/mokagio/Swift-Functor-Introduction-Playground
- Owner: mokagio
- Created: 2015-07-12T01:59:08.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-07-12T01:59:48.000Z (over 9 years ago)
- Last Synced: 2024-11-15T21:42:44.534Z (28 days ago)
- Language: Swift
- Size: 121 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- Awesome-Swift-Playgrounds - Functors in Swift - A playground to introduce Functors in Swift, and their practical usage. ⏳ (Theoretical Computer Science / Functional Reactive Programming)
README
# Functors in Swift - A simple introduction
> Note: This Playground is written in Swift 2.0 and Xcode 7 Beta 3.
This is the playground for my lightning talk at [July's 2015 Melbourne CocoaHeads](http://www.melbournecocoaheads.com/).
The talk is a simple introduction to Functors and Monads in Swift, focusing on the practical use of these concepts, rather than their mathematical definition.
The cool things about Functors and Monads are `map` and `flatMap`. Using these we can simplify code like this:
```swift
func getFancyEmojiForUser() -> String? {
if let actuallyAUser = userFromDatabase() {
let joinedUserName = joinedName(actuallyAUser)if let emojiForUser = emojiFromString(joinedUserName) {
return fancifyEmoji(emojiForUser)
}
}return .None
}
```into something that is easier to write and specially **easier to reason about**.
```swift
func getFancyEmojiForUser() -> String? {
return userFromDatabase()
.map(joinedName)
.flatMap(emojiFromString)
.map(fancifyEmoji)
}
```If you are interested in this tweet me [@mokagio](https://twitter.com/mokagio) or visit [mokacoding](http://mokacoding.com)
---
(c) 2015 - Giovanni Lodi