{"id":13800338,"url":"https://github.com/alskipp/Swift-Adventures-In-Monad-Land","last_synced_at":"2025-05-13T09:31:31.524Z","repository":{"id":29775880,"uuid":"33319871","full_name":"alskipp/Swift-Adventures-In-Monad-Land","owner":"alskipp","description":"A Swift adventure with Optionals, Monads, bananas and squirrels","archived":false,"fork":false,"pushed_at":"2016-09-15T10:08:19.000Z","size":142,"stargazers_count":171,"open_issues_count":0,"forks_count":11,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-11-18T15:51:56.701Z","etag":null,"topics":["monad","playground","squirrel","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alskipp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-02T16:36:49.000Z","updated_at":"2023-04-11T15:34:58.000Z","dependencies_parsed_at":"2022-09-06T11:20:29.996Z","dependency_job_id":null,"html_url":"https://github.com/alskipp/Swift-Adventures-In-Monad-Land","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alskipp%2FSwift-Adventures-In-Monad-Land","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alskipp%2FSwift-Adventures-In-Monad-Land/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alskipp%2FSwift-Adventures-In-Monad-Land/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alskipp%2FSwift-Adventures-In-Monad-Land/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alskipp","download_url":"https://codeload.github.com/alskipp/Swift-Adventures-In-Monad-Land/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253913107,"owners_count":21983259,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["monad","playground","squirrel","swift"],"created_at":"2024-08-04T00:01:11.547Z","updated_at":"2025-05-13T09:31:30.285Z","avatar_url":"https://github.com/alskipp.png","language":"Swift","funding_links":[],"categories":["Theoretical Computer Science"],"sub_categories":["Functional Reactive Programming"],"readme":"Swift Adventures In Monad Land\n========\n\nQuestion: Is it vital to understand the concept of Monads to program in Swift?\n\nAnswer: No.\n\n* * *\n\nBut it's an interesting topic nonetheless and if you tinker with Swift enough you'll find yourself doing monadic things, perhaps without realising.\n\nThe 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](http://www.meetup.com/swiftlondon/) and [Swift Summit](https://www.swiftsummit.com) and are presented as Xcode Playground files. The video of my Swift London talk (Swift and Nothingness) can be found [here.](https://skillsmatter.com/skillscasts/6203-swift-and-nothingness)\n\nPlaygrounds\n---\n\nNOTE: Xcode 8.0 or higher is recommended.\n\n### 1a ) I See No Nil\n\nThe weird world of nil in Swift.\n\n```swift\nnil \u003c 0\nTrue\n```\n\n### 1b) Going Bananas\n\nA cautionary tale about self-generating Swift bananas.\n\n```swift\nlet banana:🍌 = nil\n🍌\n```\n\n### 2a) Maybe Type\n\nHow to implement the Optional type.\n\n```swift\nenum Maybe\u003cT\u003e : NilLiteralConvertible {\n    case None\n    case Some(T)\n}\n```\n\n### 2b) Maybe Type Monad\n\nThe monad is revealed.\n\n```swift\nfunc \u003e\u003e= \u003cA,B\u003e (m: Maybe\u003cA\u003e, f: A -\u003e Maybe\u003cB\u003e) -\u003e Maybe\u003cB\u003e {\n    switch m {\n    case .None : return .None\n    case .Some(let m) : return f(m)\n    }\n}\n```\n\n### 3a) Optional Madness\n\nWhen Optionals go Bad!\n\n```swift\npeople.filter { person in person.pet?.age \u003c 4 }\n// non-existent pets are younger than 4?!\n```\n\n### 4a) Three Binds are Better than One\n\nYou're looking for one Optional bind, then three turn up at once. (With a side helping of JSON parsing).\n\n```swift\n\u003e\u003e=\n?\nif let\nflatMap\n```\n\n### 5a) flatMap for Array\n\nMonadic stocktaking for squirrels using curried functions.\n\n```swift\ncountOfHazelNuts = squirrels.flatMap(nutsOfType(.Hazel)).count\n```\n\n### 5b) More flatMap for Squirrels\n\nSquirrels, flatMap and the ‘beaky’ operator |\u003e.\n\n```swift\nsquirrelWithNearestCache = squirrels \n                        |\u003e minBy { $0.caches.map(distance) |\u003e minElement }\n```\n\n### 5c) Last of the Squirrels\n\n**TODO** Squirrels, flatMap and function composition.\n\n### 6a) Threatening Monad\n\nBeginners guide to managing an oppressive surveillance state.\n\n```swift\nsafeGroup \u003e\u003e= addPerson(Person(name: \"Gideon\", occupation: \"MP\", threat: .Elevated))\n```\n\n* * *\n\nSo, what is a ‘Monad’ anyway? (Don't expect a straight answer)\n---\n\nIt'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:\n\n\u003e 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).\n\nFurnished 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?’\n\nThe equivalent, abstract answer to, ‘What is a monad?’, might be:\n\n\u003e A monad is a monoid in the category of endofunctors, what's the problem?\n\u003e\n\u003e\u003e [A Brief, Incomplete, and Mostly Wrong History of Programming Languages](http://james-iry.blogspot.co.uk/2009/05/brief-incomplete-and-mostly-wrong.html)\n\nAn 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?…\n\n* * *\n\nRather 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.\n\nIn 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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falskipp%2FSwift-Adventures-In-Monad-Land","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falskipp%2FSwift-Adventures-In-Monad-Land","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falskipp%2FSwift-Adventures-In-Monad-Land/lists"}