4 ms·
> Though we notice we only greet for mornings and afternoons so always having to pass “Good morning” or “Good afternoon” becomes a bit annoying. There are solu
by kazinator 7y ago
> Though we notice we only greet for mornings and afternoons so always having to pass “Good morning” or “Good afternoon” becomes a bit annoying.
There are solutions for this specific example not involving currying, like making a simple convenience wrapper:
func greet(greeting, name string) string {
return fmt.Sprintf("%v %v", greeting, name)
}
func goodMorning(name string) string {
return greet("Good Morning", name)
}
Or just binding "Good morning" to a variable like gm and using that in repeated calls to greet: greet(gm, "This Person"); greet(gm, "That Person"). That is still reasonably ergonomic, and avoids repeating the greeting string.
- kitd 7y agoThe point is about composability of functions though. Your example does it statically. Currying and partial application allows it to be done dynamically within the running program.
- deleted 7y ago[deleted]
- kazinator 7y agoActually my example is based on partial application involving anonymous functions. It's just hand compiled. I manually lifted the lambda to the top level, and de-anonymized it with a name. Then I realized that the only captured parameter had a binding to a string literal "Good Morning", so I propagated that literal into the closure body, eliminating the variable, and that variable's environment. It ended up looking static due to the static literal, basically.