Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir: generate a list of `n` occurrences of a given argument (similar to Haskell's replicate)

I want a Elixir function to generate a list of n occurrences of an argument, similar to Haskell's replicate function:

Input: replicate 3 5

Output: [5,5,5]

Input: replicate 5 "aa"

Output: ["aa","aa","aa","aa","aa"]

Input: replicate 5 'a'

Output: "aaaaa"

I have made a function to "replicate" an Integer ntimes:

import String

def replicate(number, n)
  String.duplicate(to_string(number), n) 
  |> split("", trim: true) 
  |> Enum.map(fn n -> String.to_integer(n) end
end

But that's doesn't match the specification :( . Could you help me?

like image 779
Marcus Vinícius Monteiro Avatar asked Dec 27 '16 19:12

Marcus Vinícius Monteiro


2 Answers

def replicate(n, x), do: for _ <- 1..n, do: x

If you want the last case to return string then you should add definition for this type using guard, something like this:

def replicate(n, [x]) when is_integer(x), do: to_string(for _ <- 1..n, do: x)
def replicate(n, x), do: for _ <- 1..n, do: x

iex(1)> replicate 3, 5
[5, 5, 5]
iex(2)> replicate 3, 'a'
"aaa"

You can also use String.duplicate/2 and List.duplicate/2 as other suggested:

def replicate(n, x = [c]) when is_integer(c), do: String.duplicate(to_string(x), n)
def replicate(n, x), do: List.duplicate(x, n)

Also please note that Char list 'a' and String "a" are different things in Elixir, so ensure you understand this correctly.

And finally if this is not a home task then I'd suggest not to reinvent bicycle but directly use functions from String and List module, when possible.

like image 133
raacer Avatar answered Nov 19 '22 15:11

raacer


Building on previous answer, but avoiding the replicate(0, :foo) bug.

def replicate(n, x), do: for i <- 0..n, i > 0, do: x

like image 1
Zhivko Chobanov Avatar answered Nov 19 '22 13:11

Zhivko Chobanov