Skip to content

Commit

Permalink
Add list.filter_map.
Browse files Browse the repository at this point in the history
  • Loading branch information
smimram committed Jun 1, 2023
1 parent 0212613 commit 9ddd8c0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ New:
[Pico TTS](https://github.com/naggety/picotts) (#2934).
- Added `"metadata_url"` to the default list of exported metadata (#2946)
- Added log colors!
- Added `list.filter_map`.

Changed:

Expand Down
25 changes: 25 additions & 0 deletions src/libs/list.liq
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ def list.filter(~remove=fun(_)->(), p, l)
list.ind(l, [], fun(x, _, l) -> if p(x) then list.cons(x, l) else (remove(x):unit); l end)
end

# Map a function on a list (like `list.map`) excepting that the value is removed
# if the function returns `null`.
# @category List
# @param f Function called on every element of the list.
# @param l The list.
def list.filter_map(f, l)
def f(x, _, l)
y = f(x)
if null.defined(y) then
list.cons(null.get(y), l)
else
l
end
end
list.ind(l, [], f)
end

# Associate a value to a key in an association list. This functions raises
# `error.not_found` if no default value is specified.
# @category List
Expand All @@ -147,6 +164,14 @@ def list.assoc.filter(p, l)
list.filter(p, l)
end

# Map a function of every element of the associative list, removing the entry if
# the function returns `null`.
# @category List
def list.assoc.filter_map(f, l)
def f(kv) = f(fst(kv), snd(kv)) end
list.filter_map(f, l)
end

# Remove the first pair from an associative list.
# @category List
# @param key Key of pair to be removed.
Expand Down

0 comments on commit 9ddd8c0

Please sign in to comment.