Browse files

Merge pull request #155 from dstnbrkr/sequence-as-list

Sequence as list
  • Loading branch information...
2 parents 6ee2f4a + 849d387 commit de40b6a9e4755ad839a5f9f338c09bd389fa656e @stevedekorte committed Oct 12, 2011
View
27 libs/iovm/source/IoSeq_immutable.c
@@ -688,6 +688,32 @@ IO_METHOD(IoSeq, asNumber)
return IONUMBER(d);
}
+IO_METHOD(IoSeq, asList)
+{
+ /*doc Sequence asList
+ Returns the receiver converted to a List containing all elements of the Sequence.
+ */
+
+ List *list = List_new();
+
+ size_t i;
+
+ for (i = 0; i < UArray_size(DATA(self)); i++)
+ {
+ if(UArray_isFloatType(DATA(self)))
+ {
+ List_append_(list, IONUMBER(UArray_doubleAt_(DATA(self), i)));
+ }
+ else
+ {
+ long c = UArray_longAt_(DATA(self), i);
+ List_append_(list, IOSYMBOL(&c));
+ }
+ }
+
+ return IoList_newWithList_(IOSTATE, list);
+}
+
IoList *IoSeq_whiteSpaceStrings(IoSeq *self, IoObject *locals, IoMessage *m)
{
/*doc Sequence whiteSpaceStrings
@@ -1910,6 +1936,7 @@ void IoSeq_addImmutableMethods(IoSeq *self)
{"asSymbol", IoSeq_asSymbol},
{"asString", IoSeq_asSymbol},
{"asNumber", IoSeq_asNumber},
+ {"asList", IoSeq_asList},
{"whiteSpaceStrings", IoSeq_whiteSpaceStrings},
{"print", IoSeq_print},
{"linePrint", IoSeq_linePrint},
View
1 libs/iovm/source/IoSeq_immutable.h
@@ -21,6 +21,7 @@ IOVM_API IO_METHOD(IoSeq, asSymbol);
IOVM_API IO_METHOD(IoSeq, isMutable);
IOVM_API IO_METHOD(IoSeq, isSymbol);
IOVM_API IO_METHOD(IoSeq, asNumber);
+IOVM_API IO_METHOD(IoSeq, asList);
IOVM_API IO_METHOD(IoSeq, whiteSpaceStrings);
View
9 libs/iovm/tests/correctness/SequenceTest.io
@@ -41,6 +41,15 @@ SequenceTest := UnitTest clone do(
assertSame(string, string asString)
)
+ testAsList := method(
+ assertEquals(list("a", "b", "c", "d"), "abcd" asList)
+ vector := Sequence clone setItemType("float32") setEncoding("number")
+ vector atPut(0, -0.5)
+ vector atPut(1, 0)
+ vector atPut(2, 0.5)
+ assertEquals(list(-0.5, 0, 0.5), vector asList)
+ )
+
testAsMutableSequence := method(
string := "blah"
assertEquals(Sequence type, string asMutable type)

0 comments on commit de40b6a

Please sign in to comment.