# Allow foldr operation for Strings?

**URL:** https://discourse.julialang.org/t/allow-foldr-operation-for-strings/23343
**Category:** Performance
**Tags:** question, performance, strings
**Created:** [April 20, 2019, 12:09pm UTC](https://discourse.julialang.org/t/allow-foldr-operation-for-strings/23343 "2019-04-20T12:09:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)
#### Post date: [April 20, 2019, 12:09pm UTC](https://discourse.julialang.org/t/allow-foldr-operation-for-strings/23343/1 "2019-04-20T12:09:53Z")

</div>

Currently, if users want to achieve `foldr` on a String, they have to choose one from the alternatives below as their workaround:

1. `collect` the string and get a char array, and `foldr` among this array, causing redundant copy once.
2. `foldl` among a `reverse`d String, causing redundant copy once.
3. find the string’s `lastindex` and then get reversed indices of strings as a lazy stream, causing no redundant copy but extremely annoying and really tricky(also affect the maintainability).

I wonder if there’s another feasible solution without redundant copy.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 20, 2019, 12:29pm UTC](https://discourse.julialang.org/t/allow-foldr-operation-for-strings/23343/2 "2019-04-20T12:29:00Z")

</div>

Are you sure about this?

```julia
julia> foldr((x, y) -> (x * '|' * y), "abracadabra")
"a|b|r|a|c|a|d|a|b|r|a"

julia> VERSION
v"1.2.0-pre.0"

```

---

<div class="post-metadata">

### Author: ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)
#### Post date: [April 20, 2019, 4:36pm UTC](https://discourse.julialang.org/t/allow-foldr-operation-for-strings/23343/3 "2019-04-20T16:36:31Z")

</div>

If there’re any unicode chars…

```julia
julia> foldr("λ x.x", init=1) do each, prev
           prev
       end
ERROR: StringIndexError("λ x.x", 2)

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 20, 2019, 6:11pm UTC](https://discourse.julialang.org/t/allow-foldr-operation-for-strings/23343/4 "2019-04-20T18:11:04Z")

</div>

This is a bug:

[https://github.com/JuliaLang/julia/issues/31780](https://github.com/JuliaLang/julia/issues/31780)
