Skip to content

Commit 4c8f24b

Browse files
committed
Ap for Trampolines
1 parent c100c94 commit 4c8f24b

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

Basis/Trampoline.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,20 @@ extension Trampoline : Applicative {
6464
public static func pure<A>(a : A) -> Trampoline<A> {
6565
return Trampoline<A>(Pure(x: a))
6666
}
67+
68+
public static func ap<B>(stfn: Trampoline<A -> B>) -> Trampoline<A> -> Trampoline<B> {
69+
return { st in
70+
return Trampoline<B>(stfn.t.flatMap({ f in
71+
return st.t.flatMap({ a in
72+
return Pure(x: f(a))
73+
})
74+
}))
75+
}
76+
}
6777
}
6878

6979
public func <*><A, B>(stfn: Trampoline<A -> B>, st: Trampoline<A>) -> Trampoline<B> {
70-
return Trampoline(stfn.t.flatMap({ f in
71-
return st.t.flatMap({ a in
72-
return Pure(x: f(a))
73-
})
74-
}))
80+
return Trampoline.ap(stfn)(st)
7581
}
7682

7783
public func *><A, B>(a : Trampoline<A>, b : Trampoline<B>) -> Trampoline<B> {

0 commit comments

Comments
 (0)