-
-
Save onmyway133/343daa72f32237e2879a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct Box<T> {} | |
protocol Functor { | |
typealias A | |
typealias B | |
typealias Boxed = Box<B> | |
func fmap<B>(f: A -> B) -> Boxed | |
} | |
extension Array: Functor { | |
typealias A = T | |
typealias B = Any | |
typealias Boxed = [B] | |
func fmap<B>(f: A -> B) -> Boxed { | |
return self.map({ f($0) }) | |
} | |
} | |
extension Optional: Functor { | |
typealias A = T | |
typealias B = Any | |
typealias Boxed = B? | |
func fmap<B>(f: A -> B) -> Boxed { | |
switch self { | |
case .Some(let value): | |
return Boxed(f(value)) | |
case .None: | |
return .None | |
} | |
} | |
} | |
let n: Int? = 1 | |
let m = n.fmap { x in "\(x)" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment