relude-0.7.0.0: Safe, performant, user-friendly and lightweight Haskell Standard Library

Copyright(c) 2018-2020 Kowainik
LicenseMIT
MaintainerKowainik <xrom.xkov@gmail.com>
StabilityExperimental
PortabilityPortable
Safe HaskellSafe
LanguageHaskell2010

Relude.Extra.Foldable

Description

Contains utility functions for working with tuples.

Since: 0.6.0.0

Synopsis

Documentation

foldlSC :: forall t b a. Foldable t => (b -> a -> Either b b) -> b -> t a -> b Source #

A left-associative fold that's tail-recursive but can still short-circuit. Returning a Left short-circuits and immediately returns the value inside. Returning a Right continues the fold as usual with the value inside.

>>> foldlSC (\acc x -> if x == 0 then Left 0 else Right $! acc * x) 1 [1..6]
720
>>> foldlSC (\acc x -> if x == 0 then Left 0 else Right $! acc * x) 1 (0:error "Short-circuiting should keep this from happening")
0

Since: 0.6.0.0