From e8740a3284b3745bee5a7e14fd9bb98b1797e201 Mon Sep 17 00:00:00 2001 From: bchamagne Date: Thu, 4 Jul 2024 15:11:07 +0200 Subject: [PATCH] add List.set_at and List.delete_at --- .../interpreter/library/common/list.ex | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/archethic/contracts/interpreter/library/common/list.ex b/lib/archethic/contracts/interpreter/library/common/list.ex index 47d3c9efd..78451ef60 100644 --- a/lib/archethic/contracts/interpreter/library/common/list.ex +++ b/lib/archethic/contracts/interpreter/library/common/list.ex @@ -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)) && @@ -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