Skip to content

Commit

Permalink
feat: add Option::as_iter (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
KKKIIO authored Jun 20, 2024
1 parent 1b3cdcf commit 5e05d93
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
33 changes: 33 additions & 0 deletions option/option.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,36 @@ pub fn unwrap[X](self : X?) -> X {
pub fn Option::default[X]() -> X? {
None
}

pub fn as_iter[T](self : T?) -> Iter[T] {
match self {
Some(v) => Iter::singleton(v)
None => Iter::empty()
}
}

test "as_iter" {
let x = Option::Some(42)
let exb = Buffer::make(0)
x.as_iter().iter(
fn(x) {
exb.write_string(x.to_string())
exb.write_char('\n')
},
)
exb.expect(
content=
#|42
#|
,
)?
exb.reset()
let y : Int? = None
y.as_iter().iter(
fn(x) {
exb.write_string(x.to_string())
exb.write_char('\n')
},
)
exb.expect(content="")?
}
1 change: 1 addition & 0 deletions option/option.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fn when[T](Bool, () -> T) -> T?

// Types and methods
impl Option {
as_iter[T](T?) -> Iter[T]
bind[T, U](T?, (T) -> U?) -> U?
compare[X : Compare + Eq](X?, X?) -> Int
default[X]() -> X?
Expand Down

0 comments on commit 5e05d93

Please sign in to comment.