Skip to content

Commit

Permalink
Add Keyword.ex split/2
Browse files Browse the repository at this point in the history
Used by Supervisor.ex

Signed-off-by: Peter M <petermm@gmail.com>
  • Loading branch information
petermm committed Sep 27, 2024
1 parent 1e5e1e9 commit 7b2d5fa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ also non string parameters (e.g. `Enum.join([1, 2], ",")`
- Support for Elixir `List.Chars` protocol
- Support for `gen_server:start_monitor/3,4`
- Support for `code:ensure_loaded/1`
- Add support to Elixir for `Keyword.split/2`

### Changed

Expand Down
15 changes: 14 additions & 1 deletion libs/exavmlib/lib/Keyword.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Copyright 2012-2024 Elixir Contributors
# https://github.com/elixir-lang/elixir/commits/v1.10.1/lib/elixir/lib/keyword.ex
#
# merge/2 take/2 pop/2/3 pop!/2 keyword?/1 has_key?/2 from:
# merge/2 take/2 pop/2/3 pop!/2 keyword?/1 has_key?/2 split/2 from:
# https://github.com/elixir-lang/elixir/blob/v1.16/lib/elixir/lib/keyword.ex
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -101,6 +101,19 @@ defmodule Keyword do
end
end

def split(keywords, keys) when is_list(keywords) and is_list(keys) do
fun = fn {k, v}, {take, drop} ->
case k in keys do
true -> {[{k, v} | take], drop}
false -> {take, [{k, v} | drop]}
end
end

acc = {[], []}
{take, drop} = :lists.foldl(fun, acc, keywords)
{:lists.reverse(take), :lists.reverse(drop)}
end

def take(keywords, keys) when is_list(keywords) and is_list(keys) do
:lists.filter(fn {k, _} -> :lists.member(k, keys) end, keywords)
end
Expand Down

0 comments on commit 7b2d5fa

Please sign in to comment.