Skip to content

Commit

Permalink
stdlib: rename has to includes, add missing Identity bounds
Browse files Browse the repository at this point in the history
works on #88
  • Loading branch information
soc committed Jun 1, 2024
1 parent 7dc6416 commit 16c6f0a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 29 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
20 changes: 12 additions & 8 deletions dora/stdlib/primitives.dora
Original file line number Diff line number Diff line change
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 @@ -514,6 +510,12 @@ impl[T: Identity] Identity for Option[T] {
@pub fun identicalTo(rhs: Option[T]): Bool = self === rhs;
}

impl[T: Identity] Option[T] {
@pub fun includes(val: T): Bool = if self
... is Some(actual) { actual.identicalTo(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 @@ -585,10 +587,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 @@ -634,6 +632,12 @@ impl[T: Identity, E] Identity for Result[T, E] {
@pub fun identicalTo(rhs: Result[T, E]): Bool = self === rhs;
}

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
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
fun main(): Unit {
let x = Array[Bool]::fill(3i64, true);
assert(x.has(true));
assert(x.includes(true));

let x = Array[Int32]::fill(3i64, 3i32);
assert(x.has(3i32));
assert(x.includes(3i32));

let x = Array[Int64]::fill(3i64, 3i64);
assert(x.has(3i64));
assert(x.includes(3i64));

let x = Array[Float64]::fill(3i64, 0.0/0.0);
assert(x.has(0.0/0.0));
assert(x.includes(0.0/0.0));
}
4 changes: 2 additions & 2 deletions tests/stdlib/option2.dora
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ fun main(): Unit {
let x = None[Int32];
assert(x.isNone());
assert(x.isSome().not());
assert(x.has(0i32).not());
assert(x.includes(0i32).not());
assert(x.contains(0i32).not());
assert(x.equals(None[Int32]));
assert(x.equals(Some[Int32](0i32)).not());
Expand All @@ -12,7 +12,7 @@ fun main(): Unit {
assert(x.isSome());
assert(x.isNone().not());
assert(x.getOrPanic() == 42i32);
assert(x.has(42i32));
assert(x.includes(42i32));
assert(x.contains(42i32));
assert(x.equals(Some[Int32](42i32)));
assert(x.equals(Some[Int32](2i32)).not());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ fun main(): Unit {
let er1 = Result[Int32, String]::Err("err1");
let nan = Result[Float64, String]::Ok(0.0/0.0);

assert(ok1.has(ok));
assert(ok1.has("ok").not());
assert(er1.has(23i32).not());
assert(nan.has(0.0/0.0));
assert(ok1.includes(ok));
assert(ok1.includes("ok").not());
assert(er1.includes(23i32).not());
assert(nan.includes(0.0/0.0));
}
14 changes: 7 additions & 7 deletions tests/string/string-indexOfFirst.dora
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ fun main(): Unit {
}

fun testBothEmpty(): Unit {
assert("".indexOfFirst("").has(0i64));
assert("".indexOfFirst("").includes(0i64));
}

fun testNeedleEmpty(): Unit {
assert("abc".indexOfFirst("").has(0i64));
assert("abc".indexOfFirst("").includes(0i64));
}

fun testHaystackEmpty(): Unit {
Expand All @@ -25,14 +25,14 @@ fun testNeedleLarger(): Unit {
}

fun testSuccess(): Unit {
assert("abcdef".indexOfFirst("abc").has(0i64));
assert("defabc".indexOfFirst("abc").has(3i64));
assert("ababbaadef".indexOfFirst("aa").has(5i64));
assert("apapplapple".indexOfFirst("apple").has(6i64));
assert("abcdef".indexOfFirst("abc").includes(0i64));
assert("defabc".indexOfFirst("abc").includes(3i64));
assert("ababbaadef".indexOfFirst("aa").includes(5i64));
assert("apapplapple".indexOfFirst("apple").includes(6i64));
}

fun testMultipleMatch(): Unit {
assert("abcdefabc".indexOfFirst("abc").has(0i64));
assert("abcdefabc".indexOfFirst("abc").includes(0i64));
}

fun testFailure(): Unit {
Expand Down

0 comments on commit 16c6f0a

Please sign in to comment.