Skip to content

Instantly share code, notes, and snippets.

@sammoore
Created February 18, 2017 18:07
Show Gist options
  • Save sammoore/bc6ea190a01d037c6e8304af8068c285 to your computer and use it in GitHub Desktop.
Save sammoore/bc6ea190a01d037c6e8304af8068c285 to your computer and use it in GitHub Desktop.
//
// main.swift
// Functor
//
// Created by sam on 10/4/16.
// Copyright © 2016 Sam Moore. All rights reserved.
//
import Foundation
var str: String = "Hello, playground"
// Protocol of a "function"
// OUT OF DATE
protocol Functor {
associatedtype In
associatedtype Out
init<U>(x: In, f: In -> U)
func apply(x: In) -> Out
}
// Operator
infix operator ∆ { }
func ∆<In, Out> (lhs: In, rhs: In -> Out) -> Out {
return rhs(lhs)
}
// Functors
func map<In, Out>(f: In -> Out) -> In -> Out {
return { x in f(x) }
}
func filter<In>(f: In -> Bool) -> In -> In? {
return { x in if f(x) { return x } else { return nil } }
}
// TODO: fix associatvitiy
let string = ("Hello"
∆ map({ x in x + ", world!"}))
∆ filter({ x in x.containsString("Hello")})
print(string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment