defined select:,reject:,collect: in Array

This commit is contained in:
hyunghwan.chung 2019-06-28 06:13:09 +00:00
parent 2d8277a046
commit 20b50c7bb6
3 changed files with 120 additions and 17 deletions

View File

@ -37,15 +37,15 @@
* #\e123 Error literal * #\e123 Error literal
* #\pB8000000 SmallPointer(smptr) literal * #\pB8000000 SmallPointer(smptr) literal
* #() Array * #() Array. Comma as element delimiter is optional
* #[] ByteArray * #[] ByteArray. Comma as element delimiter is optional
* #{} Dictionary - not supported yet * #{} Dictionary - not supported yet
The following are not literals. The following are not literals. The followings forms expressions.
* ##() Array expression * ##() Array expression. Comma required to delimit elements
* ##[] ByteArray expression * ##[] ByteArray expression. Comma required to delimit elements
* ##{} Dictionary expression * ##{} Dictionary expression. Comma required to delimit elements
* S%[] String literal with each character specified * S%[] String literal with each character specified
** S%{A B C '\n'} ** S%{A B C '\n'}

View File

@ -253,6 +253,105 @@ class(#pointer) Array(SequenceableCollection)
{ {
^(self = anArray) not ^(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 ^coll
} }
method select: aBlock method select: condition_block
{ {
| coll | | coll |
coll := self class new. coll := self class new.
// TODO: using at:put: here isn't really right. implement add: to be able to insert the assocication without // TODO: using at:put: here isn't really right. implement add: to be able to insert the assocication without
// creating another new association. // creating another new association.
//self associationsDo: [ :ass | if (aBlock value: ass value) { coll add: ass } ]. //self associationsDo: [ :ass | if (condition_block 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 at: (ass key) put: (ass value) } ].
^coll ^coll
} }
method do: aBlock method do: action_block
{ {
| bs i ass | | bs i ass |
bs := self.bucket size. bs := self.bucket size.
i := 0. i := 0.
while (i < bs) 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. i := i + 1.
}. }.
} }
method keysDo: aBlock method keysDo: action_block
{ {
| bs i ass | | bs i ass |
bs := self.bucket size. bs := self.bucket size.
i := 0. i := 0.
while (i < bs) 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. i := i + 1.
}. }.
} }
method keysAndValuesDo: aBlock method keysAndValuesDo: action_block
{ {
| bs i ass | | bs i ass |
bs := self.bucket size. bs := self.bucket size.
i := 0. i := 0.
while (i < bs) 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. i := i + 1.
}. }.
} }
method associationsDo: aBlock method associationsDo: action_block
{ {
| bs i ass | | bs i ass |
bs := self.bucket size. bs := self.bucket size.
i := 0. i := 0.
while (i < bs) 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. i := i + 1.
}. }.
} }

View File

@ -188,6 +188,10 @@ class MyObject(Object)
//2 timesRepeat: [ |k| k := B"ABC\xFF" copy. k dump. k at: 3 put: 16r39. k dump ]. //2 timesRepeat: [ |k| k := B"ABC\xFF" copy. k dump. k at: 3 put: 16r39. k dump ].
//B"" 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.
} }
} }