Add more ergonomic ways to define folds and scans

Open
#3,125 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
38/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
haskell
Domain
backend

Research direction

Start by reading the fold and scan APIs, especially Fold.foldlM', and the inline example using MutArray in the issue. Work out how flipped fold and scan variants should accept a termination condition, then check the existing API tests for the expected behavior. Done means both folds and scans have ergonomic flipped forms with termination conditions.

Written by the indexing model from the issue text.

Description

Flipped versions of folds are better when we need to define the fold inline.

primes :: MonadIO m => Fold m Int (MutArray Int)
primes =
      flip Fold.foldlM' (pure MutArray.empty)
    $ \arr n -> do
        isPrime <-
            Stream.fold (Fold.all (\p -> n `mod` p /= 0))
                $ Stream.takeWhile (\p -> p * p <= n)
                $ MutArray.read arr
        if isPrime
        then MutArray.snoc arr n
        else pure arr

We can call the flipped version as foldWith. We can also add a termination condition, which makes it more like a familiar for loop.

The applies to scans as well.

Dominant language
Haskell
Stars
926
Forks
71
Avg merge
6h 49m
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from composewell/streamly

All issues in composewell/streamly

Similar issues

More Haskell issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.