# Is the a function that essentially does foldr but witha stopping condition?

**URL:** <https://discourse.julialang.org/t/is-the-a-function-that-essentially-does-foldr-but-witha-stopping-condition/36875>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 1, 2020, 9:11pm UTC](https://discourse.julialang.org/t/is-the-a-function-that-essentially-does-foldr-but-witha-stopping-condition/36875 "2020-04-01T21:11:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtaylor306](https://avatars.discourse-cdn.com/v4/letter/j/a9a28c/32.png) [@jtaylor306](https://discourse.julialang.org/u/jtaylor306)\
**Post date:** [April 1, 2020, 9:11pm UTC](https://discourse.julialang.org/t/is-the-a-function-that-essentially-does-foldr-but-witha-stopping-condition/36875/1 "2020-04-01T21:11:34Z")

</div>

What I essentially want to do is keep folding an array from left to right with an operator, and then stopping what I hit a total so some number X.

for example:  
foldl(+,[1,2,3,4,5,6],10)  
I expect the results to be 10, or true. Basically anything that lets me know that somewhere in my fold I found the number 10.

Is there anything function currently in Julia that operates like this?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 1, 2020, 9:30pm UTC](https://discourse.julialang.org/t/is-the-a-function-that-essentially-does-foldr-but-witha-stopping-condition/36875/2 "2020-04-01T21:30:26Z")

</div>

Sounds like your best option might just be a for loop. You could make `foldl_special` that does this with one pretty easily.

---

<div class="post-metadata">

**Author:** ![jtaylor306](https://avatars.discourse-cdn.com/v4/letter/j/a9a28c/32.png) [@jtaylor306](https://discourse.julialang.org/u/jtaylor306)\
**Post date:** [April 1, 2020, 9:37pm UTC](https://discourse.julialang.org/t/is-the-a-function-that-essentially-does-foldr-but-witha-stopping-condition/36875/3 "2020-04-01T21:37:17Z")

</div>

Yeah I can manage it on my own, I was just curious if anything had the functionality already built in.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 1, 2020, 9:46pm UTC](https://discourse.julialang.org/t/is-the-a-function-that-essentially-does-foldr-but-witha-stopping-condition/36875/4 "2020-04-01T21:46:17Z")

</div>

Not as far as I’m aware. I might be wrong though.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [April 2, 2020, 2:40am UTC](https://discourse.julialang.org/t/is-the-a-function-that-essentially-does-foldr-but-witha-stopping-condition/36875/5 "2020-04-02T02:40:17Z")

</div>

You can do

```julia
julia> foldl(right, Scan(+) |> ReduceIf(>=(10)), [1, 2, 2, 6, 7])
11

julia> foldl(right, Map(x -> @show x) |> Scan(+) |> Map(sum -> @show sum) |> ReduceIf(>=(10)), [1, 2, 2, 6, 7])
x = 1
sum = 1
x = 2
sum = 3
x = 2
sum = 5
x = 6
sum = 11
11

```

with [https://github.com/tkf/Transducers.jl](https://github.com/tkf/Transducers.jl)
