Naming of append/prepend

TL;DR: when do you mostly use String.append and List.append—inline or in chains of forward pipes? Does append or prepend make more sense in your head?

Because Elm “uses the convention that the data structure is always the last argument”, I often read functions as do something (onto/on/with/in) thisThing, e.g.: map someFunction onto thisList, or set thisKey to thisValue in thisDict, replace this with that in someString.

I’m sure this is far from novel, and is possible for (I think) the same sort of reasons that make chaining with forward pipes so readable (start with this list, then do this to it, then do this to it). But that’s exactly why I always find List.append and String.append strange. I always want to read it as append this to that, where I “should” be reading it as append this and that. But in my head, append should be called prepend. Does anybody else feel this way?

I guess it comes down to where List.append and String.append are most commonly used? If it’s mostly used inline, I can see why append [1, 2] [3, 4] makes sense. But if it’s mostly used in forward pipes (as I think I do), wouldn’t it make more sense to write the following?

    |> String.replace " " "-"
    |> String.toLower
    |> String.prepend "Username: "

P.S. I’ve tried to find out if anybody else has brought this up in Slack or Discourse and can’t find anything, so maybe it is just me!


I use forward pipes a lot but havn’t used append that much. But now that I think about this, I think you are right and prepend does feel like the correct name for the function and append just feels wrong.

Even inline I would probably prefer prepend [1, 2] [3, 4] now that I have gotten used to the notion that the thing you are working with is always last argument, and so what is being done here is really prepending, not appending.

Same discussion from 2016:


Ah, thanks for sharing that, the discussion makes a lot of sense.

I think appendTo and prependTo make a lot more sense, as they don’t come with the baggage of what prepend/append should mean. And I kind of think it would be worth the breaking change in the next big release. I also think I use the current append functions about as much as I do an anonymous function to do “prepend”.

Sometimes it is nice to write things without pipes:

"Username: " ++ String.toLower (String.replace " " "-" myString)

When I want my code to be easier to read, I like to make new top-level functions, or use let to name a couple values before I put them together. So I might write it like this:

viewName : String -> Html msg
viewName name =
  text ("Username: " ++ toUserName name)

toUserName : String -> String
toUserName str =
  String.toLower (String.replace " " "-" str)

I might also use String.concat if there are a bunch of things to put together. I learned ML-family languages that did not have (|>) though, so my style is influenced by that experience ¯\_(ツ)_/¯


This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.