Upgrade to Pro — share decks privately, control downloads, hide ads and more …

World of Swift operators

Dmitrii
January 31, 2017

World of Swift operators

This is all about how to work with operators in Swift, how to declare custom ones and the main thing, that we don’t have to be afraid to see them and to use them.

Dmitrii

January 31, 2017
Tweet

More Decks by Dmitrii

Other Decks in Programming

Transcript

  1. @objc protocol ObjectiveCProtocol { optional func someOptionalFunct() } class SomeProcessor:

    NSObject, ObjectiveCProtocol { func processSomething(inout something: [String: AnyObject]) { // some Objective-C like code something = [:] } override class func initialize() { // some awful stuff } } © Dmitrii Celpan, mobOS 2017
  2. extension Employee: Decodable { static func decode(json: JSON) -> Decoded<Employee>

    { return curry(Employee.init) <^> json <| "id" <*> json <| "name" <*> json <|? "email" <*> json <| ["department", "name"] <*> json <|| "projects" } } © Dmitrii Celpan, mobOS 2017
  3. !!! extension Employee: Decodable { static func decode(json: JSON) ->

    Decoded<Employee> { return curry(Employee.init) <^> json <| "id" <*> json <| "name" <*> json <|? "email" <*> json <| ["department", "name"] <*> json <|| "projects" } } © Dmitrii Celpan, mobOS 2017
  4. PREFIX OPERATOR prefix operator =/ prefix func =/ (string: String)

    -> String { return "!, " + string } print(=/"not sure") // "!, not sure" © Dmitrii Celpan, mobOS 2017
  5. POSTFIX OPERATOR postfix operator ^^ postfix func ^^ (string: String)

    -> String { return string + " !" } print("Pretty nice"^^) // "Pretty nice !" © Dmitrii Celpan, mobOS 2017
  6. SWIFT OPERATORS postfix operator .> postfix func .> <T>(value: T)

    { print(value) } "print me".> // "print me" © Dmitrii Celpan, mobOS 2017
  7. INFIX OPERATOR infix operator ?? : NilCoalescingPrecedence infix operator ..<

    : RangeFormationPrecedence infix operator ... : RangeFormationPrecedence infix operator + : AdditionPrecedence infix operator - : AdditionPrecedence infix operator * : MultiplicationPrecedence infix operator / : MultiplicationPrecedence [and many more] © Dmitrii Celpan, mobOS 2017
  8. INFIX OPERATOR infix operator ?? : NilCoalescingPrecedence public func ??

    <T>(optionalValue: T?, value: T) -> T { guard let unwrapedValue = optionalValue else { return value } return unwrapedValue } © Dmitrii Celpan, mobOS 2017
  9. PIPELINE OPERATOR infix operator |> : PipelineLeftPrecedence public func |>

    <T, U>(x: T, f: (T) -> U) -> U { return f(x) } infix operator <| : PipelineRightPrecedence public func <| <T, U>(f: (T) -> U, x: T) -> U { return f(x) } © Dmitrii Celpan, mobOS 2017
  10. PIPELINE OPERATOR let intToDouble: (Int) -> Double = { Double($0)

    } let doubleToString: (Double) -> String = { String($0) } let parameter = 3 let someString = doubleToString(intToDouble(parameter)) © Dmitrii Celpan, mobOS 2017
  11. PIPELINE OPERATOR let intToDouble: (Int) -> Double = { Double($0)

    } let doubleToString: (Double) -> String = { String($0) } let parameter = 3 //let someString = doubleToString(intToDouble(parameter)) let someString1 = parameter |> intToDouble |> doubleToString © Dmitrii Celpan, mobOS 2017
  12. FUNCTION COMPOSITION OPERATOR precedencegroup FunctionCompositionPrecedence { associativity: left higherThan: MultiplicationPrecedence

    } infix operator >>> : FunctionCompositionPrecedence public func >>> <A, B, C>(f: @escaping (A) -> B, g: @escaping (B) -> C) -> (A) -> C { return { x in g(f(x)) } } © Dmitrii Celpan, mobOS 2017
  13. FUNCTION COMPOSITION OPERATOR let managedObjectToModel: (NSManagedObject) -> Model let modelToDictionary:

    (Model) -> Dictionary let managedObjectToDictionary = managedObjectToModel >>> modelToDictionary © Dmitrii Celpan, mobOS 2017
  14. OPTIONAL+APPLICATIVE OPERATOR precedencegroup ApplicativePrecedence { associativity: left lowerThan: NilCoalescingPrecedence }

    infix operator <*> : ApplicativePrecedence public func <*> <T, U>(f: ((T) -> U)?, a: T?) -> U? { return f.flatMap { a.map($0) } } © Dmitrii Celpan, mobOS 2017
  15. OPTIONAL+APPLICATIVE OPERATOR let optionalValue: String? = "someString" let optionalFunction: ((String)

    -> Void)? = { print($0) } guard let value = optionalValue, let function = optionalFunction else { return } function(value) © Dmitrii Celpan, mobOS 2017
  16. OPTIONAL+APPLICATIVE OPERATOR let optionalValue: String? = "someString" let optionalFunction: ((String)

    -> Void)? = { print($0) } //guard let value = optionalValue, // let function = optionalFunction else { // return //} //function(value) optionalFunction <*> optionalValue © Dmitrii Celpan, mobOS 2017
  17. THINGS TO REMEMBER > Documentation is a must > Think

    twice let result = a &- b ||> c <^> d *> f © Dmitrii Celpan, mobOS 2017
  18. THINGS TO REMEMBER > Documentation is a must > Think

    twice > Avoid complicated symbols © Dmitrii Celpan, mobOS 2017
  19. THINGS TO REMEMBER > Documentation is a must > Think

    twice > Avoid complicated symbols > Don't overuse "?" © Dmitrii Celpan, mobOS 2017
  20. THINGS TO REMEMBER > Documentation is a must > Think

    twice > Avoid complicated symbols > Don't overuse "?" > Be closer to the community © Dmitrii Celpan, mobOS 2017
  21. > Custom Operators in Swift, http:// www.codingexplorer.com > Swift Operators,

    http://nshipster.com > Operators, Operators, Operators: Introducing Precedence, http://ericasadun.com > https://github.com/apple/swift-evolution/blob/ master/proposals/0077-operator-precedence.md © Dmitrii Celpan, mobOS 2017
  22. > Argo, Functional JSON parsing library > Runes, Infix operators

    for monadic functions > Prelude, µframework of simple functional programming tools > Kickstarter, Open Sourced iOS app © Dmitrii Celpan, mobOS 2017