Packages
gleam_stdlib
0.8.0
1.0.3
1.0.2
1.0.1
1.0.0
0.71.0
0.70.0
0.69.0
0.68.1
0.68.0
0.67.1
0.67.0
0.65.0
0.64.0
0.63.2
0.63.1
0.63.0
0.62.1
0.62.0
0.61.0
0.60.0
0.59.0
0.58.0
0.57.0
0.56.0
0.55.0
0.54.0
0.53.0
0.52.0
0.51.0
0.50.0
0.49.0
0.48.0
0.47.0
0.46.0
0.45.0
0.44.0
0.43.0
0.42.0
0.41.0
0.40.0
0.39.0
0.38.0
0.37.0
0.36.0
0.35.1
0.35.0
0.34.0
0.33.1
0.33.0
0.32.1
0.32.0
0.31.0
0.30.2
0.30.1
0.30.0
0.29.2
0.29.1
0.29.0
0.28.2
0.28.1
0.28.0
0.27.0
0.26.1
0.26.0
0.25.0
0.24.0
0.23.0
0.22.3
0.22.2
0.22.1
0.22.0
0.21.0
0.20.0
0.19.3
0.19.2
0.19.1
0.19.0
0.18.1
0.18.0
0.18.0-rc1
0.17.1
0.17.0
0.16.0
0.15.0
0.14.0
0.13.0
0.12.0
0.11.0
0.10.1
0.10.0
0.9.0
0.8.0
0.7.0
0.6.0
0.5.0
0.4.0
0.4.0-rc1
0.3.1
0.3.0
0.2.0
retired
A standard library for the Gleam programming language
Current section
Files
Jump to
Current section
Files
src/gleam_stdlib.erl
-module(gleam_stdlib).
-include_lib("eunit/include/eunit.hrl").
-export([should_equal/2, should_not_equal/2, should_be_true/1, should_be_false/1,
should_be_ok/1, should_be_error/1, atom_from_string/1,
atom_create_from_string/1, atom_to_string/1, map_get/2,
iodata_append/2, iodata_prepend/2, identity/1, decode_int/1,
decode_string/1, decode_bool/1, decode_float/1, decode_thunk/1, decode_atom/1,
decode_list/1, decode_field/2, decode_element/2, parse_int/1, parse_float/1, compare_strings/2,
string_contains/2]).
should_equal(Actual, Expected) -> ?assertEqual(Expected, Actual).
should_not_equal(Actual, Expected) -> ?assertNotEqual(Expected, Actual).
should_be_true(A) -> ?assert(A).
should_be_false(A) -> ?assertNot(A).
should_be_ok(A) -> ?assertMatch({ok, _}, A).
should_be_error(A) -> ?assertMatch({error, _}, A).
map_get(Map, Key) ->
case maps:find(Key, Map) of
error -> {error, nil};
OkFound -> OkFound
end.
atom_create_from_string(S) ->
binary_to_atom(S, utf8).
atom_to_string(S) ->
atom_to_binary(S, utf8).
atom_from_string(S) ->
try {ok, binary_to_existing_atom(S, utf8)} catch
error:badarg -> {error, atom_not_loaded}
end.
iodata_append(Iodata, String) -> [Iodata, String].
iodata_prepend(Iodata, String) -> [String, Iodata].
identity(X) -> X.
decode_error_msg(Type, Data) ->
{error, iolist_to_binary(io_lib:format("Expected ~s, got ~s", [Type, classify(Data)]))}.
classify(X) when is_atom(X) -> "an atom";
classify(X) when is_binary(X) -> "a binary";
classify(X) when is_integer(X) -> "an int";
classify(X) when is_float(X) -> "a float";
classify(X) when is_list(X) -> "a list";
classify(X) when is_boolean(X) -> "a bool";
classify(X) when is_function(X, 0) -> "a zero arity function";
classify(_) -> "some other type".
decode_atom(Data) when is_atom(Data) -> {ok, Data};
decode_atom(Data) -> decode_error_msg("an atom", Data).
decode_string(Data) when is_binary(Data) -> {ok, Data};
decode_string(Data) -> decode_error_msg("a string", Data).
decode_int(Data) when is_integer(Data) -> {ok, Data};
decode_int(Data) -> decode_error_msg("an int", Data).
decode_float(Data) when is_float(Data) -> {ok, Data};
decode_float(Data) -> decode_error_msg("a float", Data).
decode_bool(Data) when is_boolean(Data) -> {ok, Data};
decode_bool(Data) -> decode_error_msg("a bool", Data).
decode_thunk(Data) when is_function(Data, 0) -> {ok, Data};
decode_thunk(Data) -> decode_error_msg("a zero arity function", Data).
decode_list(Data) when is_list(Data) -> {ok, Data};
decode_list(Data) -> decode_error_msg("a list", Data).
decode_field(Data, Key) ->
case Data of
#{Key := Value} ->
{ok, Value};
_ ->
decode_error_msg(io_lib:format("a map with key `~p`", [Key]), Data)
end.
decode_element(Data, Position) when is_tuple(Data) ->
case catch element(Position + 1, Data) of
{'EXIT', _Reason} ->
Reason = io_lib:format("Expected a tuple of at least ~w size, got a tuple of ~w sise",
[Position + 1, tuple_size(Data)]),
{error, Reason};
Value ->
{ok, Value}
end;
decode_element(Data, _Position) -> decode_error_msg("a Tuple", Data).
parse_int(String) ->
case string:to_integer(binary:bin_to_list(String)) of
{Integer, []} ->
{ok, Integer};
_ ->
{error, nil}
end.
parse_float(String) ->
case string:to_float(binary:bin_to_list(String)) of
{Float, []} ->
{ok, Float};
_ ->
{error, nil}
end.
compare_strings(Lhs, Rhs) ->
if
Lhs == Rhs ->
eq;
Lhs < Rhs ->
lt;
true ->
gt
end.
string_contains(Haystack, Needle) ->
case string:find(Haystack, Needle) of
nomatch ->
false;
_ ->
true
end.