Skip to content

foldM is not tail recursive and crashes on large collections #70

Closed
@jutaro

Description

@jutaro

E.g. this implementation does not crash and is more efficient, but it has an additional type class constraint. I'm not aware of the implications of this constraint

foldM :: forall m a b. MonadRec m => (a -> b -> m a) -> a -> Array b -> m a
foldM func init array = tailRecM2 go init 0
  where
    go res index | index >= length array = pure (Right res)
    go res index                         = do
        res' <- func res (unsafePartial (unsafeIndex array index))
        pure $ Left {a:res', b:(index + 1)}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions