Skip to content

Instantly share code, notes, and snippets.

func addTwo(_ num: Int) -> Int {
return num + 2
}
addTwo(2) // 4
addTwo(3) // 5
func partialAdd(_ fixed: Int) -> (Int)->Int {
return { (num) -> Int in num + fixed}
}
let addTwo = partialAdd(2)
let addThree = partialAdd(3)
let addALot = partialAdd(284)
addTwo(3) // 5
addThree(5) // 8
addTwo(addThree(addTwo(1))) //8
func pipe(_ fns: (Int) -> (Int)...) -> (Int) -> (Int) {
return { (input) in fns.reduce(input, {value, fn in fn(value)})}
}
pipe(addOne, addOne)(1) // 3
pipe(addOne, addTwo, addOne)(1) // 5
func pipe<T>(_ fns: (T) -> (T)...) -> (T) -> (T) {
return { (input) in fns.reduce(input, {value, fn in fn(value)})}
}
func addPrefix(_ prefix: String) -> (String) -> (String) {
return {(inputStr) in "\(prefix) \(inputStr)" }
}
func addSuffix(_ suffix: String) -> (String) -> (String) {
return {(inputStr) in "\(inputStr), \(suffix)" }
pipe(
getToken,
makeRequest,
mapResponse,
sendReactiveSignal
)(requestObject)
module.exports = {
replaceParams,
composeURL,
...
}
const template = 'api/{id}/{action}'
const params = {id: 123, action: 'run'}
const endpoint = replaceParams(template, params) // api/123/run
const host = process.env.HOST
const uri = composeUrl(host, endpoint) // http://api.name.com/api/123/run
const requestOptions = { uri }
module.exports = {
method1,
method2,
...
}
function method1 () {}
function method2 () {}
// pipe :: [any -> any] -> any -> any
pipe(
executeFirst,
executeSecond
)(initialValue)