Search code examples
haskelltextfunctional-programmingtext-processing

Map Text.append over a list but with the arguments from the list first in Haskell?


Basically, I have a list ["apple", "banana"] and I want to append "|4" to each argument in the list, so that I end up with ["apple|4", "banana|4"].

I can do map (Text.append "|4") ["apple", "banana"], but that appends in the wrong order, i.e. the result is ["|4apple", "|4banana"].

Is there a good way to tell Text.append to go in the other direction in this map?


Solution

  • There are a couple ways to do this. One way is to use flip:

    map (flip Text.append "|4") ["apple", "banana"]
    

    You could also explicitly use a lambda:

    map (\t -> Text.append t "|4") ["apple", "banana"]
    

    Or use the backtick infix operator syntax:

    map (`Text.append` "|4") ["apple", "banana"]