我需要bind
/ flatMap
/>>=
在Optional
Dhall 上。
我找不到它的实现并想出了我自己的。
let bindOptional
: ∀(a : Type) → ∀(b : Type) → (a → Optional b) → Optional a → Optional b
= λ(a : Type) →
λ(b : Type) →
λ(f : a → Optional b) →
λ(o : Optional a) →
Prelude.Optional.fold a o (Optional b) f (None b)
然后我按如下方式使用
bindOptional Outer Inner (λ(x : Outer) → x.inner) outer
中真的没有定义这样的功能Prelude
吗?我想我可能错过了。
此外:
有没有更惯用的方式来定义它?
是否有可能利用类型推断并缩短调用时间?就像是
bindOptional _.inner outer -- the compiler infers `Outer` from `outer` and `Inner` from `_.inner`
我试图不提供类型参数,但似乎不可能(从我对语言的有限了解)。