Created
April 29, 2017 19:14
-
-
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.
This file contains hidden or 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
// 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