Skip to content

Commit

Permalink
Merge pull request #21 from spebbe/bjorns/ilist-ascons-option-head-tail
Browse files Browse the repository at this point in the history
Added asCons() and option to IList. Added head and tail to Cons.
  • Loading branch information
spebbe authored Aug 20, 2019
2 parents 88412ec + 86180a1 commit f0c56ae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/src/ilist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ abstract class IList<A> extends TraversableOps<IList, A> with FunctorOps<IList,

Option<IList<A>> get tailOption;

Option<IList<A>> get option => asCons();

Option<Cons<A>> asCons();

bool _isCons();

A _unsafeHead();
Expand Down Expand Up @@ -357,6 +361,11 @@ class Cons<A> extends IList<A> {
@override Option<A> get headOption => some(_head);

@override Option<IList<A>> get tailOption => some(_tail);

@override Option<Cons<A>> asCons() => some(this);

A get head => _head;
IList<A> get tail => _tail;
}

class _Nil<A> extends IList<A> {
Expand All @@ -368,6 +377,8 @@ class _Nil<A> extends IList<A> {
@override Option<A> get headOption => none();

@override Option<IList<A>> get tailOption => none();

@override Option<Cons<A>> asCons() => none();
}

IList<A> nil<A>() => new _Nil();
Expand Down
17 changes: 17 additions & 0 deletions test/ilist_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,21 @@ void main() {
}));
});

test("to option", () {
qc.check(forall(intILists, (dynamicL) {
final l = dynamicL as IList<int>;
final empty = l.option.fold(() => true, (_) => false);
return empty == (l == nil());
}));
});

test("asCons, head and tail", () {
int sumThroughAsCons(IList<int> l) => l.asCons().fold(() => 0, (c) => c.head + sumThroughAsCons(c.tail));

qc.check(forall(intILists, (dynamicL) {
final l = dynamicL as IList<int>;
return sumThroughAsCons(l) == l.concatenate(IntSumMi);
}));
});

}

0 comments on commit f0c56ae

Please sign in to comment.