Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
23 lines (17 sloc) 932 Bytes
layout title date comments categories
post
Simple Elixir Pleasures
2014-08-15 20:48:47 -0400
true
elixir

You know that part of a blog where there's x number of comments and your job, as the programmer, is to properly label them with "comment" or "comments"? It's an annoying little bit of code, usually involving another if/then sequence. You can sorta shortcut it if you want to look cheap by saying "comment(s)" and saving yourself one check.

Bill C, in his Redditex repo, has a better solution, thanks to Elixir's pattern matching of functions:

    defp comment(0), do: ""
    defp comment(1), do: "(1 comment)"
    defp comment(n), do: "(#{n} comments)"

I love that. It's so clear and so clean looking to me.

Sigh. I'm turning into one of those one jerks who hates if/then/else.

You can’t perform that action at this time.