MarcinKasprowicz

MarcinKasprowicz

Blog Post: Elixir for JavaScript developers: first impressions

What I genuinely value at my workplace is that I can easily explore new languages through internal mobility. Throughout my career within Schibsted, I have been building things with JavaScript, TypeScript, Go, Kotlin, and recently Elixir…

#js

Most Liked

stevensonmt

stevensonmt

Nice article, but you might want to edit this:

Elixir, also known as Phoenix/LiveView, the most loved web framework

which is just not quite true. You probably meant something more like

Elixir, probably best known as the language behind Phoenix/LiveView, the most loved web framework in the StackOverflow 2022 survey …

Eiji

Eiji

Unfortunately that’s not true, but I like your dreams. :smiling_imp:

Enum was added to provide generic API for every Enumerable. If I’m not wrong some optimizations were even rejected on GitHub. Even if Enum would have every optimization there is still one extra call for Enumerable implementation. Since the post is for Elixir newbies we should avoid going this topic too long. :exploding_head:

In mentioned post I have added a comment with both reduce and fast implementations. If we want to do it really fast we should only use pattern matching and recursion, for example:

defmodule Example do
  # function head with default arguments as described in article
  # `?\s` or `?\ ` returns a codepoint of space
  def sample(string, separator \\ ?\s, word \\ "", acc \\ "")

  # we simply pattern match if
  # current input, word (characters joined so far) and acc (words joined so far)
  # are empty which is true only if
  # the whole string is empty or contains only separator characters
  def sample("", _separator, "", ""), do: ""

  # when we reached end of input string
  # the only thing left is to join last word with our accumulator
  # Note: acc is common naming and short version of accumulator
  def sample("", separator, word, acc), do: <<word::binary, separator::utf8, acc::binary>>

  # trimming goes here
  # we simply pattern match checking if next 2 characters are our separator
  # in such case the function calls itself with only one separator
  def sample(<<separator::utf8, separator::utf8, rest::binary>>, separator, word, acc) do
    sample(<<separator::utf8, rest::binary>>, separator, word, acc)
  end

  # pattern matching for last separator after recent word (see clasule above)
  # in this case we do not want to have trailing separator
  # so we change our empty acc to the first word
  def sample(<<separator::utf8, rest::binary>>, separator, word, "") do
    sample(rest, separator, "", word)
  end

  # same as above, but with non empty acc
  # notice we reset the word after we set/add it to acc
  def sample(<<separator::utf8, rest::binary>>, separator, word, acc) do
    sample(rest, separator, "", <<word::binary, separator::utf8, acc::binary>>)
  end

  # this simple function clasule collects all characters that are not separator
  # and adds it to word parameter
  def sample(<<char::utf8, rest::binary>>, separator, word, acc) do
    sample(rest, separator, <<word::binary, char::utf8>>, acc)
  end
end

Edit: Oh, for those newbies confused with too many solutions I would recommend to give benchee a try. With just few lines we can determine which solutions is faster.

Sebb

Sebb

:grin:

It will at least be magnitudes faster than the version provided in the article and my recursive version. Hopefully also significantly faster than the tail-recursive reverse2 … or is it?

I’ll look into those with Benchee as you suggested.

Where Next?

Popular in Blog Posts Top

JEG2
I’m closing out, for now, my series on questions at the heart of development with an analysis of when we need more abstraction. For exam...
New
marcelo
Long story short, over the past years we’ve been scaling our operation quite a lot at Unbabel. Most of our codebase is Python and some of...
New
New
brainlid
There is a new community resource available on writing “Safe Ecto Migrations”. When we get a migration wrong, it can lock up your product...
New
brainlid
On your LiveView page, you are using a custom component. You want to be able to pass HTML attributes into the component, but the componen...
New
AstonJ
Update: How to use the blogs section You can post in one of the Official Blog Posts threads (like this one), or, via Devtalk and a new t...
New
bemesa21
Sorting and deleting many-to-many associations made easy with Ecto’s :sort_param and :delete_param options from cast_assoc, powered by Li...
New
brainlid
Dialyzer is a tool that you’ve probably heard about in the Elixir community. You may have even used it. However, adding Dialyzer to an ex...
New
mssantosdev
Our take on how to build a frontend style guide with Phoenix Components, Atomic Design and plain CSS, with focus on reusability and code ...
New
davydog187
Hello all! I wrote a blog post called Elixir Observability: OpenTelemetry, Lightstep, Honeycomb. The post post covers how to integrate ...
New

Other popular topics Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New

We're in Beta

About us Mission Statement