Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smart Contracts: Add List.sort_by/2,uniq/1,delete_at/2,set_at/2 #1538

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/archethic/contracts/interpreter/library/common/list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ defmodule Archethic.Contracts.Interpreter.Library.Common.List do
[element | list]
end

@spec sort_by(list(), String.t()) :: list()
def sort_by(list, key) do
Enum.sort_by(list, & &1[key])
end

@spec uniq(list()) :: list()
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 @@ -96,5 +114,24 @@ defmodule Archethic.Contracts.Interpreter.Library.Common.List do
AST.is_list?(first) || AST.is_variable_or_function_call?(first)
end

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

def check_types(:uniq, [first]) 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
Loading