3 ms·
But 'fold' isn't very useful over option types as there aren't really any non-trivial operations that you can perform on something that's either Nothing or Just
by foldr 7y ago
But 'fold' isn't very useful over option types as there aren't really any non-trivial operations that you can perform on something that's either Nothing or Just x. I think OP was probably thinking of folding over a list of option types, which you can do easily enough in Rust.
- lmm 7y agoThat's the opposite of my experience; fold is probably the main way I consume options in Scala. It's pretty much the only thing you ultimately want to do with an option, after you've finished transforming and composing.
- foldr 7y agoI'm really puzzled by this. Which Haskell 'fold' function would I ever want to apply to a Maybe type? I think there must be some kind of terminological variation in the use of 'fold' here. edit: Ah yes, I see that Scala chooses to use the name 'fold' for what is essentially the 'maybe' function in Haskell. I don't think there is any very useful generic notion of 'fold' that corresponds to this usage. (I see how the operation is a kind of fold, but the type signature of the function isn't flexible enough to be used for any foldable type.) So that is why Rust and Haskell don't name this operation 'fold'.
- shirogane86x 7y agoActually, in Haskell, given that Maybe is Foldable, you can technically implement the maybe function as maybe' def f value = foldr (const . f) def value Although in pratcice almost noone uses Maybe's foldable instance (and it also doesn't scale to other datatypes, like Either, nearly as well, because of the multiple type parameters).
- lmm 7y agoAh, indeed. That said I do occasionally use foldLeft, foldMap, and sum (which I think is what Haskell calls fold?) on options, so I'd say there are a handful of legitimate use cases for most kinds of folds.
- geofft 7y agoAnd here we see exactly why implementing a specific function on a specific instance of a monad isn't the answer - two users of two different, well-respected functional languages can't communicate.
- lmm 7y agoThat's completely backwards - the confusion here is precisely because the function is not implemented, if it was present in Rust (with a type signature) then there would have been no misunderstanding. (There was also a mistake in the original statement - fold is nothing to do with monads)
- foldr 7y agoThe function is implemented for option types in Rust, it's just not called 'fold', because there's no universal convention according to which functions with that signature are folds. https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or_else https://doc.rust-lang.org/std/option/enum.Option.html#method...