From cac9990cd539ba5c76c502097fc9be0ef346cfee Mon Sep 17 00:00:00 2001 From: Daniel Asher Date: Sun, 6 Sep 2015 20:45:24 +0100 Subject: [PATCH 1/4] make `fix` polymorphic in two type variables and add an exception enable version `fixt` --- Swiftx/Combinators.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Swiftx/Combinators.swift b/Swiftx/Combinators.swift index b8677b2..4f01afc 100644 --- a/Swiftx/Combinators.swift +++ b/Swiftx/Combinators.swift @@ -88,10 +88,16 @@ public func |> (a : A, f : A -> B) -> B { /// point at which further application of x to a function is the same x. /// /// x = f(x) -public func fix(f : ((A -> A) -> A -> A)) -> A -> A { +public func fix(f : (A -> B) -> A -> B) -> A -> B { return { x in f(fix(f))(x) } } +/// The fixpoint (or Y) combinator computes the least fixed point of an equation. That is, the first +/// point at which further application of x to a function is the same x. +/// fixt is the exception enabled version of fix. +public func fixt(f : (A throws -> B) -> A throws -> B) -> A throws -> B { + return { x in try f(fixt(f))(x) } +} /// On | Applies the function on its right to both its arguments, then applies the function on its /// left to the result of both prior applications. /// From f04f7db053f5a9be788d45b91910cd0bbf2364e1 Mon Sep 17 00:00:00 2001 From: Daniel Asher Date: Sun, 6 Sep 2015 21:17:09 +0100 Subject: [PATCH 2/4] /// line added above `public func fixt` comment line /// added above `fixt` definition. --- Swiftx/Combinators.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Swiftx/Combinators.swift b/Swiftx/Combinators.swift index 4f01afc..ec4c66b 100644 --- a/Swiftx/Combinators.swift +++ b/Swiftx/Combinators.swift @@ -94,7 +94,8 @@ public func fix(f : (A -> B) -> A -> B) -> A -> B { /// The fixpoint (or Y) combinator computes the least fixed point of an equation. That is, the first /// point at which further application of x to a function is the same x. -/// fixt is the exception enabled version of fix. +/// fixt is the exception-enabled version of fix. +/// public func fixt(f : (A throws -> B) -> A throws -> B) -> A throws -> B { return { x in try f(fixt(f))(x) } } From e24d631c615ee819ccbeeedebbeff5e66193bf81 Mon Sep 17 00:00:00 2001 From: Daniel Asher Date: Sun, 6 Sep 2015 21:22:02 +0100 Subject: [PATCH 3/4] add markdown to comment before `fixt` --- Swiftx/Combinators.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Swiftx/Combinators.swift b/Swiftx/Combinators.swift index ec4c66b..5d70602 100644 --- a/Swiftx/Combinators.swift +++ b/Swiftx/Combinators.swift @@ -94,8 +94,8 @@ public func fix(f : (A -> B) -> A -> B) -> A -> B { /// The fixpoint (or Y) combinator computes the least fixed point of an equation. That is, the first /// point at which further application of x to a function is the same x. -/// fixt is the exception-enabled version of fix. /// +/// `fixt` is the exception-enabled version of fix. public func fixt(f : (A throws -> B) -> A throws -> B) -> A throws -> B { return { x in try f(fixt(f))(x) } } From 7c8e9a21c76bb968fb0054cc6dcf4c341a349f17 Mon Sep 17 00:00:00 2001 From: Daniel Asher Date: Sun, 6 Sep 2015 21:39:22 +0100 Subject: [PATCH 4/4] reformulate `fixt` to use `rethrows` correctly fixt now uses rethrows correctly --- Swiftx/Combinators.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Swiftx/Combinators.swift b/Swiftx/Combinators.swift index 5d70602..f33212f 100644 --- a/Swiftx/Combinators.swift +++ b/Swiftx/Combinators.swift @@ -96,7 +96,7 @@ public func fix(f : (A -> B) -> A -> B) -> A -> B { /// point at which further application of x to a function is the same x. /// /// `fixt` is the exception-enabled version of fix. -public func fixt(f : (A throws -> B) -> A throws -> B) -> A throws -> B { +public func fixt(f : (A throws -> B) throws -> (A throws -> B)) rethrows -> A throws -> B { return { x in try f(fixt(f))(x) } } /// On | Applies the function on its right to both its arguments, then applies the function on its