Elixir vs Erlang

200 words, estimated reading time: 1 minute(s)

A friend of mine who is learning Elixir asked me how I would write a function that takes a length n and a char c to build a string consisting of n chars c.

Firing up iex and messing around with it brings me up to the conclusion - easy:

Stream.repeatedly(fn -> "-" end) |> Stream.take(10) |> Enum.join("")

Wow, so Clojure. Nothing wrong with that, but the said friend rated it ugly. Then I figured String.duplicate/2 exists:

String.duplicate("-", 10)

Which made me angry enough to look up String.duplicate/2 source:

  @spec duplicate(t, non_neg_integer) :: t
  def duplicate(subject, n) when is_integer(n) and n >= 0 do
    :binary.copy(subject, n)

Duh. All hail Erlang.

© 2019-2020 Adam Rutkowski.
All Rights Reserved, unless explicitly licensed otherwise.