Recently I'm learning functional programing using swift programing language, and found a library named Swiftz, I found a very strange way to call function and would like to ask if anyone knows why it works, thanks!
Simple code here, Person().walk <*> 10
is too wired for me, before <*>
and after <*>
are whitespaces but no error occurred.
import Swiftz
class Person {
func walk(step: Int) {
print("walk \(step) step")
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Person().walk <*> 10
// ⬇️ normal way for same result
// Person().walk(step: 10)
}
}
// print result: walk 10 step
I try to found the <*>
code in Swiftz, but It's not clear for reason.
public func <*> <A, B>(f : ((A) -> B)?, a : A?) -> B? {
return f.flatMap { $0 <^> a }
}