Last active
August 29, 2015 14:27
-
-
Save alonecuzzo/5d51e6e0f5bd894384aa to your computer and use it in GitHub Desktop.
Curry Swift
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
//adding without currying | |
func add(a: Int, b:Int) -> Int { | |
return a + b | |
} | |
//when we use the swift curry form this is what is happening behind the scenes | |
func addCurry(a: Int) -> (Int -> Int) { | |
return { b in | |
return a + b | |
} | |
} | |
//adding with swift's curry short hand notation... this is functionally equivalent to addCurry | |
func addCurrySwiftForm(a: Int)(b: Int) -> Int { | |
return a + b | |
} | |
//logger | |
struct Logger { | |
enum Level: Printable { | |
case Debug, Warn, Error, None | |
var description: String { | |
switch self { | |
case .Debug: | |
return "Debug" | |
case .Warn: | |
return "Warn" | |
case .Error: | |
return "Error" | |
case .None: | |
return "None" | |
} | |
} | |
} | |
static func log(level: Level)(name: String)(message: String) -> String { | |
return "\(level.description):: \(name)- \(message)" | |
} | |
static let debug = Logger.log(.Debug) //function that returns a function that expects a "name" param | |
static let warn = Logger.log(.Warn) | |
static let error = Logger.log(.Error) | |
static let none = Logger.log(.None) | |
} | |
let sarahSaidDebugLogger = Logger.debug(name: "Sarah") //applies "name" param to the function | |
sarahSaidDebugLogger(message: "Business") //applies "message" param to the function returned from debug(name: String) | |
//the function doesn't get executed until this last parameter is passed in |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment