Marcel Taeumel uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-mt.686.mcz ==================== Summary ==================== Name: Collections-mt.686 Author: mt Time: 12 April 2016, 11:24:26.579799 am UUID: 3ce0e394-c4eb-9c4d-9367-4f885e4a62f8 Ancestors: Collections-ul.685 Adds possibility to map and filter streams using generators. There are *Infinitely-versions of map and filter to support streams that can have contents again after #atEnd returned true - like socket streams. In that case, there is an #upToNil to get a chunk of objects to work with. =============== Diff against Collections-ul.685 =============== Item was added: + ----- Method: Stream>>collect: (in category 'enumerating') ----- + collect: block + + ^ Generator on: [:g | + [self atEnd] whileFalse: [ + g yield: (self next + ifNil: [nil] + ifNotNil: [:object | block value: object])]]! Item was added: + ----- Method: Stream>>collectInfinitely: (in category 'enumerating - infinitely') ----- + collectInfinitely: block + + ^ Generator on: [:g | + [ + g yield: (self next + ifNil: [nil] + ifNotNil: [:object | block value: object]) + ] repeat ]! Item was added: + ----- Method: Stream>>gather: (in category 'enumerating') ----- + gather: block + + ^ Generator on: [:g | + [self atEnd] whileFalse: [ + self next + ifNil: [g yield: nil] + ifNotNil: [:object | + (block value: object) do: [:ea | + g yield: ea]]]]! Item was added: + ----- Method: Stream>>gatherInfinitely: (in category 'enumerating - infinitely') ----- + gatherInfinitely: block + + ^ Generator on: [:g | + [ + self next + ifNil: [g yield: nil] + ifNotNil: [:object | + (block value: object) in: [:result | | n | + result isStream "Simulate upToNil for infinite gathering." + ifTrue: [ [n := result next] whileNotNil: [g yield: n] ] + ifFalse: [result do: [:ea | g yield: ea]]]] + ] repeat ]! Item was added: + ----- Method: Stream>>nextSatisfy: (in category 'accessing') ----- + nextSatisfy: aBlock + + self do: [:each | (aBlock value: each) ifTrue: [^ each]]. + Error signal: 'No object could satisfy the block'.! Item was added: + ----- Method: Stream>>reject: (in category 'enumerating') ----- + reject: aBlock + + ^ self select: [:element | (aBlock value: element) == false]! Item was added: + ----- Method: Stream>>rejectInfinitely: (in category 'enumerating - infinitely') ----- + rejectInfinitely: aBlock + + ^ self selectInfinitely: [:element | (aBlock value: element) == false]! Item was added: + ----- Method: Stream>>select: (in category 'enumerating') ----- + select: block + + ^ Generator on: [:g | + [self atEnd] whileFalse: [ + self next + ifNil: [g yield: nil] + ifNotNil: [:object | + (block value: object) + ifTrue: [g yield: object]]]]! Item was added: + ----- Method: Stream>>select:thenCollect: (in category 'enumerating') ----- + select: block thenCollect: anotherBlock + + ^ (self select: block) collect: anotherBlock! Item was added: + ----- Method: Stream>>selectInfinitely: (in category 'enumerating - infinitely') ----- + selectInfinitely: block + + ^ Generator on: [:g | + [ + self next + ifNil: [g yield: nil] + ifNotNil: [:object | + (block value: object) + ifTrue: [g yield: object]] + ] repeat ]! Item was changed: ----- Method: Stream>>upToEnd (in category 'accessing') ----- upToEnd + "Answer the remaining elements in the stream." + - "answer the remaining elements in the string" | elements | elements := OrderedCollection new. + [self atEnd] whileFalse: [ + elements add: self next]. + ^ elements! - [ self atEnd ] whileFalse: [ - elements add: self next ]. - ^elements! Item was added: + ----- Method: Stream>>upToNil (in category 'accessing') ----- + upToNil + "Answer all elements in the stream until a nil shows up." + + | elements next | + elements := OrderedCollection new. + [next := self next] whileNotNil: [ + elements add: next]. + ^ elements! |
Free forum by Nabble | Edit this page |