815 ^ super reverseDo:aBlock |
815 ^ super reverseDo:aBlock |
816 ! |
816 ! |
817 |
817 |
818 traverse:aBlock |
818 traverse:aBlock |
819 "Evaluate aBlock for every element that is not an Array, |
819 "Evaluate aBlock for every element that is not an Array, |
820 and traverse Arrays. |
820 and recursively traverse Arrays. |
821 Implemented here to support better search for selectors in |
821 Implemented here to support better search for selectors in |
822 literal arrays - might be a good idea to move it up in the collection |
822 literal arrays - might be a good idea to move it up in the collection |
823 hierarchy, since this may be a useful method for other collections |
823 hierarchy, since this may be a useful method for other collections |
824 as well." |
824 as well." |
825 |
825 |
826 self do: [:el | |
826 self do: [:el | |
827 el isArray |
827 el isArray |
828 ifTrue: [el traverse: aBlock] |
828 ifTrue: [el traverse: aBlock] |
829 ifFalse: [aBlock value: el]] |
829 ifFalse: [aBlock value: el]] |
830 |
830 |
831 " |
831 " |
832 example: flattening an Array: |
832 example: flattening an Array: |
833 |
833 |
834 |s| |
834 |s| |
835 |
835 |
836 s := WriteStream on:Array new. |
836 s := WriteStream on:Array new. |
837 #(1 2 (3 (4 5 (6 7) 8) 9 10) 11 (12 (13)) 14) traverse:[:el | s nextPut:el]. |
837 #(1 2 (3 (4 5 (6 7) 8) 9 10) 11 (12 (13)) 14) traverse:[:el | s nextPut:el]. |
838 s contents |
838 s contents |
839 " |
839 " |
|
840 " |
|
841 example: deep search |
|
842 |
|
843 #(1 2 (3 (4 5 (6 7) 8) 9 10) 11 (12 (13)) 14) traverse:[:el | |
|
844 el == 10 ifTrue:[Transcript showCr:'found'] |
|
845 ] |
|
846 " |
|
847 |
|
848 "Modified: 26.3.1996 / 17:08:10 / cg" |
840 ! ! |
849 ! ! |
841 |
850 |
842 !Array methodsFor:'filling & replacing'! |
851 !Array methodsFor:'filling & replacing'! |
843 |
852 |
844 from:index1 to:index2 put:anObject |
853 from:index1 to:index2 put:anObject |