#(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20) -> { #(17 17 16 18 17 18 19) . #(19 19 18 19 19 20 19) . #(20 19 20 20 20 19 20)} any code?
The first thing I found: #(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20) groupsOf: 7 atATimeCollect: [ :x | x ] "#(#(16 17 17 16 18 17 18) #(19 19 19 18 19 19 20) #(19 20 19 20 20 20 19))"
On 25 Apr 2017, at 22:08, Stephane Ducasse <stepharo.self@gmail.com> wrote:
#(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20)
->
{ #(17 17 16 18 17 18 19) . #(19 19 18 19 19 20 19) . #(20 19 20 20 20 19 20)}
any code?
#(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20) allButFirst groupsOf: 7 atATimeCollect: [ :element | element ]. ----- Nacho Smalltalker apprentice. Buenos Aires, Argentina. -- View this message in context: http://forum.world.st/little-challenge-tp4944073p4944079.html Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
Tx same comments! On Tue, Apr 25, 2017 at 10:48 PM, nacho <0800nacho@gmail.com> wrote:
#(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20) allButFirst groupsOf: 7 atATimeCollect: [ :element | element ].
----- Nacho Smalltalker apprentice. Buenos Aires, Argentina. -- View this message in context: http://forum.world.st/little- challenge-tp4944073p4944079.html Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
I did not know this one :) tx sven On Tue, Apr 25, 2017 at 10:26 PM, Sven Van Caekenberghe <sven@stfx.eu> wrote:
The first thing I found:
#(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20) groupsOf: 7 atATimeCollect: [ :x | x ]
"#(#(16 17 17 16 18 17 18) #(19 19 19 18 19 19 20) #(19 20 19 20 20 20 19))"
On 25 Apr 2017, at 22:08, Stephane Ducasse <stepharo.self@gmail.com> wrote:
#(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19
->
{ #(17 17 16 18 17 18 19) . #(19 19 18 19 19 20 19) . #(20 19 20 20 20
19 20)}
any code?
In 2003, I implemented the following method. It is something that comes up from time to time. !SequenceableCollection publicMethods ! splitEvery: chunkSize "Answer a collection of sub-collections each of the specified chunk size. If the receiver size is not a multiple of chunkSize, the last sub-collection will be shorter." | chunks stream | chunkSize < 1 ifTrue: [ ^self splitEvery: 1 ]. stream := ReadStream on: self. chunks := OrderedCollection new. [stream atEnd] whileFalse: [ chunks add: (stream nextOrLess: chunkSize). ]. ^chunks. ! ! _____ From: Pharo-users [mailto:pharo-users-bounces@lists.pharo.org] On Behalf Of Stephane Ducasse Sent: April 25, 2017 13:08 To: Any question about pharo is welcome Subject: [Pharo-users] little "challenge" #(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20) -> { #(17 17 16 18 17 18 19) . #(19 19 18 19 19 20 19) . #(20 19 20 20 20 19 20)} any code?
This is fun because my method was called splitEvery: too :) On Wed, Apr 26, 2017 at 6:19 AM, Richard Sargent <rsargent@5x5.on.ca> wrote:
In 2003, I implemented the following method. It is something that comes up from time to time.
!SequenceableCollection publicMethods ! splitEvery: chunkSize "Answer a collection of sub-collections each of the specified chunk size. If the receiver size is not a multiple of chunkSize, the last sub-collection will be shorter."
| chunks stream | chunkSize < 1 ifTrue: [ ^self splitEvery: 1 ].
stream := ReadStream on: self. chunks := OrderedCollection new. [stream atEnd] whileFalse: [ chunks add: (stream nextOrLess: chunkSize). ].
^chunks. ! !
------------------------------ *From:* Pharo-users [mailto:pharo-users-bounces@lists.pharo.org] *On Behalf Of *Stephane Ducasse *Sent:* April 25, 2017 13:08 *To:* Any question about pharo is welcome *Subject:* [Pharo-users] little "challenge"
#(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20)
->
{ #(17 17 16 18 17 18 19) . #(19 19 18 19 19 20 19) . #(20 19 20 20 20 19 20)}
any code?
we do not have nextOrLess: in Pharo. On Wed, Apr 26, 2017 at 6:19 AM, Richard Sargent <rsargent@5x5.on.ca> wrote:
In 2003, I implemented the following method. It is something that comes up from time to time.
!SequenceableCollection publicMethods ! splitEvery: chunkSize "Answer a collection of sub-collections each of the specified chunk size. If the receiver size is not a multiple of chunkSize, the last sub-collection will be shorter."
| chunks stream | chunkSize < 1 ifTrue: [ ^self splitEvery: 1 ].
stream := ReadStream on: self. chunks := OrderedCollection new. [stream atEnd] whileFalse: [ chunks add: (stream nextOrLess: chunkSize). ].
^chunks. ! !
------------------------------ *From:* Pharo-users [mailto:pharo-users-bounces@lists.pharo.org] *On Behalf Of *Stephane Ducasse *Sent:* April 25, 2017 13:08 *To:* Any question about pharo is welcome *Subject:* [Pharo-users] little "challenge"
#(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20)
->
{ #(17 17 16 18 17 18 19) . #(19 19 18 19 19 20 19) . #(20 19 20 20 20 19 20)}
any code?
Sorry, Stephane. Feel free to implement missing methods (or their equivalent). :-) On Apr 25, 2017, 23:39, at 23:39, Stephane Ducasse <stepharo.self@gmail.com> wrote:
we do not have nextOrLess: in Pharo.
On Wed, Apr 26, 2017 at 6:19 AM, Richard Sargent <rsargent@5x5.on.ca> wrote:
In 2003, I implemented the following method. It is something that comes up from time to time.
!SequenceableCollection publicMethods ! splitEvery: chunkSize "Answer a collection of sub-collections each of the specified chunk size. If the receiver size is not a multiple of chunkSize, the last sub-collection will be shorter."
| chunks stream | chunkSize < 1 ifTrue: [ ^self splitEvery: 1 ].
stream := ReadStream on: self. chunks := OrderedCollection new. [stream atEnd] whileFalse: [ chunks add: (stream nextOrLess: chunkSize). ].
^chunks. ! !
------------------------------ *From:* Pharo-users [mailto:pharo-users-bounces@lists.pharo.org] *On Behalf Of *Stephane Ducasse *Sent:* April 25, 2017 13:08 *To:* Any question about pharo is welcome *Subject:* [Pharo-users] little "challenge"
#(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20)
->
{ #(17 17 16 18 17 18 19) . #(19 19 18 19 19 20 19) . #(20 19 20 20 20 19 20)}
any code?
Doesn't matter as next returns if there is less than required. groups := OrderedCollection new. input := (String loremIpsum: 20). input readStream in: [ :s | [s atEnd] whileFalse: [ groups add: (s next: 7) ]]. groups. Interestingly loremIpsum: adds Character cr to from paragraphs (output looked strange at first glance). Phil On Wed, Apr 26, 2017 at 8:38 AM, Stephane Ducasse <stepharo.self@gmail.com> wrote:
we do not have nextOrLess: in Pharo.
On Wed, Apr 26, 2017 at 6:19 AM, Richard Sargent <rsargent@5x5.on.ca> wrote:
In 2003, I implemented the following method. It is something that comes up from time to time.
!SequenceableCollection publicMethods ! splitEvery: chunkSize "Answer a collection of sub-collections each of the specified chunk size. If the receiver size is not a multiple of chunkSize, the last sub-collection will be shorter."
| chunks stream | chunkSize < 1 ifTrue: [ ^self splitEvery: 1 ].
stream := ReadStream on: self. chunks := OrderedCollection new. [stream atEnd] whileFalse: [ chunks add: (stream nextOrLess: chunkSize). ].
^chunks. ! !
------------------------------ *From:* Pharo-users [mailto:pharo-users-bounces@lists.pharo.org] *On Behalf Of *Stephane Ducasse *Sent:* April 25, 2017 13:08 *To:* Any question about pharo is welcome *Subject:* [Pharo-users] little "challenge"
#(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20)
->
{ #(17 17 16 18 17 18 19) . #(19 19 18 19 19 20 19) . #(20 19 20 20 20 19 20)}
any code?
I implemented it for fun and noticed that in your test data, the first 16 is gone. So, groupings are wrong with all methods proposed in a sense. With Richard's version, we get this in Pharo #(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20) splitEvery: 7 "an OrderedCollection(#(16 17 17 16 18 17 18) #(19 19 19 18 19 19 20) #(19 20 19 20 20 20 19) #(20))" splitEvery: chunkSize "Answer a collection of sub-collections each of the specified chunk size. If the receiver size is not a multiple of chunkSize, the last sub-collection will be shorter." | chunks stream | chunkSize < 1 ifTrue: [ ^self splitEvery: 1 ]. stream := ReadStream on: self. chunks := OrderedCollection new. [stream atEnd] whileFalse: [ chunks add: (stream next: chunkSize). ]. ^chunks. Phil On Wed, Apr 26, 2017 at 8:38 AM, Stephane Ducasse <stepharo.self@gmail.com> wrote:
we do not have nextOrLess: in Pharo.
On Wed, Apr 26, 2017 at 6:19 AM, Richard Sargent <rsargent@5x5.on.ca> wrote:
In 2003, I implemented the following method. It is something that comes up from time to time.
!SequenceableCollection publicMethods ! splitEvery: chunkSize "Answer a collection of sub-collections each of the specified chunk size. If the receiver size is not a multiple of chunkSize, the last sub-collection will be shorter."
| chunks stream | chunkSize < 1 ifTrue: [ ^self splitEvery: 1 ].
stream := ReadStream on: self. chunks := OrderedCollection new. [stream atEnd] whileFalse: [ chunks add: (stream nextOrLess: chunkSize). ].
^chunks. ! !
------------------------------ *From:* Pharo-users [mailto:pharo-users-bounces@lists.pharo.org] *On Behalf Of *Stephane Ducasse *Sent:* April 25, 2017 13:08 *To:* Any question about pharo is welcome *Subject:* [Pharo-users] little "challenge"
#(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20)
->
{ #(17 17 16 18 17 18 19) . #(19 19 18 19 19 20 19) . #(20 19 20 20 20 19 20)}
any code?
Now I fully got the implementation " #(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20) groupsOf: 7 atATimeCollect: [ :x | x ]
#(#(16 17 17 16 18 17 18) #(19 19 19 18 19 19 20) #(19 20 19 20 20 20 19)) " " #(1 1 1 10 10 10 100 100 100) groupsOf: 3 atATimeCollect: [ :x :y :z | x + y + z] #(3 30 300) "
" #(1 1 1 10 10 10 100 100 100) groupsOf: 3 atATimeCollect: [ :x | x ]
#(#(1 1 1) #(10 10 10) #(100 100 100)) "
On Wed, Apr 26, 2017 at 8:38 AM, Stephane Ducasse <stepharo.self@gmail.com> wrote:
we do not have nextOrLess: in Pharo.
On Wed, Apr 26, 2017 at 6:19 AM, Richard Sargent <rsargent@5x5.on.ca> wrote:
In 2003, I implemented the following method. It is something that comes up from time to time.
!SequenceableCollection publicMethods ! splitEvery: chunkSize "Answer a collection of sub-collections each of the specified chunk size. If the receiver size is not a multiple of chunkSize, the last sub-collection will be shorter."
| chunks stream | chunkSize < 1 ifTrue: [ ^self splitEvery: 1 ].
stream := ReadStream on: self. chunks := OrderedCollection new. [stream atEnd] whileFalse: [ chunks add: (stream nextOrLess: chunkSize). ].
^chunks. ! !
------------------------------ *From:* Pharo-users [mailto:pharo-users-bounces@lists.pharo.org] *On Behalf Of *Stephane Ducasse *Sent:* April 25, 2017 13:08 *To:* Any question about pharo is welcome *Subject:* [Pharo-users] little "challenge"
#(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20)
->
{ #(17 17 16 18 17 18 19) . #(19 19 18 19 19 20 19) . #(20 19 20 20 20 19 20)}
any code?
So the last 20 is left out. Correct? Phil On Wed, Apr 26, 2017 at 11:20 AM, Stephane Ducasse <stepharo.self@gmail.com> wrote:
Now I fully got the implementation
" #(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20) groupsOf: 7 atATimeCollect: [ :x | x ]
#(#(16 17 17 16 18 17 18) #(19 19 19 18 19 19 20) #(19 20 19 20 20 20 19)) " " #(1 1 1 10 10 10 100 100 100) groupsOf: 3 atATimeCollect: [ :x :y :z | x + y + z] #(3 30 300) "
" #(1 1 1 10 10 10 100 100 100) groupsOf: 3 atATimeCollect: [ :x | x ]
#(#(1 1 1) #(10 10 10) #(100 100 100)) "
On Wed, Apr 26, 2017 at 8:38 AM, Stephane Ducasse <stepharo.self@gmail.com
wrote:
we do not have nextOrLess: in Pharo.
On Wed, Apr 26, 2017 at 6:19 AM, Richard Sargent <rsargent@5x5.on.ca> wrote:
In 2003, I implemented the following method. It is something that comes up from time to time.
!SequenceableCollection publicMethods ! splitEvery: chunkSize "Answer a collection of sub-collections each of the specified chunk size. If the receiver size is not a multiple of chunkSize, the last sub-collection will be shorter."
| chunks stream | chunkSize < 1 ifTrue: [ ^self splitEvery: 1 ].
stream := ReadStream on: self. chunks := OrderedCollection new. [stream atEnd] whileFalse: [ chunks add: (stream nextOrLess: chunkSize). ].
^chunks. ! !
------------------------------ *From:* Pharo-users [mailto:pharo-users-bounces@lists.pharo.org] *On Behalf Of *Stephane Ducasse *Sent:* April 25, 2017 13:08 *To:* Any question about pharo is welcome *Subject:* [Pharo-users] little "challenge"
#(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20)
->
{ #(17 17 16 18 17 18 19) . #(19 19 18 19 19 20 19) . #(20 19 20 20 20 19 20)}
any code?
splitEvery: n ^ (n to: self size by: n) collect: [:index | (self copyFrom: index - n + 1 to: index)] is my solution On Wed, Apr 26, 2017 at 11:34 AM, phil@highoctane.be <phil@highoctane.be> wrote:
So the last 20 is left out.
Correct?
Phil
On Wed, Apr 26, 2017 at 11:20 AM, Stephane Ducasse < stepharo.self@gmail.com> wrote:
Now I fully got the implementation
" #(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20) groupsOf: 7 atATimeCollect: [ :x | x ]
#(#(16 17 17 16 18 17 18) #(19 19 19 18 19 19 20) #(19 20 19 20 20 20 19)) " " #(1 1 1 10 10 10 100 100 100) groupsOf: 3 atATimeCollect: [ :x :y :z | x + y + z] #(3 30 300) "
" #(1 1 1 10 10 10 100 100 100) groupsOf: 3 atATimeCollect: [ :x | x ]
#(#(1 1 1) #(10 10 10) #(100 100 100)) "
On Wed, Apr 26, 2017 at 8:38 AM, Stephane Ducasse < stepharo.self@gmail.com> wrote:
we do not have nextOrLess: in Pharo.
On Wed, Apr 26, 2017 at 6:19 AM, Richard Sargent <rsargent@5x5.on.ca> wrote:
In 2003, I implemented the following method. It is something that comes up from time to time.
!SequenceableCollection publicMethods ! splitEvery: chunkSize "Answer a collection of sub-collections each of the specified chunk size. If the receiver size is not a multiple of chunkSize, the last sub-collection will be shorter."
| chunks stream | chunkSize < 1 ifTrue: [ ^self splitEvery: 1 ].
stream := ReadStream on: self. chunks := OrderedCollection new. [stream atEnd] whileFalse: [ chunks add: (stream nextOrLess: chunkSize). ].
^chunks. ! !
------------------------------ *From:* Pharo-users [mailto:pharo-users-bounces@lists.pharo.org] *On Behalf Of *Stephane Ducasse *Sent:* April 25, 2017 13:08 *To:* Any question about pharo is welcome *Subject:* [Pharo-users] little "challenge"
#(16 17 17 16 18 17 18 19 19 19 18 19 19 20 19 20 19 20 20 20 19 20)
->
{ #(17 17 16 18 17 18 19) . #(19 19 18 19 19 20 19) . #(20 19 20 20 20 19 20)}
any code?
participants (5)
-
nacho -
phil@highoctane.be -
Richard Sargent -
Stephane Ducasse -
Sven Van Caekenberghe