eef64b9253
For now only with RDF.PropertyMap.add/2 on purpose, since we want to enforce a conscious usage of this feature, as put/2 would silently overwrite terms.
44 lines
1.1 KiB
Elixir
44 lines
1.1 KiB
Elixir
defmodule RDF.Utils do
|
|
@moduledoc false
|
|
|
|
def downcase?(term) when is_atom(term), do: term |> Atom.to_string() |> downcase?()
|
|
def downcase?(term), do: term =~ ~r/^(_|\p{Ll})/u
|
|
|
|
def lazy_map_update(map, key, init_fun, fun) do
|
|
case map do
|
|
%{^key => value} ->
|
|
Map.put(map, key, fun.(value))
|
|
|
|
%{} ->
|
|
Map.put(map, key, init_fun.())
|
|
|
|
other ->
|
|
:erlang.error({:badmap, other}, [map, key, init_fun, fun])
|
|
end
|
|
end
|
|
|
|
def map_while_ok(enum, fun) do
|
|
with {:ok, mapped} <-
|
|
Enum.reduce_while(enum, {:ok, []}, fn e, {:ok, acc} ->
|
|
with {:ok, value} <- fun.(e) do
|
|
{:cont, {:ok, [value | acc]}}
|
|
else
|
|
error -> {:halt, error}
|
|
end
|
|
end) do
|
|
{:ok, Enum.reverse(mapped)}
|
|
end
|
|
end
|
|
|
|
def flat_map_while_ok(enum, fun) do
|
|
with {:ok, mapped} <- map_while_ok(enum, fun) do
|
|
{:ok, Enum.concat(mapped)}
|
|
end
|
|
end
|
|
|
|
def map_join_while_ok(enum, joiner \\ "", fun) do
|
|
with {:ok, mapped} <- map_while_ok(enum, fun) do
|
|
{:ok, Enum.join(mapped, joiner)}
|
|
end
|
|
end
|
|
end
|