From 20b50c7bb656703280b0fcdeaf1d2f50b2a25416 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Fri, 28 Jun 2019 06:13:09 +0000 Subject: [PATCH] defined select:,reject:,collect: in Array --- moo/README.md | 12 ++-- moo/kernel/Collect.moo | 121 ++++++++++++++++++++++++++++++++++++---- moo/kernel/test-002.moo | 4 ++ 3 files changed, 120 insertions(+), 17 deletions(-) diff --git a/moo/README.md b/moo/README.md index 8ce7130..75b2e0f 100644 --- a/moo/README.md +++ b/moo/README.md @@ -37,15 +37,15 @@ * #\e123 Error literal * #\pB8000000 SmallPointer(smptr) literal -* #() Array -* #[] ByteArray +* #() Array. Comma as element delimiter is optional +* #[] ByteArray. Comma as element delimiter is optional * #{} Dictionary - not supported yet -The following are not literals. +The following are not literals. The followings forms expressions. -* ##() Array expression -* ##[] ByteArray expression -* ##{} Dictionary expression +* ##() Array expression. Comma required to delimit elements +* ##[] ByteArray expression. Comma required to delimit elements +* ##{} Dictionary expression. Comma required to delimit elements * S%[] String literal with each character specified ** S%{A B C '\n'} diff --git a/moo/kernel/Collect.moo b/moo/kernel/Collect.moo index 59d503e..0423b89 100644 --- a/moo/kernel/Collect.moo +++ b/moo/kernel/Collect.moo @@ -253,6 +253,105 @@ class(#pointer) Array(SequenceableCollection) { ^(self = anArray) not } + + method collect: condition_block + { + // The stock implementation needs to be overwritten + // as the Array class has no add: method which it requires. + + | cursize newarr i | + + cursize := self size. + newarr := self class new: cursize. + + i := 0. + while (i < cursize) + { + newarr at: i put: (condition_block value: (self at: i)). + i := i + 1. + }. + + ^newarr. + } + + method select: condition_block + { + // The stock implementation needs to be overwritten + // as the Array class has no add: method which it requires. + + | seltab cursize newarr newsize i | + + cursize := self size. + seltab := ByteArray new: cursize. + + newsize := 0. + i := 0. + while (i < cursize) + { + if (condition_block value: (self at: i)) + { + seltab at: i put: 1. + newsize := newsize + 1. + }. + i := i + 1. + }. + + newarr := self class new: newsize. + + newsize := 0. + i := 0. + while (i < cursize) + { + if ((seltab at: i) == 1) + { + newarr at: newsize put: (self at: i). + newsize := newsize + 1. + }. + i := i + 1. + }. + + ^newarr. + } + + method reject: condition_block + { + // The stock implementation needs to be overwritten + // as the Array class has no add: method which it requires. + + | seltab cursize newarr newsize i | + + cursize := self size. + seltab := ByteArray new: cursize. + + newsize := 0. + i := 0. + while (i < cursize) + { + ifnot (condition_block value: (self at: i)) + { + seltab at: i put: 1. + newsize := newsize + 1. + }. + i := i + 1. + }. + + newarr := self class new: newsize. + + newsize := 0. + i := 0. + while (i < cursize) + { + if ((seltab at: i) == 1) + { + newarr at: newsize put: (self at: i). + newsize := newsize + 1. + }. + i := i + 1. + }. + + ^newarr. + } + } // ------------------------------------------------------------------------------- @@ -1216,61 +1315,61 @@ class AssociativeCollection(Collection) ^coll } - method select: aBlock + method select: condition_block { | coll | coll := self class new. // TODO: using at:put: here isn't really right. implement add: to be able to insert the assocication without // creating another new association. - //self associationsDo: [ :ass | if (aBlock value: ass value) { coll add: ass } ]. - self associationsDo: [ :ass | if (aBlock value: ass value) { coll at: (ass key) put: (ass value) } ]. + //self associationsDo: [ :ass | if (condition_block value: ass value) { coll add: ass } ]. + self associationsDo: [ :ass | if (condition_block value: ass value) { coll at: (ass key) put: (ass value) } ]. ^coll } - method do: aBlock + method do: action_block { | bs i ass | bs := self.bucket size. i := 0. while (i < bs) { - if ((ass := self.bucket at: i) notNil) { aBlock value: ass value }. + if ((ass := self.bucket at: i) notNil) { action_block value: ass value }. i := i + 1. }. } - method keysDo: aBlock + method keysDo: action_block { | bs i ass | bs := self.bucket size. i := 0. while (i < bs) { - if ((ass := self.bucket at: i) notNil) { aBlock value: ass key }. + if ((ass := self.bucket at: i) notNil) { action_block value: ass key }. i := i + 1. }. } - method keysAndValuesDo: aBlock + method keysAndValuesDo: action_block { | bs i ass | bs := self.bucket size. i := 0. while (i < bs) { - if ((ass := self.bucket at: i) notNil) { aBlock value: ass key value: ass value }. + if ((ass := self.bucket at: i) notNil) { action_block value: ass key value: ass value }. i := i + 1. }. } - method associationsDo: aBlock + method associationsDo: action_block { | bs i ass | bs := self.bucket size. i := 0. while (i < bs) { - if ((ass := self.bucket at: i) notNil) { aBlock value: ass }. + if ((ass := self.bucket at: i) notNil) { action_block value: ass }. i := i + 1. }. } diff --git a/moo/kernel/test-002.moo b/moo/kernel/test-002.moo index 00b6965..14c3177 100644 --- a/moo/kernel/test-002.moo +++ b/moo/kernel/test-002.moo @@ -188,6 +188,10 @@ class MyObject(Object) //2 timesRepeat: [ |k| k := B"ABC\xFF" copy. k dump. k at: 3 put: 16r39. k dump ]. //B"" dump. +//##(#( 1 2 3 4 5 6 7 8 9 ) asOrderedCollection select: [:x | x > 5]) do: [:x | x dump ]. +//(#( 1 2 3 4 5 6 7 8 9 ) select: [:x | x > 5]) dump. +//(#( 1 2 3 4 5 6 7 8 9 ) collect: [:x | x * 2]) dump. +//(#( 1 2 3 4 5 6 7 8 9 ) reject: [:x | x > 5]) dump. } }