Rich_Morin
Formatting a list of strings - am I missing anything?
I recently wrote some Elixir to format a list of strings (eg, author names) for output. I think it’s reasonably idiomatic, but I probably missed a few tricks. Suggestions, anyone?
-r
It should work like this…
iex(0)> list_str []
""
iex(1)> list_str [1]
1
iex(2)> list_str [1,2]
"1 and 2"
iex(3)> list_str [1,2,3]
"1, 2, and 3"
And now, the code… (ducks)
@doc """
Join a list of strings into a (mostly) comma-delimited string.
"""
def list_str( [] ), do: ""
def list_str( [ one ] ), do: one
def list_str( [ one, two ] ), do: "#{ one } and #{ two }"
def list_str(inp_list) do
[ last | base_list ] = Enum.reverse(inp_list)
base_str = base_list
|> Enum.reverse()
|> Enum.join(", ")
"#{ base_str }, and #{ last }"
end
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 10 of 18 Posts
blatyo
Should probably be:
Otherwise, looks fine to me.
Rich_Morin
In case whatever is coming in isn’t already a string. Agreed, but is this any better than (say) “#{ one }”?
blatyo
They should be the same. I personally tend to use to_string instead of interpolation, when it’s only the value that I’m converting to a string. I’d use interpolation when I want to convert the values to a string and concatenate with something else.
Rich_Morin
That seems to be clearer, in terms of expressing intent, but in this case one could argue for staying in the same style as the following clauses. Dunno…
peerreynders
When it comes to serial commas the comma before the “and” is optional - it just can’t be there if you have less than three items.
So I guess recursion isn’t idiomatic?
Rich_Morin
Thank you for that tour de force, Peer. However, it doesn’t produce the requested result, so a picky client might complain and a picky test suite would certainly fail. Do you have a version that would pass?
As I’m sure you realize, omission of the Oxford comma can lead to ambiguity. To cite a well known example, “I’d like to thank my parents, God and Mother Teresa…” So, pedants like me don’t consider it to be optional. (Then again, I also use a lot of optional parentheses and spaces in my code…)
Leaving that issue aside, I wonder about the use of
<>. Is this versionactually better for some reason (e.g., faster) than this version?
Finally, although your recursive approach is most impressive, does it have any other benefits (aside from its stunning clarity and simplicity) that you can suggest? Inquiring gnomes need to mine…
peerreynders
Consistent version
Easy version
Haven’t looked at it too deeply - my gut-response is that it likely should be faster but that doesn’t preclude some smarty-pants compile time magic from proving me wrong and even if there is runtime overhead it could be minimal - though I wouldn’t count on it without verification.
Rich_Morin
EEx uses “some smarty-pants compile time magic” to turn Phoenix templates into sets of functions; this form of string interpolation doesn’t seem all that different to this newbie…
NobbZ
The second versions is syntactic sugar for the second.
LostKobrakai
There’s also cldr_lists, which can do that for you. Especially if you might need multiple languages I’d look into that.