Skip to content

Commit

Permalink
Merge pull request #15 from facile-it/CAI-86-free-functions-namespace
Browse files Browse the repository at this point in the history
CAI-86 moved all the free functions to the `f` namespace
  • Loading branch information
broomburgo authored Jan 11, 2018
2 parents 607e9ee + acd63ed commit 6dd827e
Show file tree
Hide file tree
Showing 31 changed files with 722 additions and 695 deletions.
6 changes: 6 additions & 0 deletions FunctionalKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
380C6FF41FDC1D9100DA01FB /* FunctorTransformers3.generated.swift in Sources */ = {isa = PBXBuildFile; fileRef = 380C6FF01FDC1D9100DA01FB /* FunctorTransformers3.generated.swift */; };
380C6FF71FDC1D9100DA01FB /* FunctorTransformers2.generated.swift in Sources */ = {isa = PBXBuildFile; fileRef = 380C6FF21FDC1D9100DA01FB /* FunctorTransformers2.generated.swift */; };
380C6FF81FDC1D9100DA01FB /* FunctorTransformers2.generated.swift in Sources */ = {isa = PBXBuildFile; fileRef = 380C6FF21FDC1D9100DA01FB /* FunctorTransformers2.generated.swift */; };
381E9B24200764B200A392DB /* NameSpace.swift in Sources */ = {isa = PBXBuildFile; fileRef = 381E9B23200764B200A392DB /* NameSpace.swift */; };
381E9B25200764B200A392DB /* NameSpace.swift in Sources */ = {isa = PBXBuildFile; fileRef = 381E9B23200764B200A392DB /* NameSpace.swift */; };
385370691FC40D86004096EB /* ArrayType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 385370681FC40D86004096EB /* ArrayType.swift */; };
3853706A1FC40D86004096EB /* ArrayType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 385370681FC40D86004096EB /* ArrayType.swift */; };
3853706C1FC44178004096EB /* ReaderType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3853706B1FC44178004096EB /* ReaderType.swift */; };
Expand Down Expand Up @@ -140,6 +142,7 @@
380C6FE91FDC0EF400DA01FB /* MonadTransformers2.generated.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MonadTransformers2.generated.swift; sourceTree = "<group>"; };
380C6FF01FDC1D9100DA01FB /* FunctorTransformers3.generated.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FunctorTransformers3.generated.swift; sourceTree = "<group>"; };
380C6FF21FDC1D9100DA01FB /* FunctorTransformers2.generated.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FunctorTransformers2.generated.swift; sourceTree = "<group>"; };
381E9B23200764B200A392DB /* NameSpace.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NameSpace.swift; sourceTree = "<group>"; };
385370681FC40D86004096EB /* ArrayType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrayType.swift; sourceTree = "<group>"; };
3853706B1FC44178004096EB /* ReaderType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReaderType.swift; sourceTree = "<group>"; };
3853706E1FC46BA4004096EB /* FutureType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FutureType.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -251,6 +254,7 @@
38F179AF1FBDA7B400243994 /* Combinators.swift */,
38F179B01FBDA7B400243994 /* Curry.swift */,
38F179B11FBDA7B500243994 /* Fixed.swift */,
381E9B23200764B200A392DB /* NameSpace.swift */,
38F179B21FBDA7B500243994 /* Operators.swift */,
38F179B61FBDA7B500243994 /* Typealiases.swift */,
38F179AC1FBDA7B400243994 /* Weakly.swift */,
Expand Down Expand Up @@ -559,6 +563,7 @@
38F179C21FBDA7B500243994 /* Fixed.swift in Sources */,
38F179C01FBDA7B500243994 /* Curry.swift in Sources */,
38F179C81FBDA7B500243994 /* Product.swift in Sources */,
381E9B25200764B200A392DB /* NameSpace.swift in Sources */,
38F179C41FBDA7B500243994 /* Operators.swift in Sources */,
385370701FC46BA4004096EB /* FutureType.swift in Sources */,
38F179C61FBDA7B500243994 /* Exponential.swift in Sources */,
Expand Down Expand Up @@ -627,6 +632,7 @@
38F179BD1FBDA7B500243994 /* Combinators.swift in Sources */,
38F179C31FBDA7B500243994 /* Operators.swift in Sources */,
38F179BF1FBDA7B500243994 /* Curry.swift in Sources */,
381E9B24200764B200A392DB /* NameSpace.swift in Sources */,
38F179C11FBDA7B500243994 /* Fixed.swift in Sources */,
3853706F1FC46BA4004096EB /* FutureType.swift in Sources */,
38F179BB1FBDA7B500243994 /* Boolean.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
18 changes: 9 additions & 9 deletions Sources/FunctionalKit/ArrayType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ extension ArrayType {

public static func lift<A,Applicative2>(_ function: @escaping (ParameterType, Applicative2.ParameterType) -> A) -> (Self, Applicative2) -> [A] where Applicative2: ArrayType {
return { (ap1, ap2) in
Concrete.pure(fcurry(function)) <*> ap1 <*> ap2
Concrete.pure(f.curry(function)) <*> ap1 <*> ap2
}
}

public static func lift<A,Applicative2,Applicative3>(_ function: @escaping (ParameterType, Applicative2.ParameterType, Applicative3.ParameterType) -> A) -> (Self, Applicative2, Applicative3) -> [A] where Applicative2: ArrayType, Applicative3: ArrayType {
return { ap1, ap2, ap3 in
Concrete.pure(fcurry(function)) <*> ap1 <*> ap2 <*> ap3
Concrete.pure(f.curry(function)) <*> ap1 <*> ap2 <*> ap3
}
}
}
Expand Down Expand Up @@ -112,55 +112,55 @@ extension ArrayType {
typealias Returned = [Traversed<Applicative>]

return fold(Returned.pure([])) { previous, element in
Applicative.Concrete.pure(fcurry(++)) <*> previous <*> transform(element)
Applicative.Concrete.pure(f.curry(++)) <*> previous <*> transform(element)
}
}

public func traverse<Applicative>(_ transform: (ParameterType) -> Applicative) -> Future<Traversed<Applicative>> where Applicative: FutureType {
typealias Returned = Future<Traversed<Applicative>>

return fold(Returned.pure([])) { previous, element in
Applicative.Concrete.pure(fcurry(++)) <*> previous <*> transform(element)
Applicative.Concrete.pure(f.curry(++)) <*> previous <*> transform(element)
}
}

public func traverse<Applicative>(_ transform: (ParameterType) -> Applicative) -> Optional<Traversed<Applicative>> where Applicative: OptionalType {
typealias Returned = Optional<Traversed<Applicative>>

return fold(Returned.pure([])) { previous, element in
Applicative.Concrete.pure(fcurry(++)) <*> previous <*> transform(element)
Applicative.Concrete.pure(f.curry(++)) <*> previous <*> transform(element)
}
}

public func traverse<Applicative>(_ transform: (ParameterType) -> Applicative) -> Reader<Applicative.EnvironmentType,Traversed<Applicative>> where Applicative: ReaderType {
typealias Returned = Reader<Applicative.EnvironmentType,Traversed<Applicative>>

return fold(Returned.pure([])) { previous, element in
Applicative.Concrete.pure(fcurry(++)) <*> previous <*> transform(element)
Applicative.Concrete.pure(f.curry(++)) <*> previous <*> transform(element)
}
}

public func traverse<Applicative>(_ transform: (ParameterType) -> Applicative) -> Result<Applicative.ErrorType,Traversed<Applicative>> where Applicative: ResultType {
typealias Returned = Result<Applicative.ErrorType,Traversed<Applicative>>

return fold(Returned.pure([])) { previous, element in
Applicative.Concrete.pure(fcurry(++)) <*> previous <*> transform(element)
Applicative.Concrete.pure(f.curry(++)) <*> previous <*> transform(element)
}
}

public func traverse<Applicative>(_ transform: (ParameterType) -> Applicative) -> State<Applicative.StateParameterType,Traversed<Applicative>> where Applicative: StateType {
typealias Returned = State<Applicative.StateParameterType,Traversed<Applicative>>

return fold(Returned.pure([])) { previous, element in
Applicative.Concrete.pure(fcurry(++)) <*> previous <*> transform(element)
Applicative.Concrete.pure(f.curry(++)) <*> previous <*> transform(element)
}
}

public func traverse<Applicative>(_ transform: (ParameterType) -> Applicative) -> Writer<Applicative.LogType,Traversed<Applicative>> where Applicative: WriterType {
typealias Returned = Writer<Applicative.LogType,Traversed<Applicative>>

return fold(Returned.pure([])) { previous, element in
Applicative.Concrete.pure(fcurry(++)) <*> previous <*> transform(element)
Applicative.Concrete.pure(f.curry(++)) <*> previous <*> transform(element)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/FunctionalKit/Collections.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ extension Sequence {

extension Sequence where Iterator.Element == Bool {
public var isAllTrue: Bool {
return all(conformTo: fidentity)
return all(conformTo: f.identity)
}

public var isAnyTrue: Bool {
return any(conformsTo: fidentity)
return any(conformsTo: f.identity)
}
}

Expand Down
72 changes: 37 additions & 35 deletions Sources/FunctionalKit/Combinators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,84 +3,86 @@
#endif
import Abstract

public func fcompose <A,B,C> (_ first: @escaping (A) -> B, _ second: @escaping (B) -> C) -> (A) -> C {
return { second(first($0)) }
}
extension f {
public static func compose <A,B,C> (_ firstFunction: @escaping (A) -> B, _ secondFunction: @escaping (B) -> C) -> (A) -> C {
return { secondFunction(firstFunction($0)) }
}

public func fcompose <A,B,C> (_ first: @escaping (A) throws -> B, _ second: @escaping (B) -> C) -> (A) throws -> C {
return { try second(first($0)) }
}
public static func compose <A,B,C> (_ firstFunction: @escaping (A) throws -> B, _ secondFunction: @escaping (B) -> C) -> (A) throws -> C {
return { try secondFunction(firstFunction($0)) }
}

public func fcompose <A,B,C> (_ first: @escaping (A) -> B, _ second: @escaping (B) throws -> C) -> (A) throws -> C {
return { try second(first($0)) }
}
public static func compose <A,B,C> (_ firstFunction: @escaping (A) -> B, _ secondFunction: @escaping (B) throws -> C) -> (A) throws -> C {
return { try secondFunction(firstFunction($0)) }
}

public func fcompose <A,B,C> (_ first: @escaping (A) throws -> B, _ second: @escaping (B) throws -> C) -> (A) throws -> C {
return { try second(first($0)) }
}
public static func compose <A,B,C> (_ firstFunction: @escaping (A) throws -> B, _ secondFunction: @escaping (B) throws -> C) -> (A) throws -> C {
return { try secondFunction(firstFunction($0)) }
}

public func fflip<A,B,C>(_ function: @escaping (A) -> (B) -> C) -> (B) -> (A) -> C {
return { b in { a in function(a)(b) } }
}
public static func flip<A,B,C>(_ function: @escaping (A) -> (B) -> C) -> (B) -> (A) -> C {
return { b in { a in function(a)(b) } }
}

public func fflip<A,B,C>(_ function: @escaping (A) throws -> (B) -> C) -> (B) -> (A) throws -> C {
return { b in { a in try function(a)(b) } }
}
public static func flip<A,B,C>(_ function: @escaping (A) throws -> (B) -> C) -> (B) -> (A) throws -> C {
return { b in { a in try function(a)(b) } }
}

public func fflip<A,B,C>(_ function: @escaping (A) -> (B) throws -> C) -> (B) throws -> (A) throws -> C {
return { b in { a in try function(a)(b) } }
}
public static func flip<A,B,C>(_ function: @escaping (A) -> (B) throws -> C) -> (B) throws -> (A) throws -> C {
return { b in { a in try function(a)(b) } }
}

public func fflip<A,B,C>(_ function: @escaping (A) throws -> (B) throws -> C) -> (B) throws -> (A) throws -> C {
return { b in { a in try function(a)(b) } }
public static func flip<A,B,C>(_ function: @escaping (A) throws -> (B) throws -> C) -> (B) throws -> (A) throws -> C {
return { b in { a in try function(a)(b) } }
}
}

public func <A,B,C> (second: @escaping (B) -> C, first: @escaping (A) -> B) -> (A) -> C {
return fcompose(first, second)
return f.compose(first, second)
}

public func <A,B,C> (second: @escaping (B) -> C, first: @escaping (A) throws -> B) -> (A) throws -> C {
return fcompose(first, second)
return f.compose(first, second)
}

public func <A,B,C> (second: @escaping (B) throws -> C, first: @escaping (A) -> B) -> (A) throws -> C {
return fcompose(first, second)
return f.compose(first, second)
}

public func <A,B,C> (second: @escaping (B) throws -> C, first: @escaping (A) throws -> B) -> (A) throws -> C {
return fcompose(first, second)
return f.compose(first, second)
}

public func .. <A,B,C> (first: @escaping (A) -> B, second: @escaping (B) -> C) -> (A) -> C {
return fcompose(first, second)
return f.compose(first, second)
}

public func .. <A,B,C> (first: @escaping (A) throws -> B, second: @escaping (B) -> C) -> (A) throws -> C {
return fcompose(first, second)
return f.compose(first, second)
}

public func .. <A,B,C> (first: @escaping (A) -> B, second: @escaping (B) throws -> C) -> (A) throws -> C {
return fcompose(first, second)
return f.compose(first, second)
}

public func .. <A,B,C> (first: @escaping (A) throws -> B, second: @escaping (B) throws -> C) -> (A) throws -> C {
return fcompose(first, second)
return f.compose(first, second)
}

public postfix func .. <A,B> (function: @escaping (A) -> B) -> (@escaping (B) -> (B)) -> (A) -> B {
return { bc in fcompose(function, bc) }
return { bc in f.compose(function, bc) }
}

public postfix func .. <A,B> (function: @escaping (A) throws -> B) -> (@escaping (B) -> (B)) -> (A) throws -> B {
return { bc in fcompose(function, bc) }
return { bc in f.compose(function, bc) }
}

public prefix func .. <B,C> (function: @escaping (B) -> C) -> (@escaping (B) -> (B)) -> (B) -> C {
return { ab in fcompose(ab, function) }
return { ab in f.compose(ab, function) }
}

public prefix func .. <B,C> (function: @escaping (B) throws -> C) -> (@escaping (B) -> (B)) -> (B) throws -> C {
return { ab in fcompose(ab, function) }
return { ab in f.compose(ab, function) }
}

public func § <A,B> (function: (A) throws -> B, value: A) rethrows -> B {
Expand Down
18 changes: 9 additions & 9 deletions Sources/FunctionalKit/Coproduct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ extension CoproductType where LeftType: Equatable, RightType: Equatable {
onLeft: { left in
rhs.fold(
onLeft: { left == $0 },
onRight: fconstant(false))
onRight: f.constant(false))
},
onRight: { right in
rhs.fold(
onLeft: fconstant(false),
onLeft: f.constant(false),
onRight: { right == $0 })
})
}
Expand All @@ -52,25 +52,25 @@ extension CoproductType {
}

public var tryLeft: LeftType? {
return fold(onLeft: fidentity, onRight: { _ in nil })
return fold(onLeft: f.identity, onRight: { _ in nil })
}

public var tryRight: RightType? {
return fold(onLeft: { _ in nil }, onRight: fidentity)
return fold(onLeft: { _ in nil }, onRight: f.identity)
}

public func foldToLeft(_ transform: (RightType) -> LeftType) -> LeftType {
return fold(onLeft: fidentity, onRight: transform)
return fold(onLeft: f.identity, onRight: transform)
}

public func foldToRight(_ transform: (LeftType) -> RightType) -> RightType {
return fold(onLeft: transform, onRight: fidentity)
return fold(onLeft: transform, onRight: f.identity)
}
}

extension CoproductType where LeftType == RightType {
public var merged: LeftType {
return fold(onLeft: fidentity, onRight: fidentity)
return fold(onLeft: f.identity, onRight: f.identity)
}
}

Expand All @@ -84,11 +84,11 @@ extension CoproductType {
}

public func mapLeft<T>(_ transform: (LeftType) -> T) -> Coproduct<T,RightType> {
return bimap(transform, fidentity)
return bimap(transform, f.identity)
}

public func mapRight<U>(_ transform: (RightType) -> U) -> Coproduct<LeftType,U> {
return bimap(fidentity, transform)
return bimap(f.identity, transform)
}
}

Expand Down
Loading

0 comments on commit 6dd827e

Please sign in to comment.