diff --git a/builtin/core.wren b/builtin/core.wren index 35b6db7a..a3722dd9 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 1938cd81..ea75e896 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]