diff options
author | Jechol Lee <mr.jechol@gmail.com> | 2020-08-14 15:53:24 +0900 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2020-08-14 11:59:46 +0100 |
commit | 8e0cec5d29e1af956b9c4fce552b96319ad2a602 (patch) | |
tree | 8a372c4fc9d7ec8cb04eee156f6e6dfff8f640c2 | |
parent | 4943602689657203ba763d4f7107f2fd8048c864 (diff) | |
download | gleam_stdlib-8e0cec5d29e1af956b9c4fce552b96319ad2a602.tar.gz gleam_stdlib-8e0cec5d29e1af956b9c4fce552b96319ad2a602.zip |
Annotate type for iterator.gleam
-rw-r--r-- | src/gleam/bit_string.gleam | 6 | ||||
-rw-r--r-- | src/gleam/iterator.gleam | 28 |
2 files changed, 19 insertions, 15 deletions
diff --git a/src/gleam/bit_string.gleam b/src/gleam/bit_string.gleam index 5236cf8..38227b5 100644 --- a/src/gleam/bit_string.gleam +++ b/src/gleam/bit_string.gleam @@ -58,11 +58,7 @@ pub external fn int_from_u32(BitString) -> Result(Int, Nil) = pub fn is_utf8(bits: BitString) -> Bool { case bits { <<>> -> True - <<c:utf8, rest:binary>> -> { - // TODO: https://github.com/gleam-lang/gleam/issues/704 - let _ = c - is_utf8(rest) - } + <<_:utf8, rest:binary>> -> is_utf8(rest) _ -> False } } diff --git a/src/gleam/iterator.gleam b/src/gleam/iterator.gleam index b891156..6489aa4 100644 --- a/src/gleam/iterator.gleam +++ b/src/gleam/iterator.gleam @@ -28,7 +28,10 @@ pub type Step(element, accumulator) { } // Creating Iterators -fn do_unfold(initial, f) { +fn do_unfold( + initial: acc, + f: fn(acc) -> Step(element, acc), +) -> fn() -> Action(element) { fn() { case f(initial) { Next(x, acc) -> Continue(x, do_unfold(acc, f)) @@ -70,7 +73,7 @@ pub fn unfold( /// repeatedly. /// pub fn repeatedly(f: fn() -> element) -> Iterator(element) { - unfold(Nil, fn(acc) { Next(f(), acc) }) + unfold(Nil, fn(_) { Next(f(), Nil) }) } /// Create an iterator that returns the same value infinitely. @@ -104,7 +107,11 @@ pub fn from_list(list: List(element)) -> Iterator(element) { } // Consuming Iterators -fn do_fold(iterator, initial, f) { +fn do_fold( + iterator: fn() -> Action(e), + initial: acc, + f: fn(e, acc) -> acc, +) -> acc { case iterator() { Continue(element, iterator) -> do_fold(iterator, f(element, initial), f) Stop -> initial @@ -141,8 +148,8 @@ pub fn fold( /// you wish to trigger any side effects that would occur when evaluating /// the iterator. /// -pub fn run(iterator) -> Nil { - fold(iterator, Nil, fn(_, acc) { acc }) +pub fn run(iterator: Iterator(e)) -> Nil { + fold(iterator, Nil, fn(_, _) { Nil }) } /// Evaluate an iterator and return all the elements as a list. @@ -161,7 +168,7 @@ pub fn to_list(iterator: Iterator(element)) -> List(element) { |> list.reverse } -fn do_take(iterator, desired, acc) { +fn do_take(iterator: fn() -> Action(e), desired: Int, acc: List(e)) -> List(e) { case desired > 0 { True -> case iterator() { Continue( @@ -169,8 +176,10 @@ fn do_take(iterator, desired, acc) { iterator, ) -> do_take(iterator, desired - 1, [element, ..acc]) Stop -> acc + |> list.reverse } False -> acc + |> list.reverse } } @@ -190,10 +199,9 @@ fn do_take(iterator, desired, acc) { pub fn take(from iterator: Iterator(e), up_to desired: Int) -> List(e) { iterator.continuation |> do_take(desired, []) - |> list.reverse } -fn do_drop(iterator, desired) { +fn do_drop(iterator: fn() -> Action(e), desired: Int) -> fn() -> Action(e) { case desired > 0 { True -> case iterator() { Continue(_, iterator) -> do_drop(iterator, desired - 1) @@ -226,7 +234,7 @@ pub fn drop(from iterator: Iterator(e), up_to desired: Int) -> Iterator(e) { |> Iterator } -fn do_map(continuation, f) { +fn do_map(continuation: fn() -> Action(a), f: fn(a) -> b) -> fn() -> Action(b) { fn() { case continuation() { Continue(e, continuation) -> Continue(f(e), do_map(continuation, f)) @@ -254,7 +262,7 @@ pub fn map(over iterator: Iterator(a), with f: fn(a) -> b) -> Iterator(b) { |> Iterator } -fn do_filter(iterator, predicate) { +fn do_filter(iterator: fn() -> Action(e), predicate: fn(e) -> Bool) -> fn() -> Action(e) { fn() { case iterator() { Continue(e, iterator) -> case predicate(e) { |