Skip to content

atermenji/FunctionSwifter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FunctionSwifter is a simple playground for composing Swift functions.

It introduces a Functor class, which wraps a function and provides some basic operations to compose functions.

func logger(text: String) {
    println("LOG: \(text)")
}

loggerFunc = F(logger)
loggerFunc.run("hello")

Usage

Composing functions with '+' operator

func logger(text: String) {
    println("LOG: \(text)")
}

func request(url: String) -> String {
    return "Success 200"
}

let logRequest = F(request) + F(logger)
logRequest("http://some.awesome.url") // => "LOG: Success 200"

Repeating function calls

func greeting(firstName: String, lastName: String) {
    print("Hello, " + firstName + " " + lastName + "!")
}

F(greeting).repeat(("John", "Doe"), times: 3) // => Hello, John Doe! Hello, John Doe! Hello, John Doe!

Retrying function calls

var requestResult: Int = 0
func randomRequest(url: String) {
    let result = Int(arc4random_uniform(UInt32(2)))

    if result > 0 {
        requestResult = 200
    } else {
        requestResult = 404
    }
}

F(randomRequest).retry("http://some.awesome.url", maxTries: 5, condition: { requestResult == 200 })

Composing functions without '+' operator

This repo also includes Swifter class which provides a way to compose functions without using + operator:

let composedRequest = Before(request).run({ println("Request is fired!") })
let loggedRequest = After(request).run(logger)

You can find all the samples in main.swift file.

About

Simple playground for composing Swift functions.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages