All Projects → alskipp → Swift Adventures In Monad Land

alskipp / Swift Adventures In Monad Land

A Swift adventure with Optionals, Monads, bananas and squirrels

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Swift Adventures In Monad Land

Whats New In Swift 5 1
An Xcode playground that demonstrates the new features introduced in Swift 5.1.
Stars: ✭ 122 (-26.51%)
Mutual labels:  playground
Ood Principles In Swift
💎 The Principles of OOD (SOLID) based on Uncle Bob articles.
Stars: ✭ 1,710 (+930.12%)
Mutual labels:  playground
Burrido
Do-notation for JavaScript
Stars: ✭ 150 (-9.64%)
Mutual labels:  monad
Pulsesensorplayground
A PulseSensor library (for Arduino) that collects our most popular projects in one place.
Stars: ✭ 126 (-24.1%)
Mutual labels:  playground
Testdrive
Quickly try out any Swift pod or framework in a playground
Stars: ✭ 1,612 (+871.08%)
Mutual labels:  playground
Layer5
Layer5, the service mesh company, representing every service mesh
Stars: ✭ 137 (-17.47%)
Mutual labels:  playground
Ios Design Patterns
Learning ground for iOS Design Pattern included with sample projects for MVC, MVP, MVVM, and VIPER
Stars: ✭ 120 (-27.71%)
Mutual labels:  playground
Web Maker
A blazing fast & offline frontend playground
Stars: ✭ 2,029 (+1122.29%)
Mutual labels:  playground
Rubico
[a]synchronous functional programming
Stars: ✭ 133 (-19.88%)
Mutual labels:  monad
Widgetsplayground
前端组件管理系统
Stars: ✭ 150 (-9.64%)
Mutual labels:  playground
What The Filter
A visual playground to JavaScript array & object transformations.
Stars: ✭ 128 (-22.89%)
Mutual labels:  playground
Play Monadic Actions
A simple scala DSL to allow clean and monadic style for Play! Actions
Stars: ✭ 129 (-22.29%)
Mutual labels:  monad
Coredataplaygrounds
Exploring Core Data through Swift playgrounds
Stars: ✭ 139 (-16.27%)
Mutual labels:  playground
Vuep.run
🏃 An online playground for Vue2.0
Stars: ✭ 125 (-24.7%)
Mutual labels:  playground
Whats New In Swift 4
An Xcode playground showcasing the new features in Swift 4.0.
Stars: ✭ 1,860 (+1020.48%)
Mutual labels:  playground
Demosify
Create a playground to show the demos of your projects.
Stars: ✭ 121 (-27.11%)
Mutual labels:  playground
Swift Summary
A summary of Apple's Swift language written on Playgrounds
Stars: ✭ 1,668 (+904.82%)
Mutual labels:  playground
Fpgo
Monad, Functional Programming features for Golang
Stars: ✭ 165 (-0.6%)
Mutual labels:  monad
Play With Docker
You know it, you use it, now it's time to improve it. PWD!.
Stars: ✭ 2,103 (+1166.87%)
Mutual labels:  playground
Functional Promises
Write code like a story w/ a powerful Fluent (function chaining) API
Stars: ✭ 141 (-15.06%)
Mutual labels:  monad

Swift Adventures In Monad Land

Question: Is it vital to understand the concept of Monads to program in Swift?

Answer: No.


But it's an interesting topic nonetheless and if you tinker with Swift enough you'll find yourself doing monadic things, perhaps without realising.

The purpose of this repository is to explore concepts relating to the 'M' word in Swift. The ideas are developed from talks I gave at Swift London Meetup and Swift Summit and are presented as Xcode Playground files. The video of my Swift London talk (Swift and Nothingness) can be found here.

Playgrounds

NOTE: Xcode 8.0 or higher is recommended.

1a ) I See No Nil

The weird world of nil in Swift.

nil < 0
True

1b) Going Bananas

A cautionary tale about self-generating Swift bananas.

let banana:🍌 = nil
🍌

2a) Maybe Type

How to implement the Optional type.

enum Maybe<T> : NilLiteralConvertible {
    case None
    case Some(T)
}

2b) Maybe Type Monad

The monad is revealed.

func >>= <A,B> (m: Maybe<A>, f: A -> Maybe<B>) -> Maybe<B> {
    switch m {
    case .None : return .None
    case .Some(let m) : return f(m)
    }
}

3a) Optional Madness

When Optionals go Bad!

people.filter { person in person.pet?.age < 4 }
// non-existent pets are younger than 4?!

4a) Three Binds are Better than One

You're looking for one Optional bind, then three turn up at once. (With a side helping of JSON parsing).

>>=
?
if let
flatMap

5a) flatMap for Array

Monadic stocktaking for squirrels using curried functions.

countOfHazelNuts = squirrels.flatMap(nutsOfType(.Hazel)).count

5b) More flatMap for Squirrels

Squirrels, flatMap and the ‘beaky’ operator |>.

squirrelWithNearestCache = squirrels 
                        |> minBy { $0.caches.map(distance) |> minElement }

5c) Last of the Squirrels

TODO Squirrels, flatMap and function composition.

6a) Threatening Monad

Beginners guide to managing an oppressive surveillance state.

safeGroup >>= addPerson(Person(name: "Gideon", occupation: "MP", threat: .Elevated))

So, what is a ‘Monad’ anyway? (Don't expect a straight answer)

It's traditional to make an obscure analogy about what a monad is, but in a slight deviation from tradition, here's an analogy about why the question is difficult to answer. It's similar to asking, ‘What is a mammal?’. There are many instances of mammals in the world and there are also the ‘laws’ which define the attributes of a mammal. Here are some of the ‘laws’ according to wikipedia:

Mammals are a clade of endothermic amniotes distinguished from reptiles and birds by the possession of hair, three middle ear bones, mammary glands, and a neocortex (a region of the brain).

Furnished with just this information, it is unlikely you'd be able to point at an actual instance of a mammal and would probably resort to the follow-up question, ‘So, what's a mammal?’

The equivalent, abstract answer to, ‘What is a monad?’, might be:

A monad is a monoid in the category of endofunctors, what's the problem?

A Brief, Incomplete, and Mostly Wrong History of Programming Languages

An entertaining, yet enigmatic answer. In practice, it's not too complicated. At the heart of a monad there is the ‘bind’ operation, which is simply a higher order function. But, what's a higher order function?…


Rather than start from the abstract concept, an alternative approach is to sneak up on actual instances of mammals and monads in the wild and study how they behave. That's the approach which will be taken here.

In Swift, the monad you will first confront in the wilderness is the Optional type. That's why Swift Adventures in Monad Land will start with an investigation of nil, NilLiteralConvertible and Optionals. Despite the fact that there is no requirement to know that Optionals are monads to make use of them in Swift (just as it's not a requirement to know that a donkey is a mammal to get it to till your field), attaining knowledge of the true nature of your Optional or donkey can be both useful and rewarding.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].