Allow foldr operation for Strings?

question
performance
strings
#1

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 reversed 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.

1 Like
#2

Are you sure about this?

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"
1 Like
#3

If there’re any unicode chars…

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

This is a bug:

4 Likes