irregular iteration

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

irregular iteration

Adam Crumpton
somehow this got mis-threaded. this is a new thread.

hi!


instead of:

1 to: 100 by: 2 do: [:n | n doSomething ].

I want to iterate over the numbers in an irregular way based on some
sort of list or stream, like:

1 to: 100 byNext: #(1 2 3 4 5) do: [:n | Transcript show: (n
printString), cr].

and have it show:

1
3
6
10
15
16
18
21
24
29
...

any suggestions on implentation would be appreciated. The list has to be
of arbitrary length.

thanks
Adam Crumpton
_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: irregular iteration

Steffen Märcker
Hi,

how about a simple loop:

steps := #(2 3 4 5) readStream.
init := sum := 1.
last := 100.
[sum > 100 | steps atEnd] whileFalse:
                [sum := sum + steps next.
                Transcript
                        show: sum printString;
                        cr]

Best, Steffen



Am .10.2014, 02:17 Uhr, schrieb Adam Crumpton <[hidden email]>:

> somehow this got mis-threaded. this is a new thread.
>
> hi!
>
> instead of:
>
> 1 to: 100 by: 2 do: [:n | n doSomething ].
>
> I want to iterate over the numbers in an irregular way based on some
> sort of list or stream, like:
>
> 1 to: 100 byNext: #(1 2 3 4 5) do: [:n | Transcript show: (n
> printString), cr].
>
> and have it show:
>
> 1
> 3
> 6
> 10
> 15
> 16
> 18
> 21
> 24
> 29
> ...
>
> any suggestions on implentation would be appreciated. The list has to be
> of arbitrary length.
>
> thanks
> Adam Crumpton
_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: irregular iteration

Steffen Märcker
Hi,

additionally, you can do it using collections:

   steps := #(1 2 3 4 5).
   bounds := 1 to: 100.
   sum := 0.
   nums := (steps collect: [:n | sum := sum + n]) select: [:n | bounds  
includes: n].


Using the Reducers library, we can handle an infinite steps source, too.  
Here is an beefed up example using random numbers between 1 and 2:

   steps := [:r | r + 1] mapping <~ Random new.
   sum := 0.
   nums := [:n | (1 <= n) & (n <= 100)] takingWhile <~ [:r | sum := sum +  
r] mapping <~ steps.
   "no computation done yet, but lets put them into a set"
   Set <~ nums.

Best, Steffen


Am .10.2014, 09:02 Uhr, schrieb Steffen Märcker <[hidden email]>:

> Hi,
>
> how about a simple loop:
>
> steps := #(2 3 4 5) readStream.
> init := sum := 1.
> last := 100.
> [sum > 100 | steps atEnd] whileFalse:
> [sum := sum + steps next.
> Transcript
> show: sum printString;
> cr]
>
> Best, Steffen
>
>
>
> Am .10.2014, 02:17 Uhr, schrieb Adam Crumpton <[hidden email]>:
>
>> somehow this got mis-threaded. this is a new thread.
>>
>> hi!
>>
>> instead of:
>>
>> 1 to: 100 by: 2 do: [:n | n doSomething ].
>>
>> I want to iterate over the numbers in an irregular way based on some
>> sort of list or stream, like:
>>
>> 1 to: 100 byNext: #(1 2 3 4 5) do: [:n | Transcript show: (n
>> printString), cr].
>>
>> and have it show:
>>
>> 1
>> 3
>> 6
>> 10
>> 15
>> 16
>> 18
>> 21
>> 24
>> 29
>> ...
>>
>> any suggestions on implentation would be appreciated. The list has to be
>> of arbitrary length.
>>
>> thanks
>> Adam Crumpton
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: irregular iteration

Vincent Lesbros-2
Très élégant, Steffen !

I try a very basic method, implemented directly in Number class, with a "straitforward" transcription of how I understands it :


to: stop byNext: steps do: aBlock 
"1 to: 100 byNext: #(1 2 3 4 5) do: [:n | Transcript show: (n printString); cr]"

| stepIndex current |

stepIndex := 1.
current := self.
[ current <= stop ] whileTrue: [
aBlock value: current.
current := current + (steps at: stepIndex).
stepIndex := stepIndex + 1.
stepIndex > steps size ifTrue: [ stepIndex := 1 ] ]


But the result is not
1
3
6
10
15
16
18
21
24
29
...

I get :

1
2
4
7
11
16
17
19
22
26
...

Because the first step is 1 





2014-10-14 9:45 GMT+02:00 Steffen Märcker <[hidden email]>:
Hi,

additionally, you can do it using collections:

  steps := #(1 2 3 4 5).
  bounds := 1 to: 100.
  sum := 0.
  nums := (steps collect: [:n | sum := sum + n]) select: [:n | bounds includes: n].


Using the Reducers library, we can handle an infinite steps source, too. Here is an beefed up example using random numbers between 1 and 2:

  steps := [:r | r + 1] mapping <~ Random new.
  sum := 0.
  nums := [:n | (1 <= n) & (n <= 100)] takingWhile <~ [:r | sum := sum + r] mapping <~ steps.
  "no computation done yet, but lets put them into a set"
  Set <~ nums.

Best, Steffen


Am .10.2014, 09:02 Uhr, schrieb Steffen Märcker <[hidden email]>:


Hi,

how about a simple loop:

steps := #(2 3 4 5) readStream.
init := sum := 1.
last := 100.
[sum > 100 | steps atEnd] whileFalse:
                [sum := sum + steps next.
                Transcript
                        show: sum printString;
                        cr]

Best, Steffen



Am .10.2014, 02:17 Uhr, schrieb Adam Crumpton <[hidden email]>:

somehow this got mis-threaded. this is a new thread.

hi!

instead of:

1 to: 100 by: 2 do: [:n | n doSomething ].

I want to iterate over the numbers in an irregular way based on some
sort of list or stream, like:

1 to: 100 byNext: #(1 2 3 4 5) do: [:n | Transcript show: (n
printString), cr].

and have it show:

1
3
6
10
15
16
18
21
24
29
...

any suggestions on implentation would be appreciated. The list has to be
of arbitrary length.

thanks
Adam Crumpton
_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc