Skip to content

Commit dc052ea

Browse files
committed
Merge pull request #1296 from pguyot/w39/add-support-for-List.duplicate/2
Add support for Elixir `List.duplicate/2` These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents 67acfac + a6b0423 commit dc052ea

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
@@ -32,6 +32,7 @@ also non string parameters (e.g. `Enum.join([1, 2], ",")`
3232
- Support for `io:put_chars/2`
3333
- Support for `lists:nthtail/2`
3434
- Support for Elixir `IO.chardata_to_string/1`
35+
- Support for Elixir `List.duplicate/2`
3536

3637
### Changed
3738

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)