Skip to content

Commit

Permalink
Merge pull request #1288 from petermm/keyword.ex-split/2
Browse files Browse the repository at this point in the history
Add Keyword.ex split/2

Used by Supervisor.ex

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
  • Loading branch information
bettio committed Sep 29, 2024
2 parents 91ca775 + 7b2d5fa commit 4b37211
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 @@ -26,6 +26,7 @@ also non string parameters (e.g. `Enum.join([1, 2], ",")`
- Support for `gen_server:start_monitor/3,4`
- Support for `code:ensure_loaded/1`
- Support for `io_lib:latin1_char_list/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 4b37211

Please sign in to comment.