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…

https://medium.com/schibsted-engineering/elixir-for-javascript-developers-first-impressions-72d87b6eb1ad

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

lawik
One of the Erlang ecosystem’s spiciest nerd snipes are hot code updates. Because it can do it. In ways that almost no other runtime can.
New
alexgaribay
I saw this thread so I decided to write about how we do it :smile:
New
brainlid
Building a LiveView powered chat app is easier than ever when using Streams! Sophie DeBenedetto shows us how in this article. She createa...
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
fredwu
Hi folks, I wrote a blog post the other day on how I built my MVP in 3 months whilst having a day job, using Elixir/Phoenix/LiveView. Th...
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
paulanthonywilson
All a bit meta, but this is a quick post on creating a Jekyll blog post from a Livebook page. Posted via Devtalk (see this thread for ...
New
DevotionGeo
There are 3 main formatters for Erlang which you can use from the command-line, rebar3_format, Steamroller elmfmt. Visual Studio Code’...
New
adolfont
Hi, You all probably know that José Valim is Brazilian and that his native language is Portuguese. I, Herminio Torres, Matheus Pesanha a...
New
lawik
Building on other people’s work I bashed things together and suddenly I can know when someone is speaking using Elixir and Membrane.
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement