What is your implementation with with:collection: ?

I know this function from python, they  call it "zip"

we could implement this in SequenceableCollection

SequenceableCollection>>zip: aCollection
    | index |
    index := 0.
    self size > aCollection size ifTrue:[ ^ aCollection zip: self].
    ^ self class new: (aCollection size + self size) streamContents:
[:stream |
            aCollection
                do: [:each | index:=index + 1. stream nextPut: each ]
                separatedBy: [(index > self size) ifFalse:[stream nextPut:
(self at:index) ]]]

Another example, using a generator (I didn't used Generator before, but I
like the
idea to create an endless stream from a string, with some fill value (empty
string in this case)).

|gen|
gen := Generator on:[:g |  '123' do:[:k | g yield: k asString ] . [true]
whileTrue:[g yield:'']].
String streamContents:[:stream |
    'abcdefg' do:[:c | stream nextPut: c]
    separatedBy:[ stream nextPutAll:gen next]]



2016-03-15 2:09 GMT+01:00 Andy Burnett <andy.burn...@knowinnovation.com>:

> I have two strings, which are typically of different lengths, and I want
> to interleave them, i.e.:
>
> 'abcdefg' interleaveWith: '123'
>
> Would produce
>
> 'a1b2c3defg'
>
> I know how to write this procedurally, but I have been very impressed by
> what Sven shows in his Medium articles, so I wondered if there is any
> existing mechanism that would allow me to solve this elegantly?
>  with:collection: almost does it, but the collections have to be the same
> size.
>
> Cheers
> Andy
>

Reply via email to