by Ossi Hanhinen, @ohanhi
with the support of Futurice 💚.
Licensed under CC BY 4.0.
| extension Array { | |
| func first() -> Element? { | |
| if isEmpty { | |
| return nil | |
| } | |
| return self[0] | |
| } | |
| func last() -> Element? { |
| // See http://blog.sigfpe.com/2008/12/mother-of-all-monads.html | |
| // MARK: function composition | |
| infix operator >>> { associativity left } | |
| func >>> <A, B, C>(f: A -> B, g: B -> C) -> A -> C { | |
| return { x in g(f(x)) } | |
| } | |
| // MARK: Continuation monad | |
| struct Cont<R, A> { |
| // | |
| // Category.swift | |
| // Swift_Extras | |
| // | |
| // Created by Robert Widmann on 9/7/14. | |
| // Copyright (c) 2014 Robert Widmann. All rights reserved. | |
| // | |
| import Foundation |
| // | |
| // Yoneda.swift | |
| // Basis | |
| // | |
| // Created by Robert Widmann on 12/11/14. | |
| // Copyright (c) 2014 Robert Widmann. All rights reserved. | |
| // | |
| import Basis |
| // Playground - noun: a place where people can play | |
| import Cocoa | |
| var str = "Hello, playground" | |
| // Here's take 1. First, I defined the algebra like I would in | |
| // Scala, as separate protocols: | |
| protocol Semigroup { | |
| typealias T |
| // | |
| // Example.swift | |
| // | |
| // Created by Daniel Worku on 11/16/15. | |
| // | |
| // The MIT License (MIT) | |
| // Copyright (c) 2014 Daniel Worku | |
| // | |
| // Permission is hereby granted, free of charge, to any person obtaining a copy of | |
| // this software and associated documentation files (the "Software"), to deal in |
| enum Expression<Recur> { | |
| case Variable(String) | |
| case Abstraction(String, Recur) | |
| case Application(Recur, Recur) | |
| func map<Other>(transform: Recur -> Other) -> Expression<Other> { | |
| switch self { | |
| case let .Variable(x): | |
| return .Variable(x) | |
| case let .Abstraction(x, body): |
A primer/refresher on the category theory concepts that most commonly crop up in conversations about Scala or FP. (Because it's embarassing when I forget this stuff!)
I'll be assuming Scalaz imports in code samples, and some of the code may be pseudo-Scala.
A functor is something that supports map.
| import Foundation | |
| #if os(OSX) | |
| import Darwin | |
| #else | |
| import Glibc | |
| #endif | |
| let path = __FILE__ | |
| public struct ReadError : ErrorType {let reason: String} |