From 86180a19a7a975f1b89c726dfae3882609bb562c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Sperber?= Date: Mon, 22 Jul 2019 14:31:49 +0200 Subject: [PATCH] Added asCons() and option to IList. Added head and tail to Cons. --- lib/src/ilist.dart | 11 +++++++++++ test/ilist_test.dart | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/src/ilist.dart b/lib/src/ilist.dart index ecd425a..c764fae 100644 --- a/lib/src/ilist.dart +++ b/lib/src/ilist.dart @@ -8,6 +8,10 @@ abstract class IList extends TraversableOps with FunctorOps> get tailOption; + Option> get option => asCons(); + + Option> asCons(); + bool _isCons(); A _unsafeHead(); @@ -357,6 +361,11 @@ class Cons extends IList { @override Option get headOption => some(_head); @override Option> get tailOption => some(_tail); + + @override Option> asCons() => some(this); + + A get head => _head; + IList get tail => _tail; } class _Nil extends IList { @@ -368,6 +377,8 @@ class _Nil extends IList { @override Option get headOption => none(); @override Option> get tailOption => none(); + + @override Option> asCons() => none(); } IList nil() => new _Nil(); diff --git a/test/ilist_test.dart b/test/ilist_test.dart index 6ccb455..5a9aac9 100644 --- a/test/ilist_test.dart +++ b/test/ilist_test.dart @@ -157,4 +157,21 @@ void main() { })); }); + test("to option", () { + qc.check(forall(intILists, (dynamicL) { + final l = dynamicL as IList; + final empty = l.option.fold(() => true, (_) => false); + return empty == (l == nil()); + })); + }); + + test("asCons, head and tail", () { + int sumThroughAsCons(IList l) => l.asCons().fold(() => 0, (c) => c.head + sumThroughAsCons(c.tail)); + + qc.check(forall(intILists, (dynamicL) { + final l = dynamicL as IList; + return sumThroughAsCons(l) == l.concatenate(IntSumMi); + })); + }); + }