Packages

🛒 A small library of helper functions, that works as a companion of gleam_stdlib

Current section

Files

Jump to
gxyz src gxyz@list.erl
Raw

src/gxyz@list.erl

-module(gxyz@list).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gxyz/list.gleam").
-export([reject/2, filter_is/2, reject_is/2, filter_contains/2, reject_contains/2, filter_tap/3, reject_tap/3, filter_contains_tap/3, reject_contains_tap/3, with/2, map_with/3]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/gxyz/list.gleam", 10).
?DOC(
" removes elements from the list that don't satisfy the predicate\n"
" ```\n"
": let nums = [1, 2, 3, 2, 1]\n"
": assert list.reject(nums, fn(n) {n % 2 == 0} ) == [1, 3, 1]\n"
": assert list.reject(nums, fn(n) {n % 2 == 1} ) == [2, 2]\n"
" ```\n"
).
-spec reject(list(FAJ), fun((FAJ) -> boolean())) -> list(FAJ).
reject(L, Rejecting) ->
gleam@list:filter(L, fun(A) -> not Rejecting(A) end).
-file("src/gxyz/list.gleam", 20).
?DOC(
" returns elements equal to filter value\n"
" ```\n"
": let nums = [1, 2, 3, 2, 1]\n"
": assert list.filter_is(nums, 1) == [1, 1]\n"
": assert list.filter_is(nums, 4) == []\n"
" ```\n"
).
-spec filter_is(list(FAM), FAM) -> list(FAM).
filter_is(L, Filter) ->
gleam@list:filter(L, fun(A) -> A =:= Filter end).
-file("src/gxyz/list.gleam", 30).
?DOC(
" returns elements not equal to filter value\n"
" ```\n"
": let nums = [1, 2, 3, 2, 1]\n"
": assert list.reject_is(nums, 1) == [2, 3, 2]\n"
": assert list.reject_is(nums, 4) == nums\n"
" ```\n"
).
-spec reject_is(list(FAP), FAP) -> list(FAP).
reject_is(L, Filter) ->
reject(L, fun(A) -> A =:= Filter end).
-file("src/gxyz/list.gleam", 40).
?DOC(
" returns elements contained in the filter list\n"
" ```\n"
": let nums = [1, 2, 3, 2, 1]\n"
": assert list.filter_contains(nums, [3, 2]) == [2, 3, 2]\n"
": assert list.filter_contains(nums, []) == []\n"
" ```\n"
).
-spec filter_contains(list(FAS), list(FAS)) -> list(FAS).
filter_contains(L, Filter) ->
gleam@list:filter(
L,
fun(_capture) -> gleam@list:contains(Filter, _capture) end
).
-file("src/gxyz/list.gleam", 50).
?DOC(
" returns elements not contained in the filter list\n"
" ```\n"
": let nums = [1, 2, 3, 2, 1]\n"
": assert list.reject_contains(nums, [3, 2]) == [1, 1]\n"
": assert list.reject_contains(nums, []) == nums\n"
" ```\n"
).
-spec reject_contains(list(FAW), list(FAW)) -> list(FAW).
reject_contains(L, Filter) ->
reject(L, fun(_capture) -> gleam@list:contains(Filter, _capture) end).
-file("src/gxyz/list.gleam", 62).
?DOC(
" returns elements where the tapped value satisfies the predicate\n"
" ```\n"
": import gleam/pair\n"
": import gleam/string\n"
": let vals = [#(0, \"Aa\"), #(1, \"Bb\"), #(2, \"Cc\")]\n"
": assert list.filter_tap(vals, pair.first, fn(v) { v % 2 == 0 }) == [#(0, \"Aa\"), #(2, \"Cc\")]\n"
": assert list.filter_tap(vals, pair.second, string.starts_with(_, \"A\")) == [#(0, \"Aa\")]\n"
" ```\n"
).
-spec filter_tap(list(FBA), fun((FBA) -> FBC), fun((FBC) -> boolean())) -> list(FBA).
filter_tap(L, Tap, Filter) ->
_pipe = gleam@list:map(L, fun(Original) -> {Original, Tap(Original)} end),
_pipe@1 = gleam@list:filter(
_pipe,
fun(Elem) -> Filter(erlang:element(2, Elem)) end
),
gleam@list:map(_pipe@1, fun(Elem@1) -> erlang:element(1, Elem@1) end).
-file("src/gxyz/list.gleam", 76).
?DOC(
" returns elements where the tapped value does not satisfy the predicate\n"
" ```\n"
": import gleam/pair\n"
": import gleam/string\n"
": let vals = [#(0, \"Aa\"), #(1, \"Bb\"), #(2, \"Cc\")]\n"
": assert list.reject_tap(vals, pair.first, fn(v) { v % 2 == 0 }) == [#(1, \"Bb\")]\n"
": assert list.reject_tap(vals, pair.second, string.starts_with(_, \"A\")) == [#(1, \"Bb\"), #(2, \"Cc\")]\n"
" ```\n"
).
-spec reject_tap(list(FBE), fun((FBE) -> FBG), fun((FBG) -> boolean())) -> list(FBE).
reject_tap(L, Tap, Filter) ->
filter_tap(L, Tap, fun(B) -> not Filter(B) end).
-file("src/gxyz/list.gleam", 87).
?DOC(
" returns elements where the tapped value exists in the list\n"
" ```\n"
": import gleam/pair\n"
": let vals = [#(0, \"Aa\"), #(1, \"Bb\"), #(2, \"Cc\")]\n"
": assert list.filter_contains_tap(vals, pair.first, [0, 1]) == [#(0, \"Aa\"), #(1, \"Bb\")]\n"
": assert list.filter_contains_tap(vals, pair.first, [3]) == []\n"
" ```\n"
).
-spec filter_contains_tap(list(FBI), fun((FBI) -> FBK), list(FBK)) -> list(FBI).
filter_contains_tap(L, Tap, Contains) ->
filter_tap(
L,
Tap,
fun(_capture) -> gleam@list:contains(Contains, _capture) end
).
-file("src/gxyz/list.gleam", 102).
?DOC(
" returns elements where the tapped value does not exist in the list\n"
" ```\n"
": import gleam/pair\n"
": let vals = [#(0, \"Aa\"), #(1, \"Bb\"), #(2, \"Cc\")]\n"
": assert list.reject_contains_tap(vals, pair.first, [0, 1]) == [#(2, \"Cc\")]\n"
": assert list.reject_contains_tap(vals, pair.first, [3]) == vals\n"
" ```\n"
).
-spec reject_contains_tap(list(FBN), fun((FBN) -> FBP), list(FBP)) -> list(FBN).
reject_contains_tap(L, Tap, Contains) ->
reject_tap(
L,
Tap,
fun(_capture) -> gleam@list:contains(Contains, _capture) end
).
-file("src/gxyz/list.gleam", 115).
?DOC(
" returns a list of tuples containing the value in the second position\n"
" ```\n"
": let vals = [0, 1, 2]\n"
": assert list.with(vals, True) == [#(0, True), #(1, True), #(2, True)]\n"
" ```\n"
).
-spec with(list(FBS), FBU) -> list({FBS, FBU}).
with(L, Value) ->
gleam@list:map(L, fun(_capture) -> gleam@pair:new(_capture, Value) end).
-file("src/gxyz/list.gleam", 124).
?DOC(
" returns a list of tuples containing the value in the second position, mapping the first\n"
" ```\n"
": let vals = [0, 1, 2]\n"
": assert list.map_with(vals, True, fn(n) { n * 2 }) == [#(0, True), #(2, True), #(4, True)]\n"
" ```\n"
).
-spec map_with(list(FBW), FBY, fun((FBW) -> FBZ)) -> list({FBZ, FBY}).
map_with(L, Value, Fun) ->
_pipe = gleam@list:map(L, Fun),
gleam@list:map(_pipe, fun(_capture) -> gleam@pair:new(_capture, Value) end).