Skip to content

Commit

Permalink
Merge branch 'release/0.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Raphaël Bordet committed Aug 4, 2020
2 parents 032a95e + e73c540 commit a2a944d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
12 changes: 11 additions & 1 deletion Caprice/Lens.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public prefix func ^<Root, Value>(_ kp: KeyPath<Root, Value>) -> (Root) -> Value
get(kp)
}

// users.filter(by(^\User.id, 1))

public func by<Root, Value: Equatable>(
_ f: @escaping (Root) -> Value,
_ x: Value
) -> (Root) -> Bool {
return { $0 |> f == x }
}

public func their<Root, Value>(
_ f: @escaping (Root) -> Value,
_ g: @escaping (Value, Value) -> Bool
Expand All @@ -50,13 +59,14 @@ public func their<Root, Value>(

*/


public func their<Root, Value: Comparable>(
_ f: @escaping (Root) -> Value
) -> (Root, Root)-> Bool {
return their(f, <)
}

// _ = episodes.reduce(0, combining(^\.viewCount, by: +))

func combining<Root, Value>(
_ f: @escaping (Root) -> Value,
by g: @escaping (Value, Value) -> Value
Expand Down
2 changes: 1 addition & 1 deletion Caprice/PrecedenceGroups.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

precedencegroup ForwardApplication {
associativity: left
higherThan: AssignmentPrecedence
higherThan: AssignmentPrecedence, ComparisonPrecedence
}

precedencegroup EffectfulComposition {
Expand Down
36 changes: 27 additions & 9 deletions CapriceDemoTests/CapriceDemoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class AppThemeTests: XCTestCase {
XCTAssertTrue(users.filter(get(\.isStaff)).isEmpty)
}

func test_get() {
let id = user |> ^\.id

XCTAssertEqual(id, 1)
}

func test_map_composition() {
_ = users
.map(\.email)
Expand All @@ -69,16 +75,28 @@ class AppThemeTests: XCTestCase {
}
}

func test_filter() {
let userOne = users.filter { $0 |> ^\User.id == 1 }.first!

XCTAssertEqual(userOne, User(id: 1, email: "[email protected]"))

let authors = books.filter(by(^\.author.name, "Massimo"))

let surname = books
.filter(by(^\.author.name, "Massimo"))
.map(^\.author)
.map(^\.surname)
.reduce("", +)
.lowercased()

XCTAssertEqual(authors.first?.author, .max)
XCTAssertEqual(surname, "pigliucci")
}

func test_filter_composition_2() {
self.measure {

let result = users
.filter(get(\.isStaff) >>> { $0 == true })
// .filter(^\User.isStaff)
// .filter { $0.isStaff == false }

XCTAssertEqual(result.count, 0)
}
let r = users.filter(by(^\.id, 1)).first!

XCTAssertEqual(r, User(id: 1, email: "[email protected]"))
}

func test_sorting() {
Expand Down
27 changes: 27 additions & 0 deletions CapriceDemoTests/Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,33 @@ public struct User: Equatable {
}
}

let books: [Book] = [
.it,
.stoicism
]

public struct Book: Equatable {
let id: Int
let title: String

let author: Author
}

extension Book {
static var stoicism = Book(id: 0, title: "Come essere stoico", author: .max)
static var it = Book(id: 1, title: "IT", author: .king)
}

public struct Author: Equatable {
let name: String
let surname: String
}

extension Author {
static var king = Author(name: "Stephen", surname: "King")
static var max = Author(name: "Massimo", surname: "Pigliucci")
}

extension User {
var isStaff: Bool {
return self.email.hasSuffix("@syn.com")
Expand Down

0 comments on commit a2a944d

Please sign in to comment.