Current section
Files
Jump to
Current section
Files
src/counter.erl
-module(counter).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new/0, insert/2, get/2, keys/1, values/1, elements/1, update/2, add/2, subtract/2, unique_size/1, total/1, to_list/1, most_common/1, most_common_n/2, from_list/1, to_dict/1, from_dict/1]).
-export_type([counter/1]).
-opaque counter(OPL) :: {counter, gleam@dict:dict(OPL, integer())}.
-file("/Users/josephlyons/Projects/Personal/gleam/counter/src/counter.gleam", 16).
-spec new() -> counter(any()).
new() ->
{counter, gleam@dict:new()}.
-file("/Users/josephlyons/Projects/Personal/gleam/counter/src/counter.gleam", 21).
-spec insert(counter(OPO), OPO) -> counter(OPO).
insert(Counter, Item) ->
_pipe = erlang:element(2, Counter),
_pipe@2 = gleam@dict:upsert(
_pipe,
Item,
fun(Count) ->
begin
_pipe@1 = Count,
gleam@option:unwrap(_pipe@1, 0)
end
+ 1
end
),
{counter, _pipe@2}.
-file("/Users/josephlyons/Projects/Personal/gleam/counter/src/counter.gleam", 28).
-spec get(counter(OPR), OPR) -> integer().
get(Counter, Item) ->
_pipe = erlang:element(2, Counter),
_pipe@1 = gleam@dict:get(_pipe, Item),
gleam@result:unwrap(_pipe@1, 0).
-file("/Users/josephlyons/Projects/Personal/gleam/counter/src/counter.gleam", 49).
-spec keys(counter(OPZ)) -> list(OPZ).
keys(Counter) ->
_pipe = erlang:element(2, Counter),
gleam@dict:keys(_pipe).
-file("/Users/josephlyons/Projects/Personal/gleam/counter/src/counter.gleam", 56).
-spec values(counter(any())) -> list(integer()).
values(Counter) ->
_pipe = erlang:element(2, Counter),
gleam@dict:values(_pipe).
-file("/Users/josephlyons/Projects/Personal/gleam/counter/src/counter.gleam", 72).
-spec prepend_repeated_item(OQI, integer(), list(OQI)) -> list(OQI).
prepend_repeated_item(Item, Times, Acc) ->
gleam@bool:guard(
Times =< 0,
Acc,
fun() -> prepend_repeated_item(Item, Times - 1, [Item | Acc]) end
).
-file("/Users/josephlyons/Projects/Personal/gleam/counter/src/counter.gleam", 64).
-spec elements(counter(OQF)) -> list(OQF).
elements(Counter) ->
_pipe = erlang:element(2, Counter),
_pipe@1 = maps:to_list(_pipe),
gleam@list:fold(
_pipe@1,
[],
fun(Items, Item) ->
prepend_repeated_item(
erlang:element(1, Item),
erlang:element(2, Item),
Items
)
end
).
-file("/Users/josephlyons/Projects/Personal/gleam/counter/src/counter.gleam", 78).
-spec update(counter(OQL), list(OQL)) -> counter(OQL).
update(Counter, Items) ->
gleam@list:fold(Items, Counter, fun(Counter@1, Item) -> _pipe = Counter@1,
insert(_pipe, Item) end).
-file("/Users/josephlyons/Projects/Personal/gleam/counter/src/counter.gleam", 84).
-spec add(counter(OQP), counter(OQP)) -> counter(OQP).
add(Counter_1, Counter_2) ->
_pipe = gleam@dict:combine(
erlang:element(2, Counter_1),
erlang:element(2, Counter_2),
fun gleam@int:add/2
),
{counter, _pipe}.
-file("/Users/josephlyons/Projects/Personal/gleam/counter/src/counter.gleam", 93).
-spec subtract(counter(OQT), counter(OQT)) -> counter(OQT).
subtract(Counter_1, Counter_2) ->
Dict_1 = erlang:element(2, Counter_1),
Dict_2 = erlang:element(2, Counter_2),
Dict_2_exclusive = begin
_pipe = Dict_2,
gleam@dict:drop(_pipe, gleam@dict:keys(Dict_1))
end,
Dict_2_overlap = begin
_pipe@1 = Dict_2,
gleam@dict:drop(_pipe@1, gleam@dict:keys(Dict_2_exclusive))
end,
_pipe@2 = Dict_1,
_pipe@3 = gleam@dict:combine(
_pipe@2,
Dict_2_overlap,
fun gleam@int:subtract/2
),
_pipe@4 = gleam@dict:filter(_pipe@3, fun(_, B) -> B > 0 end),
{counter, _pipe@4}.
-file("/Users/josephlyons/Projects/Personal/gleam/counter/src/counter.gleam", 107).
-spec unique_size(counter(any())) -> integer().
unique_size(Counter) ->
_pipe = erlang:element(2, Counter),
maps:size(_pipe).
-file("/Users/josephlyons/Projects/Personal/gleam/counter/src/counter.gleam", 112).
-spec total(counter(any())) -> integer().
total(Counter) ->
_pipe = erlang:element(2, Counter),
_pipe@1 = maps:to_list(_pipe),
_pipe@2 = gleam@list:map(_pipe@1, fun gleam@pair:second/1),
gleam@int:sum(_pipe@2).
-file("/Users/josephlyons/Projects/Personal/gleam/counter/src/counter.gleam", 122).
-spec to_list(counter(ORB)) -> list({ORB, integer()}).
to_list(Counter) ->
_pipe = erlang:element(2, Counter),
maps:to_list(_pipe).
-file("/Users/josephlyons/Projects/Personal/gleam/counter/src/counter.gleam", 34).
-spec most_common(counter(OPT)) -> list({OPT, integer()}).
most_common(Counter) ->
_pipe = Counter,
_pipe@1 = to_list(_pipe),
gleam@list:sort(
_pipe@1,
fun(A, B) ->
gleam@int:compare(erlang:element(2, B), erlang:element(2, A))
end
).
-file("/Users/josephlyons/Projects/Personal/gleam/counter/src/counter.gleam", 42).
-spec most_common_n(counter(OPW), integer()) -> list({OPW, integer()}).
most_common_n(Counter, N) ->
_pipe = Counter,
_pipe@1 = most_common(_pipe),
gleam@list:take(_pipe@1, N).
-file("/Users/josephlyons/Projects/Personal/gleam/counter/src/counter.gleam", 127).
-spec from_list(list(ORE)) -> counter(ORE).
from_list(Items) ->
_pipe = new(),
update(_pipe, Items).
-file("/Users/josephlyons/Projects/Personal/gleam/counter/src/counter.gleam", 132).
-spec to_dict(counter(ORH)) -> gleam@dict:dict(ORH, integer()).
to_dict(Counter) ->
erlang:element(2, Counter).
-file("/Users/josephlyons/Projects/Personal/gleam/counter/src/counter.gleam", 137).
-spec from_dict(gleam@dict:dict(ORL, integer())) -> counter(ORL).
from_dict(D) ->
_pipe = D,
{counter, _pipe}.