Here's code that has a working "transduceList" variant that uses 
"List.foldl", but all of the List.reverse calls doesn't smell nice.  (Full 
context 
at 
https://github.com/marshallflax/lexical-elm/blob/master/src/BowlingScore.elm). 
 Any suggestions welcome!

transduceListL : Transducer a b (List b) s -> List a -> List b
transduceListL xform list =
    (transduce List.foldl (::) []) xform list |> List.reverse

partitionBy : (List a -> Bool) -> Transducer a (List a) r (List a)
partitionBy pred =
    { init =
        \reduce r -> ( [], r )
    , step =
        \reduce input ( hold, r ) ->
            let
                merged =
                    input :: hold
            in
                if (pred merged) then
                    ( [], reduce (List.reverse merged) r )
                else
                    ( merged, r )
    , complete =
        \reduce ( hold, r ) ->
            if (List.isEmpty hold) then
                r
            else
                reduce (List.reverse hold) r
    }

Thanks again!

Marshall



On Sunday, December 18, 2016 at 9:06:37 PM UTC-5, Marshall handheld Flax 
wrote:
>
> Suppose I have a *stateful* transducer
>
> partitionBy : (List a -> Bool) -> Transducer a (List a) r (List a)
> partitionBy pred =
>     { init =
>         \reduce r -> ( [], r )
>     , step =
>         \reduce input ( hold, r ) ->
>             let
>                 merged =
>                     input :: hold
>             in
>                 if (pred merged) then
>                     ( [], reduce merged r )
>                 else
>                     ( merged, r )
>     , complete =
>         \reduce ( hold, r ) ->
>             if (List.isEmpty hold) then
>                 r
>             else
>                 reduce hold r
>     }
>
>
> that "chunkfies" an input stream input into lists, where the supplid 
> predicate (List a -> Bool) determines when to start the next sublist.  (My 
> particular toy application is the standard "Bowling Kata" in which I need 
> to partition a stream of bowling throws into frames, but I think it could 
> be generally useful.)
>
> However, the "foldr" in the standard 
>
> transduceList : Transducer a b (List b) s -> List a -> List b
>
> transduceList =
>
>     transduce List.foldr (::) []
>
> means that the stateful processing occurs from the end, which is exactly 
> the opposite from what I need. (Since I need my state to be dependent upon 
> *earlier* messages, rather than *later* messages).
>
>
> Am I making too much of this (i.e. should just use transduceArray and be 
> done with it)?  Or is there something problematic about stateful 
> transducers, in that different "folder" can result in different semantics?
>
>
> Thanks!
>
> Marshall
>
>
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to