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

First Post!

Sebb

Sebb

maybe I’m missing the point, but

def reverse_sentence(sentence) do
  sentence |> String.split() |> Enum.reverse() |> Enum.join(" ")
end

and if reverse is the point and you do not want to use stdlib

defp reverse([]), do: []
defp reverse([word | rest]), do: reverse(rest) ++ [word]

Also, you should check you code, as it is not correct.
Elixir has the great ExUnit and mix test.

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

wmnnd
Hey there, I’ve started a little blog series about building and deploying Elixir applications. Now I would like to share with you the fi...
New
rrrene
In this series, we take a look at the different ways to organize, structure and execute a good “flow” in our Elixir programs. The latest...
New
blackode
https://medium.com/blackode This article comprises of two biggest changes of ExUnit. Arguments in the failure report Running mix tes...
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
mudasobwa
Just blogged on the general approach to creating easily extendable applications with plugin support.
New
ragamuf
Does the world need another How to create a blog article? Maybe not. But then again, creating something out of nothing is what we love....
New
mssantosdev
This post is a guide on how Norba and I are building LiveMatch, a real-time app for soccer to follow multiple games in one place. https:...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
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 44265 214
New

We're in Beta

About us Mission Statement