Functional.swift playground
struct fn {
static func get<A, B>(property: A) -> [A: B] -> B? {
return { obj in self.get(property, obj) }
}
static func get<A, B>(property: A, _ obj: [A: B]) -> B? {
return obj[property]
}
static func map<A, B>(fn: A -> B) -> [A] -> [B] {
return { list in self.map(fn, list) }
}
static func map<A, B>(fn: A -> B, _ list: [A]) -> [B] {
var result = Array<B>()
for var i = 0, len = list.count; i < len; i++ {
result.append(fn(list[i]))
}
return result
}
static func pluck<A, B>(property: A, _ list: [[A: B]]) -> [B?] {
return self.map(self.get(property), list)
}
}
func double(x: Int) -> Int {
return x * 2
}
var list = [1, 2, 8]
var objs = [
["id": 1],
["id": 2],
["id": 3]
]
// non curried map
fn.map(double, list)
// curried map
var doubleList = fn.map(double)
doubleList(list)
// non curried get
fn.get("id", objs[0])
// curried get
fn.map(fn.get("id"), objs)
// non curried pluck
fn.pluck("id", objs)