From b409569b6f8bdeeb31c24d311daff3cd084d5ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Fri, 27 Mar 2015 22:59:58 +0100 Subject: [PATCH] Added Sequence.list This helper method turns any sequence into a List. --- builtin/core.wren | 8 ++++++++ doc/site/core/sequence.markdown | 7 +++++++ src/vm/wren_core.c | 8 ++++++++ test/core/sequence/list.wren | 11 +++++++++++ 4 files changed, 34 insertions(+) create mode 100644 test/core/sequence/list.wren diff --git a/builtin/core.wren b/builtin/core.wren index 156f507f..bb8a7b6f 100644 --- a/builtin/core.wren +++ b/builtin/core.wren @@ -90,6 +90,14 @@ class Sequence { return result } + + list { + var result = new List + for (element in this) { + result.add(element) + } + return result + } } class String is Sequence {} diff --git a/doc/site/core/sequence.markdown b/doc/site/core/sequence.markdown index 526b3fd4..807e6c84 100644 --- a/doc/site/core/sequence.markdown +++ b/doc/site/core/sequence.markdown @@ -65,6 +65,13 @@ It is a runtime error if `sep` is not a string. Calls `join` with the empty string as the separator. +### **list** + +Creates a [list](list.html) containing all the elements in the sequence. + + :::dart + (1..3).list // [1, 2, 3] + ### **map**(transformation) Creates a new list by applying `transformation` to each element in the diff --git a/src/vm/wren_core.c b/src/vm/wren_core.c index fdd0cf6b..4595a294 100644 --- a/src/vm/wren_core.c +++ b/src/vm/wren_core.c @@ -136,6 +136,14 @@ static const char* libSource = "\n" " return result\n" " }\n" +"\n" +" list {\n" +" var result = new List\n" +" for (element in this) {\n" +" result.add(element)\n" +" }\n" +" return result\n" +" }\n" "}\n" "\n" "class String is Sequence {}\n" diff --git a/test/core/sequence/list.wren b/test/core/sequence/list.wren new file mode 100644 index 00000000..bc44784d --- /dev/null +++ b/test/core/sequence/list.wren @@ -0,0 +1,11 @@ +class TestSequence is Sequence { + iterate(iterator) { + if (iterator == null) return 1 + if (iterator == 3) return false + return iterator + 1 + } + + iteratorValue(iterator) { iterator } +} + +IO.print((new TestSequence).list) // expect: [1, 2, 3]