You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
scanned currently consumes folds in a way that does not let them make use of the fact that they are always going to be applied to at least one element.
For example, attaching the maximum of all elements so far via scanned is slightly annoying. Most naturally, for s :: Stream (Of a) m r, one would use scanned like this
L.purely S.scanned L.maximum s ::Stream (Of (a, Maybea)) mr
The Maybe here is misleading; all values are actually Just. One can rectify this via e.g. S.mapMaybe sequence, but it would be better to avoid this.
Concretely, one can generalize scanned like this:
scanned'::Monadm=> (a->x) -> (x->a->x) -> (x->b) ->Stream (Ofa) mr->Stream (Of (a, b)) mr
scanned' begin step done = loop Nothingwhere
loop m stream =case stream ofReturn r ->return r
Effect mn ->Effect$fmap (loop m) mn
Step (a :> rest) ->case m ofNothing->dolet!x = begin a
!b = done x
S.yield (a, b)
loop (Just x) rest
Just x ->dolet!x' = step x a
!b = done x'
S.yield (a, b)
loop (Just x') rest
One can easily define scanned in terms of scanned', namely
scanned step begin done = scanned' (const begin) step done
Happy to see that there is interest to support Control.Foldl.NonEmpty.purely.
In case it is of interest for anyone:
At my company we have some old code that uses an NEStream type (non-empty stream). This type wraps the Stream type from this library. There are fold1 and fold1_ functions that can be used to fold a non-empty stream with L1.purely. This small Streaming.NonEmpty module contains the definitions of NEStream, fold1 and fold1_.
We typically obtain values of type NEStream as a result from calling group, which is defined in the same module.
Activity
Topsii commentedon Sep 10, 2025
Happy to see that there is interest to support
Control.Foldl.NonEmpty.purely.In case it is of interest for anyone:
At my company we have some old code that uses an
NEStreamtype (non-empty stream). This type wraps theStreamtype from this library. There arefold1andfold1_functions that can be used to fold a non-empty stream withL1.purely. This smallStreaming.NonEmptymodule contains the definitions ofNEStream,fold1andfold1_.We typically obtain values of type
NEStreamas a result from callinggroup, which is defined in the same module.