Created
April 9, 2018 03:42
-
-
Save godrm/7f5b8d024e866ab8aa6ef152da693862 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.swift | |
// HelloOperator | |
// | |
// Created by JK on 09/04/2018. | |
// Copyright © 2018 JK. All rights reserved. | |
// | |
import Foundation | |
precedencegroup MyLeftOperator { | |
associativity: left | |
higherThan: LogicalConjunctionPrecedence | |
} | |
infix operator ^^ : MyLeftOperator | |
func ^^ (lhs: String, rhs: Int) -> String { | |
return "\(lhs)-\(rhs)" | |
} | |
let jk = "JK" ^^ 5 | |
precedencegroup MyRightOperator { | |
associativity: right | |
higherThan: LogicalConjunctionPrecedence | |
} | |
infix operator ^^> : MyRightOperator | |
func ^^> (lhs: Int, rhs: (Int)->String) -> String { | |
return "\(rhs(lhs))" | |
} | |
let myClosure : (Int)->(String) = { value in return "Show me the money, \(value)" } | |
let crong = 5 ^^> myClosure | |
precedencegroup ForwardPipe { | |
associativity: left | |
higherThan: LogicalConjunctionPrecedence | |
} | |
infix operator |> : ForwardPipe | |
public func |> <A,Z>(lhs: A, rhs: (A) -> Z) -> Z { | |
return rhs(lhs) | |
} | |
public func |> <A,B,Z>(lhs: A, rhs: ((A, B) -> Z, B)) -> Z { | |
return rhs.0(lhs, rhs.1) | |
} | |
public func |> <A,B,C,Z>(lhs: A, rhs: (((A, B, C) -> Z), B, C)) -> Z { | |
return rhs.0(lhs, rhs.1, rhs.2) | |
} | |
public func filter<S : Sequence>(_ seq: S, _ function: (S.Element)->Bool)->[S.Element] { | |
return seq.filter(function) | |
} | |
public func map<S : Sequence>(_ seq: S, _ function: (S.Element)->S.Element )->[S.Element] { | |
return seq.map(function) | |
} | |
public func reduce<S : Sequence>(_ seq: S, _ initial : S.Element, _ function: (S.Element, S.Element)->S.Element )->S.Element { | |
return seq.reduce(initial, function) | |
} | |
let increment : (Int)->Int = { value in value + 10 } | |
var result1 = 5 |> increment |> increment |> increment | |
//increment(increment(increment(5))) | |
let isEven : (Int)->Bool = { $0 % 2 == 0 } | |
var result2 = [1,2,3,4,5] |> (filter, isEven) |> (map, { $0 * 2 }) |> (reduce, 0, {$0 + $1}) | |
//[1,2,3,4,5].filter(isEven) | |
//filter([1,2,3,4,5], isEven) | |
precedencegroup BackwardPipe { | |
associativity: right | |
higherThan: LogicalConjunctionPrecedence | |
} | |
infix operator <| : BackwardPipe | |
public func <| <A,Z>(lhs: (A)->Z, rhs: A) -> Z { | |
return lhs(rhs) | |
} | |
var result3 = increment <| 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment