Arrays

Hi
s=Array.new
3.times { |x| 5.times.collect { |y|s.push[x, y] } }; p s; s=nil

can I do that without accumulating (in s)?
(on the fly; but depth==1 (not 2) ! )

thanks
Berg

"that" what?

Instead of your code you could simply do

  s =

which of course doesn't make any sense.
Apparently your code does not do what you intended.

Regards,
Marcus

···

Am 09.04.2016 um 16:55 schrieb A Berger:

s=Array.new
3.times { |x| 5.times.collect { |y|s.push[x, y] } }; [snip]

can I do that without accumulating (in s)?

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

> s=Array.new
> 3.times { |x| 5.times.collect { |y|s.push[x, y] } }; [snip]
>
> can I do that without accumulating (in s)?

"that" what?

Getting the same output but without storing (anything)

Instead of your code you could simply do

  s =

which of course doesn't make any sense.
Apparently your code does not do what you intended.

You didnt see the "p s" ?

s=Array.new;3.times{|x|5.times.collect{|y|s.push([x,y])}};p s
# sorry, whitespace makes trouble with copy&paste here
# sorry, no copy&paste for output possible.

···

Am 09.04.2016 17:12 schrieb <sto.mar@web.de>:

Am 09.04.2016 um 16:55 schrieb A Berger:

Regards,
Marcus

WHAT output?

The code posted by you leads to s being an empty array
(and the `p' statement doesn't change that at all).
I do not know what you want to do and I can not guess.

So please, as I just asked you to do in your other thread,
you need to put more effort into your posts and clearly write:

  1. what you did (i.e. the exact! code you used)
  2. what the output was
  3. what the problem/surprise/etc. is

Posts like "Why doesn't <some code> result in what I expected"
or similar are not likely to get too much helpful replies.

Regards,
Marcus

···

Am 09.04.2016 um 17:25 schrieb A Berger:

Am 09.04.2016 17:12 schrieb <sto.mar@web.de <mailto:sto.mar@web.de>>:

Am 09.04.2016 um 16:55 schrieb A Berger:
> s=Array.new
> 3.times { |x| 5.times.collect { |y|s.push[x, y] } }; [snip]
>
> can I do that without accumulating (in s)?

"that" what?

Getting the same output but without storing (anything)

--
GitHub: https://github.com/stomar/
PGP: 0x6B3A101A

You... you want to print out a string that looks just like the #inspect of
an array, but you don't want that array to exist? What problem are you
trying to solve, exactly?

Also, why is Enumerable#collect in there? You're not doing anything with
the collected values.

Do you want this?

    p 3.times.map{|x| 5.times.map{|y| [x, y] }}

Cheers, and goodnight

···

On 10 April 2016 at 01:25, A Berger <aberger7890@gmail.com> wrote:

Am 09.04.2016 17:12 schrieb <sto.mar@web.de>:
>
> Am 09.04.2016 um 16:55 schrieb A Berger:
> > s=Array.new
> > 3.times { |x| 5.times.collect { |y|s.push[x, y] } }; [snip]
> >
> > can I do that without accumulating (in s)?
>
> "that" what?
Getting the same output but without storing (anything)

> Instead of your code you could simply do
>
> s =
>
> which of course doesn't make any sense.
> Apparently your code does not do what you intended.
You didnt see the "p s" ?

s=Array.new;3.times{|x|5.times.collect{|y|s.push([x,y])}};p s
# sorry, whitespace makes trouble with copy&paste here
# sorry, no copy&paste for output possible.
>
> Regards,
> Marcus

--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

A Berger,

since at least two people put time into trying to help you,
it would be appropriate to bother and clarify your problem.

Marcus

···

--
GitHub: https://github.com/stomar/
PGP: 0x6B3A101A

Hi

s=Array.new;3.times{|x|5.times.collect{|y|s.push([x,y])}};p s

You... you want to print out a string that looks just like the #inspect

of an array, but you don't want that array to exist? What problem are you
trying to solve, exactly?

collect is a rest of the previous version. 2x collect makes depth 2.
I want the same output as "p s"
(having two independant loops)

Do you want this?

    p 3.times.map{|x| 5.times.map{|y| [x, y] }}

Exactly, but with depth==1 (not completely flat; is there another method to
'shift' the data up in the tree, flatten out just the outer (or inner)
level(s 'n') ?
I havent found any.

Cheers and good afternoon :wink:

Berg

I still do not get it...
Why don't you simply post an example for how the expected
output array should look like?

···

Am 09.04.2016 um 18:19 schrieb A Berger:

    p 3.times.map{|x| 5.times.map{|y| [x, y] }}

Exactly, but with depth==1 (not completely flat; is there another method
to 'shift' the data up in the tree, flatten out just the outer (or
inner) level(s 'n') ?

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Hello,
For example:
[[ 0,0] , [ 0,1 ], ..., [0,4], [1,0], [1,1] .... [ 1,4], [ 2,0], ...., [
2,3],[2,4] ]

Sure, you can fill the holes :slight_smile:
Berg

What I havnt mentioned: result should be an array...

Berg

Finally; why didn't you say so from the very first...

You can e.g.

- use two nested `times' iterators ("pure", i.e. no collects etc.)
  and from within push [x, y] to an initially empty array,
- use Array.new(15) {|num| ... } together with div 5 and mod 5

···

Am 09.04.2016 um 18:41 schrieb A Berger:

Hello,
For example:
[[ 0,0] , [ 0,1 ], ..., [0,4], [1,0], [1,1] .... [ 1,4], [ 2,0], ....,
[ 2,3],[2,4] ]

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Hello

Finally; why didn't you say so from the very first...

You can e.g.
- use two nested `times' iterators ("pure", i.e. no collects etc.)
  and from within push [x, y] to an initially empty array,

That was the first mail ("but without storing in an accumulator")

- use Array.new(15) {|num| ... } together with div 5 and mod 5

Thanks! A nice solution, but these loops are just examples.

Arising question is for a method shifting (flattening) a given level from
inner or outer side (sure Matthew knows that)
Besides that it would be interesting if there is a (surprising wonderful?)
solution with metaprogramming.

And I also want to denote my thanks to Matthew for his always helpful mails!

Good evening,
Berg

···

Am 09.04.2016 18:52 schrieb <sto.mar@web.de>:

I can only give solutions based on the information you provide,
naturally I would assume that the structure of the wanted arrays
is always similar.

  Array.new(15) {|num| [num / 5, num % 5]}

is also just an example and it should be easy to modify it
for other array dimensions.

And here another one:

  [0, 1, 2].product([0, 1, 2, 3, 4])

Regards,
Marcus

···

Am 09.04.2016 um 19:13 schrieb A Berger:

- use Array.new(15) {|num| ... } together with div 5 and mod 5

Thanks! A nice solution, but these loops are just examples.

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Array#flatten takes an optional level argument;
flatten(1) might be what you are looking for in this case.

Regards,
Marcus

···

Am 09.04.2016 um 19:13 schrieb A Berger:

Arising question is for a method shifting (flattening) a given level

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Please be careful spaces in ruby code

s.push[x, y] is meaning is call method `` on a array instance,
maybe you want do s.push([x, y]) or s.push [x, y]

···

2016-04-09 23:39 GMT+08:00 <sto.mar@web.de>:

Am 09.04.2016 um 17:25 schrieb A Berger:
>
> Am 09.04.2016 17:12 schrieb <sto.mar@web.de <mailto:sto.mar@web.de>>:
>>
>> Am 09.04.2016 um 16:55 schrieb A Berger:
>> > s=Array.new
>> > 3.times { |x| 5.times.collect { |y|s.push[x, y] } }; [snip]
>> >
>> > can I do that without accumulating (in s)?
>>
>> "that" what?
> Getting the same output but without storing (anything)

WHAT output?

The code posted by you leads to s being an empty array
(and the `p' statement doesn't change that at all).
I do not know what you want to do and I can not guess.

So please, as I just asked you to do in your other thread,
you need to put more effort into your posts and clearly write:

  1. what you did (i.e. the exact! code you used)
  2. what the output was
  3. what the problem/surprise/etc. is

Posts like "Why doesn't <some code> result in what I expected"
or similar are not likely to get too much helpful replies.

Regards,
Marcus

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Hi Marcus,
That's what I was looking for!
I knew about the flatten-arg already, dont know why that was not in my mind
anymore.
For completeness a negative argument, crunching from the inner levels would
be useful...
What do others think about that?
Thanks,
Berg

···

Am 09.04.2016 20:01 schrieb <sto.mar@web.de>:

Am 09.04.2016 um 19:13 schrieb A Berger:
> Arising question is for a method shifting (flattening) a given level

Array#flatten takes an optional level argument;
flatten(1) might be what you are looking for in this case.

Regards,
Marcus

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

- use Array.new(15) {|num| ... } together with div 5 and mod 5

Thanks! A nice solution, but these loops are just examples.

I can only give solutions based on the information you provide,
naturally I would assume that the structure of the wanted arrays
is always similar.

Array.new(15) {|num| [num / 5, num % 5]}

or just:

Array.new(15) {|i| i.divmod(5)}

-Rob

···

On 2016-Apr-9, at 13:56 , sto.mar@web.de wrote:
Am 09.04.2016 um 19:13 schrieb A Berger:

is also just an example and it should be easy to modify it
for other array dimensions.

And here another one:

[0, 1, 2].product([0, 1, 2, 3, 4])

Regards,
Marcus

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Hi
Yes thank you, I have to apologize, that arised because of bad copy&paste
(as mentioned in previous mail).
Thought only Python were space-dependent...

Bye Berg

···

Am 09.04.2016 17:58 schrieb "windwiny" <windwiny.ubt@gmail.com>:

Please be careful spaces in ruby code

s.push[x, y] is meaning is call method `` on a array instance,
maybe you want do s.push([x, y]) or s.push [x, y]

2016-04-09 23:39 GMT+08:00 <sto.mar@web.de>:

Am 09.04.2016 um 17:25 schrieb A Berger:
>
> Am 09.04.2016 17:12 schrieb <sto.mar@web.de <mailto:sto.mar@web.de>>:
>>
>> Am 09.04.2016 um 16:55 schrieb A Berger:
>> > s=Array.new
>> > 3.times { |x| 5.times.collect { |y|s.push[x, y] } }; [snip]
>> >
>> > can I do that without accumulating (in s)?
>>
>> "that" what?
> Getting the same output but without storing (anything)

WHAT output?

The code posted by you leads to s being an empty array
(and the `p' statement doesn't change that at all).
I do not know what you want to do and I can not guess.

So please, as I just asked you to do in your other thread,
you need to put more effort into your posts and clearly write:

  1. what you did (i.e. the exact! code you used)
  2. what the output was
  3. what the problem/surprise/etc. is

Posts like "Why doesn't <some code> result in what I expected"
or similar are not likely to get too much helpful replies.

Regards,
Marcus

--
GitHub: https://github.com/stomar/
PGP: 0x6B3A101A

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;