/usr/share/gnu-smalltalk/kernel/Iterable.st is in gnu-smalltalk-common 3.2.5-1build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | "======================================================================
|
| Iterable Method Definitions
|
|
======================================================================"
"======================================================================
|
| Copyright 2008, 2009 Free Software Foundation, Inc.
| Written by Paolo Bonzini.
|
| This file is part of the GNU Smalltalk class library.
|
| The GNU Smalltalk class library is free software; you can redistribute it
| and/or modify it under the terms of the GNU Lesser General Public License
| as published by the Free Software Foundation; either version 2.1, or (at
| your option) any later version.
|
| The GNU Smalltalk class library is distributed in the hope that it will be
| useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
| General Public License for more details.
|
| You should have received a copy of the GNU Lesser General Public License
| along with the GNU Smalltalk class library; see the file COPYING.LIB.
| If not, write to the Free Software Foundation, 59 Temple Place - Suite
| 330, Boston, MA 02110-1301, USA.
|
======================================================================"
Object subclass: Iterable [
<category: 'Collections'>
<comment: 'I am an abstract class. My instances are collections of objects
that can be iterated. The details on how they can be mutated (if at
all possible) are left to the subclasses.'>
Iterable class >> isUnicode [
"Answer true; the receiver is able to store arbitrary
Unicode characters."
<category: 'multibyte encodings'>
self subclassResponsibility
]
, anIterable [
"Answer an iterable that enumerates first the elements of the receiver
and then the elements of anIterable."
<category: 'enumeration'>
self subclassResponsibility
]
ifNil: nilBlock ifNotNilDo: iterableBlock [
"Evaluate nilBlock if the receiver is nil, else evaluate
iterableBlock with each element of the receiver (which
should be an Iterable)."
<category: 'iteration'>
self do: iterableBlock
]
ifNotNilDo: iterableBlock [
"Evaluate iterableBlock with each element of the receiver (which
should be an Iterable) if not nil. Else answer nil"
<category: 'iteration'>
self do: iterableBlock
]
ifNotNilDo: iterableBlock ifNil: nilBlock [
"Evaluate nilBlock if the receiver is nil, else evaluate
iterableBlock, passing each element of the receiver (which
should be an Iterable)."
<category: 'iteration'>
self do: iterableBlock
]
do: aBlock [
"Enumerate each object of the receiver, passing them to aBlock"
<category: 'enumeration'>
self subclassResponsibility
]
do: aBlock separatedBy: separatorBlock [
"Enumerate each object of the receiver, passing them to aBlock.
Between every two invocations of aBlock, invoke separatorBlock"
<category: 'enumeration'>
| first |
first := true.
self do:
[:each |
first ifTrue: [first := false] ifFalse: [separatorBlock value].
aBlock value: each]
]
select: aBlock [
"Answer a new instance of a Collection containing all the elements
in the receiver which, when passed to aBlock, answer true"
<category: 'enumeration'>
self subclassResponsibility
]
reject: aBlock [
"Answer a new instance of a Collection containing all the elements
in the receiver which, when passed to aBlock, don't answer true"
<category: 'enumeration'>
self subclassResponsibility
]
collect: aBlock [
"Answer a new instance of a Collection containing all the results
of evaluating aBlock passing each of the receiver's elements"
<category: 'enumeration'>
self subclassResponsibility
]
detect: aBlock ifNone: exceptionBlock [
"Search the receiver for an element for which aBlock returns true.
If some does, answer it. If none does, answer the result of evaluating
aBlock"
<category: 'enumeration'>
self do: [:element | (aBlock value: element) ifTrue: [^element]].
^exceptionBlock value
]
count: aBlock [
"Count the elements of the receiver for which aBlock returns true,
and return their number."
<category: 'enumeration'>
| count |
count := 0.
self do: [:element | (aBlock value: element) ifTrue: [count := count + 1]].
^count
]
allSatisfy: aBlock [
"Search the receiver for an element for which aBlock returns false.
Answer true if none does, false otherwise."
<category: 'enumeration'>
self do: [:element | (aBlock value: element) ifFalse: [^false]].
^true
]
noneSatisfy: aBlock [
"Search the receiver for an element for which aBlock returns true.
Answer true if none does, false otherwise."
<category: 'enumeration'>
self do: [:element | (aBlock value: element) ifTrue: [^false]].
^true
]
anySatisfy: aBlock [
"Search the receiver for an element for which aBlock returns true.
Answer true if some does, false otherwise."
<category: 'enumeration'>
self do: [:element | (aBlock value: element) ifTrue: [^true]].
^false
]
conform: aBlock [
"Search the receiver for an element for which aBlock returns false.
Answer true if none does, false otherwise."
<category: 'enumeration'>
self do: [:element | (aBlock value: element) ifFalse: [^false]].
^true
]
contains: aBlock [
"Search the receiver for an element for which aBlock returns true.
Answer true if some does, false otherwise."
<category: 'enumeration'>
self do: [:element | (aBlock value: element) ifTrue: [^true]].
^false
]
detect: aBlock [
"Search the receiver for an element for which aBlock returns true.
If some does, answer it. If none does, fail"
<category: 'enumeration'>
^self detect: aBlock
ifNone: [SystemExceptions.NotFound signal: 'object not found']
]
fold: binaryBlock [
"First, pass to binaryBlock the first and second elements of the
receiver; for each subsequent element, pass the result of the previous
evaluation and an element. Answer the result of the last invocation,
or the first element if the collection has size 1. Fail if the collection
is empty."
<category: 'enumeration'>
| result marker |
result := marker := Object new.
self do:
[:element |
result := result == marker
ifTrue: [element]
ifFalse: [binaryBlock value: result value: element]].
result == marker ifTrue: [^SystemExceptions.EmptyCollection signalOn: self].
^result
]
inject: thisValue into: binaryBlock [
"First, pass to binaryBlock thisValue and the first element of the
receiver; for each subsequent element, pass the result of the previous
evaluation and an element. Answer the result of the last invocation."
<category: 'enumeration'>
| result |
result := thisValue.
self do: [:element | result := binaryBlock value: result value: element].
^result
]
nextPutAllOn: aStream [
"Write all the objects in the receiver to aStream"
<category: 'streaming'>
self do: [ :each | aStream nextPut: each ]
]
readStream [
"Return a stream with the same contents as the receiver."
<category: 'streaming'>
self subclassResponsibility
]
]
|