Elixir: generate a list of `n` occurrences of a gi

2020-04-02 18:44发布

问题:

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?

回答1:

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.



回答2:

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

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