Skip to content

Commit

Permalink
Add support for Elixir List.duplicate/2
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Guyot <[email protected]>
  • Loading branch information
pguyot committed Sep 29, 2024
1 parent 4b37211 commit d74e3a2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ also non string parameters (e.g. `Enum.join([1, 2], ",")`
- Support for `code:ensure_loaded/1`
- Support for `io_lib:latin1_char_list/1`
- Add support to Elixir for `Keyword.split/2`
- Support for Elixir `List.duplicate/2`

### Changed

Expand Down
28 changes: 28 additions & 0 deletions libs/exavmlib/lib/List.ex
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,34 @@ defmodule List do

@compile :inline_list_funcs

@doc """
Duplicates the given element `n` times in a list.
`n` is an integer greater than or equal to `0`.
If `n` is `0`, an empty list is returned.
## Examples
iex> List.duplicate("hello", 0)
[]
iex> List.duplicate("hi", 1)
["hi"]
iex> List.duplicate("bye", 2)
["bye", "bye"]
iex> List.duplicate([1, 2], 3)
[[1, 2], [1, 2], [1, 2]]
"""
@spec duplicate(any, 0) :: []
@spec duplicate(elem, pos_integer) :: [elem, ...] when elem: var
def duplicate(elem, n) do
:lists.duplicate(n, elem)
end

@doc """
Deletes the given `element` from the `list`. Returns a new list without
the element.
Expand Down

0 comments on commit d74e3a2

Please sign in to comment.