Skip to content

Commit

Permalink
Add f.pure instead of f.constant (now deprecated)
Browse files Browse the repository at this point in the history
  • Loading branch information
Elviro Rocca committed Feb 19, 2018
1 parent baf2bce commit f3981e7
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 43 deletions.
4 changes: 2 additions & 2 deletions Sources/FunctionalKit/Adapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ extension AdapterType {
}

public var toLens: LensFull<SType,TType,AType,BType> {
return LensFull.init(get: from, set: to..f.constant)
return LensFull.init(get: from, set: to..f.pure)
}

public static func .. <OtherLens> (lhs: Self, rhs: OtherLens) -> LensFull<SType,TType,OtherLens.AType,OtherLens.BType> where OtherLens: LensType, OtherLens.SType == AType, OtherLens.TType == BType {
Expand All @@ -95,7 +95,7 @@ extension AdapterType {
}

public var toAffine: AffineFull<SType,TType,AType,BType> {
return AffineFull.init(tryGet: from, trySet: to..f.constant)
return AffineFull.init(tryGet: from, trySet: to..f.pure)
}

public static func .. <OtherAffine> (lhs: Self, rhs: OtherAffine) -> AffineFull<SType,TType,OtherAffine.AType,OtherAffine.BType> where OtherAffine: AffineType, OtherAffine.SType == AType, OtherAffine.TType == BType {
Expand Down
2 changes: 1 addition & 1 deletion Sources/FunctionalKit/Affine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ extension PrismType {
public var toAffine: AffineFull<SType,TType,AType,BType> {
return AffineFull<SType,TType,AType,BType>.init(
tryGet: self.tryGet,
trySet: f.constant..self.tryModify)
trySet: f.pure..self.tryModify)
}

public static func .. <OtherAffine> (lhs: Self, rhs: OtherAffine) -> AffineFull<SType,TType,OtherAffine.AType,OtherAffine.BType> where OtherAffine: AffineType, OtherAffine.SType == AType, OtherAffine.TType == BType {
Expand Down
18 changes: 9 additions & 9 deletions Sources/FunctionalKit/AlgebraicOptics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ extension Product {
public static func firstFull<T>(to: T.Type) -> LensFull<Product<A,B>,Product<T,B>,A,T> {
return LensFull<Product<A,B>,Product<T,B>,A,T>.init(
get: { product in product.first },
set: { t in { product in product.mapFirst(f.constant(t)) } })
set: { t in { product in product.mapFirst(f.pure(t)) } })
}

public static func secondFull<T>(to: T.Type) -> LensFull<Product<A,B>,Product<A,T>,B,T> {
return LensFull<Product<A,B>,Product<A,T>,B,T>.init(
get: { product in product.second },
set: { t in { product in product.mapSecond(f.constant(t)) } })
set: { t in { product in product.mapSecond(f.pure(t)) } })
}

public static var first: Lens<Product<A,B>,A> {
Expand Down Expand Up @@ -54,9 +54,9 @@ extension Inclusive {
trySet: { t in
{ inclusive in
inclusive.fold(
onLeft: f.constant(.left(t)),
onLeft: f.pure(.left(t)),
onCenter: { _, b in .center(t,b) },
onRight: f.constant(nil))
onRight: f.pure(nil))
}
})
}
Expand All @@ -67,9 +67,9 @@ extension Inclusive {
trySet: { tu in
{ inclusive in
inclusive.fold(
onLeft: f.constant(nil),
onCenter: f.constant(.center(tu.0,tu.1)),
onRight: f.constant(nil))
onLeft: f.pure(nil),
onCenter: f.pure(.center(tu.0,tu.1)),
onRight: f.pure(nil))
}
})
}
Expand All @@ -80,9 +80,9 @@ extension Inclusive {
trySet: { t in
{ inclusive in
inclusive.fold(
onLeft: f.constant(nil),
onLeft: f.pure(nil),
onCenter: { a, _ in .center(a,t) },
onRight: f.constant(.right(t)))
onRight: f.pure(.right(t)))
}
})
}
Expand Down
4 changes: 2 additions & 2 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: f.constant(false))
onRight: f.pure(false))
},
onRight: { right in
rhs.fold(
onLeft: f.constant(false),
onLeft: f.pure(false),
onRight: { right == $0 })
})
}
Expand Down
20 changes: 20 additions & 0 deletions Sources/FunctionalKit/Fixed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,42 @@ extension f {
return (a,b,c)
}

@available(*, deprecated)
public static func constant <A> (_ a : A) -> () -> A {
return { a }
}

@available(*, deprecated)
public static func constant <A,B> (_ a : A) -> (B) -> A {
return { _ in a }
}

@available(*, deprecated)
public static func constant <A,B,C> (_ a : A) -> (B,C) -> A {
return { _, _ in a }
}

@available(*, deprecated)
public static func constant <A,B,C,D> (_ a : A) -> (B,C,D) -> A {
return { _, _, _ in a }
}

public static func pure <A> (_ a : A) -> () -> A {
return { a }
}

public static func pure <A,B> (_ a : A) -> (B) -> A {
return { _ in a }
}

public static func pure <A,B,C> (_ a : A) -> (B,C) -> A {
return { _, _ in a }
}

public static func pure <A,B,C,D> (_ a : A) -> (B,C,D) -> A {
return { _, _, _ in a }
}

public static func destructure <A,B,T> (_ function: @escaping (A,B) -> T) -> ((A,B)) -> T {
return { tuple in function(tuple.0,tuple.1) }
}
Expand Down
26 changes: 13 additions & 13 deletions Sources/FunctionalKit/Inclusive.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ extension InclusiveType where LeftType: Equatable, RightType: Equatable {
onLeft: { (left) -> Bool in
rhs.fold(
onLeft: { left == $0 },
onCenter: f.constant(false),
onRight: f.constant(false))
onCenter: f.pure(false),
onRight: f.pure(false))
},
onCenter: { (left, right) -> Bool in
rhs.fold(
onLeft: f.constant(false),
onLeft: f.pure(false),
onCenter: { left == $0 && right == $1 },
onRight: f.constant(false))
onRight: f.pure(false))
},
onRight: { (right) -> Bool in
rhs.fold(
onLeft: f.constant(false),
onCenter: f.constant(false),
onLeft: f.pure(false),
onCenter: f.pure(false),
onRight: { right == $0 })
})
}
Expand All @@ -78,37 +78,37 @@ extension InclusiveType where LeftType: Equatable, RightType: Equatable {
extension InclusiveType {
public var tryToProduct: Product<LeftType,RightType>? {
return fold(
onLeft: f.constant(nil),
onLeft: f.pure(nil),
onCenter: Product.init,
onRight: f.constant(nil))
onRight: f.pure(nil))
}

public var tryToCoproduct: Coproduct<LeftType,RightType>? {
return fold(
onLeft: Coproduct.left,
onCenter: f.constant(nil),
onCenter: f.pure(nil),
onRight: Coproduct.right)
}

public var tryLeft: LeftType? {
return fold(
onLeft: f.identity,
onCenter: f.first,
onRight: f.constant(nil))
onRight: f.pure(nil))
}

public var tryRight: RightType? {
return fold(
onLeft: f.constant(nil),
onLeft: f.pure(nil),
onCenter: f.second,
onRight: f.identity)
}

public var tryBoth: (LeftType,RightType)? {
return fold(
onLeft: f.constant(nil),
onLeft: f.pure(nil),
onCenter: f.identity,
onRight: f.constant(nil))
onRight: f.pure(nil))
}
}

Expand Down
20 changes: 10 additions & 10 deletions Sources/FunctionalKit/OptionalType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ extension OptionalType where ParameterType: Equatable {
return lhs.fold(
onNone: {
rhs.fold(
onNone: f.constant(true),
onSome: f.constant(false))
onNone: f.pure(true),
onSome: f.pure(false))
},
onSome: { value in
rhs.fold(
onNone: f.constant(false),
onNone: f.pure(false),
onSome: { value == $0 })
})
}
Expand All @@ -81,7 +81,7 @@ extension OptionalType {
public func fmap <T> (_ transform: (ParameterType) -> T) -> Optional<T> {
return withoutActuallyEscaping(transform) { transform in
fold(
onNone: f.constant(Optional<T>.none),
onNone: f.pure(Optional<T>.none),
onSome: transform..Optional<T>.some)
}
}
Expand Down Expand Up @@ -110,10 +110,10 @@ extension OptionalType {

public static func zip <O1,O2> (_ first: O1, _ second: O2) -> Zipped<O1,O2> where O1: OptionalType, O2: OptionalType, ParameterType == (O1.ParameterType, O2.ParameterType) {
return first.fold(
onNone: f.constant(Zipped<O1,O2>.none),
onNone: f.pure(Zipped<O1,O2>.none),
onSome: { value in
second.fold(
onNone: f.constant(Zipped<O1,O2>.none),
onNone: f.pure(Zipped<O1,O2>.none),
onSome: { Zipped<O1,O2>.some((value,$0)) })
})
}
Expand Down Expand Up @@ -230,10 +230,10 @@ extension OptionalType {
extension OptionalType where ParameterType: OptionalType {
public var joined: Optional<ParameterType.ParameterType> {
return fold(
onNone: f.constant(Optional<ParameterType.ParameterType>.none),
onNone: f.pure(Optional<ParameterType.ParameterType>.none),
onSome: { value in
value.fold(
onNone: f.constant(Optional<ParameterType.ParameterType>.none),
onNone: f.pure(Optional<ParameterType.ParameterType>.none),
onSome: Optional<ParameterType.ParameterType>.some)
})
}
Expand Down Expand Up @@ -272,8 +272,8 @@ extension OptionalType {

public var isNil: Bool {
return fold(
onNone: f.constant(true),
onSome: f.constant(false))
onNone: f.pure(true),
onSome: f.pure(false))
}


Expand Down
2 changes: 1 addition & 1 deletion Sources/FunctionalKit/ReaderType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ extension ReaderType {

extension ReaderType {
public static func pure(_ value: ParameterType) -> Reader<EnvironmentType,ParameterType> {
return Reader.unfold(f.constant(value))
return Reader.unfold(f.pure(value))
}

public func apply <R,T> (_ transform: R) -> Reader<EnvironmentType,T> where R: ReaderType, R.ParameterType == (ParameterType) -> T, R.EnvironmentType == EnvironmentType {
Expand Down
4 changes: 2 additions & 2 deletions Sources/FunctionalKit/ResultType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,14 @@ extension ResultType {
extension ResultType {
public var toOptionalError: ErrorType? {
return fold(
onSuccess: f.constant(nil),
onSuccess: f.pure(nil),
onFailure: f.identity)
}

public var toOptionalValue: ParameterType? {
return fold(
onSuccess: f.identity,
onFailure: f.constant(nil))
onFailure: f.pure(nil))
}

public func fallback(to defaultValue: @autoclosure () -> ParameterType) -> Result<ErrorType,ParameterType> {
Expand Down
4 changes: 2 additions & 2 deletions Tests/FunctionalKitTests/CoproductTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class CoproductTests: XCTestCase {
}

func testFold() {
TestType.left(42).fold(onLeft: f.constant(true), onRight: f.constant(false)) ==! true
TestType.right("42").fold(onLeft: f.constant(false), onRight: f.constant(true)) ==! true
TestType.left(42).fold(onLeft: f.pure(true), onRight: f.pure(false)) ==! true
TestType.right("42").fold(onLeft: f.pure(false), onRight: f.pure(true)) ==! true

TestType.left(42).foldToLeft { Int($0)! } ==! 42
TestType.right("42").foldToRight { "\($0)" } ==! "42"
Expand Down
2 changes: 1 addition & 1 deletion Tests/FunctionalKitTests/FixedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FixedTests: XCTestCase {
}

func testConstant() {
let function: (Int) -> Int = f.constant(42)
let function: (Int) -> Int = f.pure(42)
function(0) ==! 42
function(1) ==! 42
function(42) ==! 42
Expand Down

0 comments on commit f3981e7

Please sign in to comment.