Skip to content

Instantly share code, notes, and snippets.

@antsmartian
Created October 17, 2015 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antsmartian/702e2e6c73036abdd0f5 to your computer and use it in GitHub Desktop.
Save antsmartian/702e2e6c73036abdd0f5 to your computer and use it in GitHub Desktop.
Composition In Swift
//: Playground - noun: a place where people can play
import Foundation
func getContents(url: String) -> String {
var a = url.componentsSeparatedByString(".")
var temp = ""
for (index, element) in enumerate(a) {
temp = "\(temp) \n \(element)";
}
return temp
}
func lines(input: String) -> [String] {
return input.componentsSeparatedByCharactersInSet(
NSCharacterSet.newlineCharacterSet())
}
let linesInURL = { url in count(lines(getContents(url))) }
linesInURL("a.n.t.o")
infix operator <<< { associativity left }
func <<< <A, B, C>(f: B -> C, g: A -> B) -> A -> C {
return { x in f(g(x)) }
}
let linesInURL2 = count <<< lines <<< getContents
linesInURL2("a.n.t.o")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment