Swift: Just about my favorite snippet of the week

From Developer Jeremy Tregunna

import Foundation

operator infix >>> { associativity left }
func >>><T, U>(lhs : T, rhs : T -> U) -> U {
    return rhs(lhs)
}

func say(str : String) -> String {
    return str
}

func makeSentence(str: String) -> String {
    return str.capitalizedString + "."
}

You run it like this:

"hello" >>> say >>> makeSentence

Give it a try in your favorite playground or repl. I love it!

One Comment

  • Shows the power of operator overloading, but also how Swift make it easier to have perfectly working unreadable code easily :). Ok, yes, too dramatic… this snippet can be read and understood in isolation, but in larger projects… this can be painful.