Skip to content

jrBordet/Caprice

Repository files navigation

Caprice

platforms

Capriccio is a type of composition characterized by a certain freedom of realization.

Caprice is a library build with the intention of explore the functional programming world without constraint.

Installation

CocoaPods

Add the following to your Podfile:

source 'https://github.com/jrBordet/Sources.git'
source 'https://cdn.cocoapods.org/'

target 'Target' do

    pod 'Caprice', '0.0.6'

end
pod install

Table of Contents

Operators

Optics

A Lens type and a bridge between the lens world and the Swift key path world.

    struct Book: Equatable {
        var id: Int
        var title: String

        var author: Author
    }

    struct Author: Equatable {
        var name: String
        var surname: String
    }

    extension Book {
        static var galacticGuideForHitchhikers = Book(id: 2, title: "galactic guide for hitchhikers", author: .adams)
    }

    extension Author {
        static var adams = Author(name: "Adams", surname: "Douglas")
    }

    let name = .galacticGuideForHitchhikers |> ^\Book.author.name
    
    let newBook = .galacticGuideForHitchhikers |> \Book.author.name *~ "Adams Noël"

Lens

get

    let authorName = .galacticGuideForHitchhikers |> ^\Book.author.name
    
    let authorName = .stoicism |> (lens(\Book.author) >>> lens(\Author.name)).get
    
    let authorName = .stoicism |> (lens(\Book.author.name)).get

set

    let result: Book = .it |> \Book.author.name *~ "new author"
    
    let newUser = lens(\User.id).set(0, user)

over

    let update =
        lens(\Book.author.name).over { $0.uppercased() }
            >>> lens(\Book.author.surname).over { $0.uppercased() }
            >>> lens(\Book.title) %~ { $0 +  " ♥️" }
    
    let newBook = Book.galacticGuideForHitchhikers |> update

Operators

map

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

filter

Filters out elements whose value match a predicates.

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

sort

Sorts the elements with respect to a Comparable property.

    let usersSorted = users.sorted(by: their(^\User.id, >))

Predicate

    let user = User(
        id: 0,
        email: "email@gmail.com"
    )

    let emailPredicate = Predicate<String> {
        $0 |> NSPredicate(format: "SELF MATCHES %@", "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}").evaluate(with:)
    }

    let userEmailCmp = emailPredicate.contramap(^\User.email)

    XCTAssertTrue(user.email |> emailPredicate.contains)
    XCTAssertTrue(user |> userEmailCmp.contains)

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published