'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 1:[1]

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"]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aplet123