Skip to content

Commit

Permalink
Feat: Added yielder support and marked iterators for deprecation.
Browse files Browse the repository at this point in the history
gleam/iterator has been marked for deprecation and suggests use of
gleam/yielder instead. Hence, this pr adds support for the same.
  • Loading branch information
Hiten Tandon authored and lunagl committed Dec 26, 2024
1 parent 85535b4 commit 467f38f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ links = []

[dependencies]
gleam_stdlib = "~> 0.37"
gleam_yielder = ">= 1.1.0 and < 2.0.0"

[dev-dependencies]
gleeunit = "~> 1.1"
6 changes: 4 additions & 2 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
# You typically do not need to edit this file

packages = [
{ name = "gleam_stdlib", version = "0.37.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "5398BD6C2ABA17338F676F42F404B9B7BABE1C8DC7380031ACB05BBE1BCF3742" },
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
{ name = "gleam_stdlib", version = "0.45.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "206FCE1A76974AECFC55AEBCD0217D59EDE4E408C016E2CFCCC8FF51278F186E" },
{ name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
]

[requirements]
gleam_stdlib = { version = "~> 0.37" }
gleam_yielder = { version = ">= 1.1.0 and < 2.0.0" }
gleeunit = { version = "~> 1.1" }
10 changes: 10 additions & 0 deletions src/glearray.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gleam/iterator.{type Iterator}
import gleam/yielder.{type Yielder}

/// Arrays are ordered sequences of elements, similar to lists.
///
Expand Down Expand Up @@ -224,10 +225,19 @@ fn do_insert(array: Array(a), index: Int, value: a) -> Array(a)
/// ["A", "B", "C"]
/// ```
///
@deprecated("Iterators have been deprecated, please use yield instead.")
pub fn iterate(array: Array(a)) -> Iterator(a) {
use index <- iterator.unfold(from: 0)
case get(array, index) {
Ok(element) -> iterator.Next(element, index + 1)
Error(_) -> iterator.Done
}
}

pub fn yield(array: Array(a)) -> Yielder(a) {
use index <- yielder.unfold(from: 0)
case get(array, index) {
Ok(element) -> yielder.Next(element, index + 1)
Error(_) -> yielder.Done
}
}
15 changes: 15 additions & 0 deletions test/glearray_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import gleam/function
import gleam/iterator
import gleam/list
import gleam/result
import gleam/yielder
import glearray.{type Array}
import gleeunit
import gleeunit/should
Expand Down Expand Up @@ -112,6 +113,20 @@ fn iterate(list: List(a)) {
|> should.equal(list)
}

pub fn yield_test() {
yield([])
yield([1, 2, 3, 4])
yield([0.4, 6.1, 99.9, -64.0])
}

fn yield(list: List(a)) {
list
|> glearray.from_list
|> glearray.yield
|> yielder.to_list
|> should.equal(list)
}

fn assert_empty(array: Array(a)) -> Array(a) {
assert_length(array, 0)
should.equal(array, glearray.new())
Expand Down

0 comments on commit 467f38f

Please sign in to comment.