Skip to content

Latest commit

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date

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"}letlogRequest=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

varrequestResult:Int=0func randomRequest(url:String){letresult=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:

letcomposedRequest=Before(request).run({println("Request is fired!")})letloggedRequest=After(request).run(logger)

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

About

Simple playground for composing Swift functions.

Resources

Stars

9 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages