Hacker News new | ask | show | jobs
by ironhaven 549 days ago
I have used fold for converting strings to a bitset in advent of code

    let string = "ewfsan";
    let bitset = string.bytes().fold(0u32 |acc, ch| acc | 1 << (ch - b'a'));
This is a idiom that I have used many times so this being more consice than a for loop is a plus

Of course if you have never seen a syntax before it will make less sense that anything you have seen before

1 comments

afair I've mostly only used fold when doing maths not covered by the standard sum or product. Fold is similar to map reduce but it's just one expression.