Skip to content

Commit

Permalink
add List.set_at and List.delete_at
Browse files Browse the repository at this point in the history
  • Loading branch information
bchamagne committed Jul 4, 2024
1 parent c455a7d commit e8740a3
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/archethic/contracts/interpreter/library/common/list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ defmodule Archethic.Contracts.Interpreter.Library.Common.List do
defdelegate uniq(list),
to: Enum

@spec set_at(list(), integer(), any()) :: list()
def set_at(list, index, value) do
List.update_at(list, index, fn _ -> value end)
end

@spec delete_at(list(), integer()) :: list()
defdelegate delete_at(list, index),
to: List

@spec check_types(atom(), list()) :: boolean()
def check_types(:at, [first, second]) do
(AST.is_list?(first) || AST.is_variable_or_function_call?(first)) &&
Expand Down Expand Up @@ -114,5 +123,15 @@ defmodule Archethic.Contracts.Interpreter.Library.Common.List do
AST.is_list?(first) || AST.is_variable_or_function_call?(first)
end

def check_types(:delete_at, [first, second]) do
(AST.is_list?(first) || AST.is_variable_or_function_call?(first)) &&
(AST.is_number?(second) || AST.is_variable_or_function_call?(second))
end

def check_types(:set_at, [first, second, _third]) do
(AST.is_list?(first) || AST.is_variable_or_function_call?(first)) &&
(AST.is_number?(second) || AST.is_variable_or_function_call?(second))
end

def check_types(_, _), do: false
end

0 comments on commit e8740a3

Please sign in to comment.