[swift-users] Operator Overloading in Swift 3.0

Kwame Bryan kwame.bryan at gmail.com
Sat Nov 12 19:54:57 CST 2016


I'm currently updating some Swift 2.2 code to Swift 3.0. I had some
Overloaded Operators that needed converting. I've managed to get my code to
compile and run. Although, looking to the community for any constructive
criticism on my approach.

Regards

import UIKit

import Foundation


func lines(input:String) -> [String] {

    return input.components(separatedBy: NSCharacterSet.newlines)

}


func getContents(text:String) -> String {

    let a = text.components(separatedBy: ".")

    var temp = ""

    for (_,element) in a.enumerated(){

        temp = "Value: \(temp) \(element) \n"

    }

    return temp

}


precedencegroup FunctionalCompostion {

    associativity:left

}


infix operator <<< : FunctionalCompostion



func  <<<  <A,B,C> (g: @escaping (B) -> C, f: @escaping (A) -> B) -> (A) ->
C {

    return { x in g(f(x)) }

}


let functionalWay =  lines <<< getContents


functionalWay("K.w.a.m.e").count
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161112/0eeaea90/attachment.html>


More information about the swift-users mailing list