-
Notifications
You must be signed in to change notification settings - Fork 105
Description
Hi,
I've read the docs here : https://github.com/plumatic/plumbing/tree/master/src/plumbing/fnk#fnk-syntax
and here : http://plumatic.github.io/plumbing/plumbing.fnk.schema.html
and I don't understand how to go with some destructuring scenarios.
Basically, there is "vanilla" destructuring (just bind a symbol to a key in a map) + 3 main extra features being :
- Make a binding optional by providing a fallback value
- Make a binding's type/schema explicit
- Rename a binding (give it another name than the key)
And I struggle to combine these extra features.
Scenario A : vanilla => destructuring a plain map
(letk [[foo] {:foo 42}] foo)
=> 42Result : ✅
Scenario B : renaming only => destructure the key named :foo and bind to bar
(letk [ [[:foo :as bar]] {:foo 42}] bar)
=> 42Result : ✅
Scenario C : fallback only => destructure a key named :bar and fallback to 0 when the key is missing
(letk [ [{bar 0}] {:foo 42}] bar)
=> 0Result : ✅
Scenario D : schema only => destructure a key named :foo and add a schema hint
(letk [[foo :- schema.core/Number] {:foo 42}] foo)
=> 42Result : ✅
Alright, using at most 1 extra feature worked.
Now let's try by combining 2 or 3 extra features.
Scenario E : schema + fallback => destructure a type-hinted missing key named :bar and fallback to 0
(letk [[{bar :- schema.core/Number 0}] {:foo 42}] bar)
=> 42Result : ✅
Where I'm stuck
However, I haven't found a way to combine :
- schema + renaming
- renaming + fallback
- schema + fallback + renaming
Are these combinations supported ? If not, what would it take to support them ?
PS : Classic let bindings support renaming + fallback, which I use often, hence the current issue :
(let [{baz :bar :or {baz 0}} {:foo 42}] baz)
=> 0