Skip to content

Commit

Permalink
stdlib: rename has to includes, add Identity bound where previo…
Browse files Browse the repository at this point in the history
…usly missing
  • Loading branch information
soc committed Dec 18, 2023
1 parent 6357f76 commit d32945c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
8 changes: 4 additions & 4 deletions dora/stdlib/collections.dora
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,11 @@ impl[T: Equals] Array[T] {
}

impl[T: Identity] Array[T] {
@pub fun has(value: T): Bool {
@pub fun includes(val: T): Bool {
var i = 0i64;

while i < self.size() {
if self.get(i).identicalTo(value) {
if self.get(i).identicalTo(val) {
return true;
}
i = i + 1i64;
Expand Down Expand Up @@ -1347,11 +1347,11 @@ impl[T: Equals] List[T] {
}

impl[T: Identity] List[T] {
@pub fun has(value: T): Bool {
@pub fun includes(val: T): Bool {
var i = 0i64;

while i < self.size() {
if self.get(i).identicalTo(value) {
if self.get(i).identicalTo(val) {
return true;
}
i = i + 1i64;
Expand Down
22 changes: 13 additions & 9 deletions dora/stdlib/primitives.dora
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std.traits.{Default, Equals, Hash, Iterator, Zero};
use std.traits.{Default, Equals, Hash, Identity, Iterator, Zero};
use std.string.Stringable;
use std.fatalError;

Expand Down Expand Up @@ -467,10 +467,6 @@ impl[T] Option[T] {
... is Some(value) { value }
... is None { alt };

@pub fun has(val: T): Bool = if self
... is Some(actual) { actual === val }
... is None { false };

@pub fun all(fct: (T): Bool): Bool = if self
... is Some(val) { fct(val) }
... is None { true };
Expand Down Expand Up @@ -510,6 +506,12 @@ impl[T] Option[Option[T]] {
... is None { None[T] };
}

impl[T: Equals] Option[T] {
@pub fun includes(val: T): Bool = if self
... is Some(actual) { actual === val }
... is None { false };
}

impl[T: Equals] Equals for Option[T] {
@pub fun equals(rhs: Option[T]): Bool {
if self.isSome() {
Expand Down Expand Up @@ -581,10 +583,6 @@ impl[T, E] Result[T, E] {
... is Ok(_) { fatalError("cannot unwrap Ok."); unreachable[E]() }
... is Err(value) { value };

@pub fun has(val: T): Bool = if self
... is Ok(actual) { actual === val }
... is Err(_) { false };

@pub fun all(fct: (T): Bool): Bool = if self
... is Ok(val) { fct(val) }
... is Err(_) { true };
Expand Down Expand Up @@ -626,6 +624,12 @@ impl[T, E] Result[T, E] {
... is Err(_) { None[T] };
}

impl[T: Identity, E] Result[T, E] {
@pub fun includes(val: T): Bool = if self
... is Ok(actual) { actual.identicalTo(val) }
... is Err(_) { false };
}

impl[T: Equals, E] Result[T, E] {
@pub fun contains(rhs: T): Bool = if self
... is Ok(val) { val.equals(rhs) }
Expand Down

0 comments on commit d32945c

Please sign in to comment.