Skip to content

Instantly share code, notes, and snippets.

@pauljohanneskraft
Last active June 30, 2016 23:58
Show Gist options
  • Save pauljohanneskraft/0cbe04757fadd4bc695cda52e79b9a4c to your computer and use it in GitHub Desktop.
Save pauljohanneskraft/0cbe04757fadd4bc695cda52e79b9a4c to your computer and use it in GitHub Desktop.
A way to describe a function using a wrapper struct

Function

let addition = Function(call: { (c: Int, d: Int) -> Int in return c + d }, description: "$0 + $1")

print( addition )
// Prints "$0 + $1"

print( addition.call((5,3)) )
// Prints "8"

The Function wrapper-struct is a struct designed to make a specific function custom-string-convertible, a.k.a. printable.

struct ThrowingFunction<Operand, Result> : CustomStringConvertible {
var call : (Operand) throws -> Result
var description : String
}
struct Function<Operand, Result> : CustomStringConvertible {
var call : (Operand) -> Result
var description : String
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment