Skip to content

Instantly share code, notes, and snippets.

@wh1pch81n
Created April 29, 2017 19:14
Show Gist options
  • Save wh1pch81n/39dbde3bf0f98963001f028195560bfe to your computer and use it in GitHub Desktop.
Save wh1pch81n/39dbde3bf0f98963001f028195560bfe to your computer and use it in GitHub Desktop.
enums with associated types can enable polymorphic currying. May help with functional programming.
// Polymorphic currying
enum Bar {
case weight(lbs: CGFloat)
}
func foo(_ valueA: String, _ valueB: Int) -> (Bar) -> ()
{
return { _bar in
switch _bar {
case .weight(let lbs):
print(valueA, valueB, lbs)
}
}
}
func foo(_ valueA: String) -> (Bar) -> ()
{
return { _bar in
switch _bar {
case .weight(let lbs):
print(valueA, lbs)
}
}
}
do {
let m = foo("cat", 9)
m(Bar.weight(lbs: 6))
m(.weight(lbs: 88))
}
do {
let m = foo("cat")
m(Bar.weight(lbs: 6))
m(.weight(lbs: 88))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment