rust-unofficial / patterns

A catalogue of Rust design patterns, anti-patterns and idioms
https://rust-unofficial.github.io/patterns/
Mozilla Public License 2.0
7.85k stars 354 forks source link

Fold pattern was confusing to me #54

Open ehuss opened 7 years ago

ehuss commented 7 years ago

The Fold pattern seemed confusing to me. The term "fold" in functional programming has a pretty clear definition (which includes a combining function), and it's not immediately clear from the example how it applies.

As used in the Rust compiler, it looks like their "Folders" are a general purpose map, reduce, or unfold operation on a tree, with the default operation of the base trait working as identity functions. Perhaps this is similar to Haskell's Traversable type class?

Anyways, I think it could be a little clearer. Perhaps in type theory or compiler circles using "Fold" in this context makes sense, but to me it seemed odd.

BTW, if you are curious, the term "fold" in a functional language sense was probably started by David Turner in the SASL language in the late 70's/early 80's.

nrc commented 7 years ago

Yeah, this is alluded to in the article, but we could do much better about being explicit about what fold in most functional languages means (especially as we have such a function on our iterators).

RalfJung commented 3 years ago

I feel like the current page causes more confusion that it helps -- that AST trait has functions of the form T -> T, so this is a map, not a fold. It looks like the compiler is using "fold" in what is at best a rather unconventional way; the pattern docs should probably just ignore what the compiler does and properly describe either map or fold (or both?).

simonsan commented 3 years ago

I feel like the current page causes more confusion that it helps -- that AST trait has functions of the form T -> T, so this is a map, not a fold. It looks like the compiler is using "fold" in what is at best a rather unconventional way; the pattern docs should probably just ignore what the compiler does and properly describe either map or fold (or both?).

Would be happy, if you make a PR for that to clear out this confusion. And add your knowledge to that article. ;)

pickfire commented 3 years ago

An additional note, there is doc aliases reduce and inject for fold. When I get started with rust, I was searching for reduce in docs but I can't find it, so now it will show it. https://github.com/rust-lang/rust/blob/master/library/core/src/iter/traits/iterator.rs#L2062-L2066

RalfJung commented 3 years ago

Would be happy, if you make a PR for that to clear out this confusion. And add your knowledge to that article. ;)

I'm afraid I won't have the time for that, sorry. I can offer to make a PR that just removes the page, to at least avoid the confusion, but I am not sure if that is a good idea.

taqtiqa-mark commented 2 years ago

RedDocMD posted a detailed description of the Fold pattern to the Rust users Discourse server.

Not sure if that is an acceptable skeleton for something to replace the current content?

oiwn commented 2 years ago

The Fold pattern seemed confusing to me. The term "fold" in functional programming has a pretty clear definition (which includes a combining function), and it's not immediately clear from the example how it applies.

your link not working since branch was renamed from master to main.

simonsan commented 1 year ago

your link not working since branch was renamed from master to main.

Updated the link.