Skip to content

Commit a6b0423

Browse files
committed
Add support for Elixir List.duplicate/2
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
1 parent 389ec97 commit a6b0423

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ also non string parameters (e.g. `Enum.join([1, 2], ",")`
2929
- Add support to Elixir for `Keyword.split/2`
3030
- Support for `binary:split/3` and `string:find/2,3`
3131
- Support for large tuples (more than 255 elements) in external terms.
32+
- Support for Elixir `List.duplicate/2`
3233

3334
### Changed
3435

libs/exavmlib/lib/List.ex

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,34 @@ defmodule List do
147147

148148
@compile :inline_list_funcs
149149

150+
@doc """
151+
Duplicates the given element `n` times in a list.
152+
153+
`n` is an integer greater than or equal to `0`.
154+
155+
If `n` is `0`, an empty list is returned.
156+
157+
## Examples
158+
159+
iex> List.duplicate("hello", 0)
160+
[]
161+
162+
iex> List.duplicate("hi", 1)
163+
["hi"]
164+
165+
iex> List.duplicate("bye", 2)
166+
["bye", "bye"]
167+
168+
iex> List.duplicate([1, 2], 3)
169+
[[1, 2], [1, 2], [1, 2]]
170+
171+
"""
172+
@spec duplicate(any, 0) :: []
173+
@spec duplicate(elem, pos_integer) :: [elem, ...] when elem: var
174+
def duplicate(elem, n) do
175+
:lists.duplicate(n, elem)
176+
end
177+
150178
@doc """
151179
Deletes the given `element` from the `list`. Returns a new list without
152180
the element.

0 commit comments

Comments
 (0)