Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


That'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.


I'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'.


Actually, 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).


And 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.


That'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)


The 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...


Ah, 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: