diff options
Diffstat (limited to 'aoc-2020-gleam/src/util')
-rw-r--r-- | aoc-2020-gleam/src/util/cache.gleam | 20 | ||||
-rw-r--r-- | aoc-2020-gleam/src/util/dir.gleam | 2 | ||||
-rw-r--r-- | aoc-2020-gleam/src/util/graph.gleam | 15 | ||||
-rw-r--r-- | aoc-2020-gleam/src/util/input_util.gleam | 4 | ||||
-rw-r--r-- | aoc-2020-gleam/src/util/parser.gleam | 10 | ||||
-rw-r--r-- | aoc-2020-gleam/src/util/pos2.gleam | 2 | ||||
-rw-r--r-- | aoc-2020-gleam/src/util/pos3.gleam | 2 | ||||
-rw-r--r-- | aoc-2020-gleam/src/util/pos4.gleam | 2 |
8 files changed, 29 insertions, 28 deletions
diff --git a/aoc-2020-gleam/src/util/cache.gleam b/aoc-2020-gleam/src/util/cache.gleam index e1eefd2..c007d35 100644 --- a/aoc-2020-gleam/src/util/cache.gleam +++ b/aoc-2020-gleam/src/util/cache.gleam @@ -1,6 +1,6 @@ -import gleam/map.{Map} -import gleam/otp/actor.{Continue, Next, Stop} -import gleam/erlang/process.{Normal, Subject} +import gleam/dict.{type Dict} +import gleam/otp/actor.{type Next, Stop} +import gleam/erlang/process.{type Subject, Normal} const timeout = 1000 @@ -13,14 +13,18 @@ type Message(k, v) { type Server(k, v) = Subject(Message(k, v)) -fn handle_message(message: Message(k, v), dict: Map(k, v)) -> Next(Map(k, v)) { +fn handle_message( + message: Message(k, v), + map: Dict(k, v), +) -> Next(Message(k, v), Dict(k, v)) { case message { Shutdown -> Stop(Normal) Get(key, client) -> { - process.send(client, map.get(dict, key)) - Continue(dict) + process.send(client, dict.get(map, key)) + + actor.continue(map) } - Set(key, value) -> Continue(map.insert(dict, key, value)) + Set(key, value) -> actor.continue(dict.insert(map, key, value)) } } @@ -29,7 +33,7 @@ pub opaque type Cache(k, v) { } pub fn create(apply fun: fn(Cache(k, v)) -> t) -> t { - let assert Ok(server) = actor.start(map.new(), handle_message) + let assert Ok(server) = actor.start(dict.new(), handle_message) let result = fun(Cache(server)) process.send(server, Shutdown) result diff --git a/aoc-2020-gleam/src/util/dir.gleam b/aoc-2020-gleam/src/util/dir.gleam index 194d712..d445d86 100644 --- a/aoc-2020-gleam/src/util/dir.gleam +++ b/aoc-2020-gleam/src/util/dir.gleam @@ -2,7 +2,7 @@ import gleam/int import gleam/iterator as iter import ext/resultx as resx import ext/iteratorx as iterx -import util/pos2.{Pos2} +import util/pos2.{type Pos2} pub type Dir { North diff --git a/aoc-2020-gleam/src/util/graph.gleam b/aoc-2020-gleam/src/util/graph.gleam index d9aa5aa..5d02fff 100644 --- a/aoc-2020-gleam/src/util/graph.gleam +++ b/aoc-2020-gleam/src/util/graph.gleam @@ -1,6 +1,6 @@ import gleam/list -import gleam/iterator.{Iterator} as iter -import gleam/set.{Set} +import gleam/iterator.{type Iterator} as iter +import gleam/set.{type Set} fn dfs_helper( neighbours: fn(a) -> Iterator(a), @@ -14,7 +14,7 @@ fn dfs_helper( neighbours, stack: node |> neighbours - |> iter.filter(for: fn(n) { !set.contains(visited, n) }) + |> iter.filter(keeping: fn(n) { !set.contains(visited, n) }) |> iter.to_list |> list.append(stack), visited: visited @@ -26,10 +26,7 @@ fn dfs_helper( } pub fn dfs(from start: a, with neighbours: fn(a) -> Iterator(a)) -> Iterator(a) { - iter.from_list(dfs_helper( - neighbours, - stack: [start], - visited: set.new(), - acc: [], - )) + iter.from_list( + dfs_helper(neighbours, stack: [start], visited: set.new(), acc: []), + ) } diff --git a/aoc-2020-gleam/src/util/input_util.gleam b/aoc-2020-gleam/src/util/input_util.gleam index 27f3009..f4f6812 100644 --- a/aoc-2020-gleam/src/util/input_util.gleam +++ b/aoc-2020-gleam/src/util/input_util.gleam @@ -3,7 +3,7 @@ import gleam/list import gleam/bool import gleam/string as str import gleam/function as fun -import gleam/erlang/file +import simplifile as file import ext/resultx as resx pub fn read_text(filename: String) -> String { @@ -17,7 +17,7 @@ pub fn read_lines(filename: String) -> List(String) { |> read_text |> str.split(on: "\n") |> list.map(with: str.trim) - |> list.filter(for: fun.compose(str.is_empty, bool.negate)) + |> list.filter(keeping: fun.compose(str.is_empty, bool.negate)) } pub fn read_numbers(filename: String) -> List(Int) { diff --git a/aoc-2020-gleam/src/util/parser.gleam b/aoc-2020-gleam/src/util/parser.gleam index 1d4bba9..3b2e20e 100644 --- a/aoc-2020-gleam/src/util/parser.gleam +++ b/aoc-2020-gleam/src/util/parser.gleam @@ -5,7 +5,7 @@ import gleam/bool import gleam/string as str import gleam/result as res import gleam/function as fun -import gleam/option.{None, Option, Some} as opt +import gleam/option.{None, type Option, Some} // Heavily inspired by https://fsharpforfunandprofit.com/posts/understanding-parser-combinators/ @@ -188,19 +188,19 @@ pub fn then_3rd(two: Parser(#(a, b)), third: Parser(c)) -> Parser(#(a, b, c)) { }) } -pub fn or(first: Parser(a), else second: Parser(a)) -> Parser(a) { +pub fn or(first: Parser(a), otherwise second: Parser(a)) -> Parser(a) { create(fn(input) { first |> run(on: input) |> res.or(run(second, on: input)) }) - |> labeled(with: first.label <> " |> or(else: " <> second.label <> ")") + |> labeled(with: first.label <> " |> or(otherwise: " <> second.label <> ")") } pub fn opt(parser: Parser(a)) -> Parser(Option(a)) { parser |> map(with: Some) - |> or(else: succeeding(with: None)) + |> or(otherwise: succeeding(with: None)) |> labeled(with: "opt(" <> parser.label <> ")") } @@ -340,7 +340,7 @@ pub fn sep1(parser: Parser(a), by separator: Parser(b)) -> Parser(List(a)) { pub fn sep0(parser: Parser(a), by separator: Parser(b)) -> Parser(List(a)) { parser |> sep1(by: separator) - |> or(else: succeeding(with: [])) + |> or(otherwise: succeeding(with: [])) |> labeled( with: "sep0(" <> parser.label <> ", by: " <> separator.label <> ")", ) diff --git a/aoc-2020-gleam/src/util/pos2.gleam b/aoc-2020-gleam/src/util/pos2.gleam index 4de2ab9..0b6256c 100644 --- a/aoc-2020-gleam/src/util/pos2.gleam +++ b/aoc-2020-gleam/src/util/pos2.gleam @@ -1,7 +1,7 @@ import gleam/int import gleam/bool import gleam/list -import gleam/set.{Set} +import gleam/set.{type Set} pub type Pos2 = #(Int, Int) diff --git a/aoc-2020-gleam/src/util/pos3.gleam b/aoc-2020-gleam/src/util/pos3.gleam index 5525607..3787c19 100644 --- a/aoc-2020-gleam/src/util/pos3.gleam +++ b/aoc-2020-gleam/src/util/pos3.gleam @@ -1,6 +1,6 @@ import gleam/list import gleam/bool -import gleam/set.{Set} +import gleam/set.{type Set} pub type Pos3 = #(Int, Int, Int) diff --git a/aoc-2020-gleam/src/util/pos4.gleam b/aoc-2020-gleam/src/util/pos4.gleam index 3eda4c5..a96bcca 100644 --- a/aoc-2020-gleam/src/util/pos4.gleam +++ b/aoc-2020-gleam/src/util/pos4.gleam @@ -1,6 +1,6 @@ import gleam/list import gleam/bool -import gleam/set.{Set} +import gleam/set.{type Set} pub type Pos4 = #(Int, Int, Int, Int) |