Array question

Any one know why Array.join can’t take a code block and join that
value instead of what is actually in the array? Perhaps I want to
join on a subfield or process the item before joining it. Right now
I use collect and then join, but that just seems wasteful of memory.

for instance (and yes I know it is a silly example):

list = [“A”, “B”, “C”]

#I want to do this
puts list.join(","){|item| item.downcase}

#but I have to do this
puts list.collect{|item| item.downcase}.join(",")

Am I missing something?

Thanks,

Walt

···

Walter Szewelanczyk
IS Director
M.W. Sewall & CO. email : walter@mwsewall.com
259 Front St. Phone : (207) 442-7994 x 128
Bath, ME 04530 Fax : (207) 443-6284


No, that appears to be a very sensible solution to me. It
communicates your intentions exactly. If you are working on a very
large array, then perhaps you might need to find a way to conserve
memory, but it shouldn’t be necessary in 99% of cases: the memory used
by #collect is very temporary and will be garbage collected.

That said, the usage of #join you propose doesn’t sound too bad at
all.

Gavin

···

On Friday, March 28, 2003, 9:31:47 AM, walter wrote:

Any one know why Array.join can’t take a code block and join that
value instead of what is actually in the array? Perhaps I want to
join on a subfield or process the item before joining it. Right now
I use collect and then join, but that just seems wasteful of memory.

for instance (and yes I know it is a silly example):

list = [“A”, “B”, “C”]

#I want to do this
puts list.join(“,”){|item| item.downcase}

#but I have to do this
puts list.collect{|item| item.downcase}.join(“,”)

Am I missing something?

Hi,

···

In message “Array question” on 03/03/28, walter@mwsewall.com walter@mwsewall.com writes:

#I want to do this
puts list.join(“,”){|item| item.downcase}

#but I have to do this
puts list.collect{|item| item.downcase}.join(“,”)

Hmm, I feel like this filtering is not part of “join” operation.
But I’m interested in how others think.

						matz.

I think this is an excellent idea and would be a nice addition to Ruby.
Please RCR it, if not done already.
— url: What is Pre-settlement Funding?


author: Bruce R. Williams
url: http://codedbliss.com
irc: { nick: iusris, channel: ruby-lang, server: irc.freenode.net }
quote: >
It does not require a majority to prevail, but rather an irate,
tireless minority keen to set brush fires in people’s minds.
– Samuel Adams

···

On Thursday 27 March 2003 05:38 pm, Gavin Sinclair wrote:

On Friday, March 28, 2003, 9:31:47 AM, walter wrote:

Any one know why Array.join can’t take a code block and join that
value instead of what is actually in the array? Perhaps I want to
join on a subfield or process the item before joining it. Right now
I use collect and then join, but that just seems wasteful of memory.

for instance (and yes I know it is a silly example):

list = [“A”, “B”, “C”]

#I want to do this
puts list.join(“,”){|item| item.downcase}

#but I have to do this
puts list.collect{|item| item.downcase}.join(“,”)

Am I missing something?

No, that appears to be a very sensible solution to me. It
communicates your intentions exactly. If you are working on a very
large array, then perhaps you might need to find a way to conserve
memory, but it shouldn’t be necessary in 99% of cases: the memory used
by #collect is very temporary and will be garbage collected.

That said, the usage of #join you propose doesn’t sound too bad at
all.

Gavin

Uh, no it doesn’t strike me as what ‘join’ is supposed to do.
Intuitively, I think that a ‘join’ function should only join the entries
of the array and not modify the entries themselves.

···

On Fri, Mar 28, 2003 at 06:00:59PM +0900, Yukihiro Matsumoto wrote:

#I want to do this
puts list.join(“,”){|item| item.downcase}

#but I have to do this
puts list.collect{|item| item.downcase}.join(“,”)

Hmm, I feel like this filtering is not part of “join” operation.
But I’m interested in how others think.

  					matz.


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

depone: depone (di-POHN) verb tr., intr.
To declare under oath.

Hi,

···

At Fri, 28 Mar 2003 18:00:59 +0900, Yukihiro Matsumoto wrote:

#I want to do this
puts list.join(“,”){|item| item.downcase}

#but I have to do this
puts list.collect{|item| item.downcase}.join(“,”)

Hmm, I feel like this filtering is not part of “join” operation.
But I’m interested in how others think.

It reminds me “mapconcat” in elisp.


Nobu Nakada

“Yukihiro Matsumoto” matz@ruby-lang.org schrieb im Newsbeitrag
news:1048842051.937425.25237.nullmailer@picachu.netlab.jp…

Hi,

#I want to do this
puts list.join(“,”){|item| item.downcase}

#but I have to do this
puts list.collect{|item| item.downcase}.join(“,”)

Hmm, I feel like this filtering is not part of “join” operation.
But I’m interested in how others think.

I agree. People who need this can easily implement it themselves (even
for Array and other built in classes) but it should definitely not be part
of the standard library.

Regards

robert
···

In message “Array question” > on 03/03/28, walter@mwsewall.com walter@mwsewall.com writes:

I would agree - otherwise you’re combining the functionality of ‘join’ with
‘collect’. What might be useful is for join to take a block which decides
what separator to use based on the value of the previous item, eg:
(1…10).to_a.join { |i|
i % 2 == 0 ? " " : “-”
} # => “1-2 3-4 5-6 7-8 9-10”
This is a trivial example, and I can’t think of a real example off the top of
my head, so feel free to reject it on the grounds that it may not be useful,
but this just occurred to me and I thought I’d throw it out there…

Tim Bates

···

On Fri, 28 Mar 2003 7:37 pm, Daniel Carrera wrote:

Uh, no it doesn’t strike me as what ‘join’ is supposed to do.
Intuitively, I think that a ‘join’ function should only join the entries
of the array and not modify the entries themselves.


tim@bates.id.au

Uh, no it doesn’t strike me as what ‘join’ is supposed to do.
Intuitively, I think that a ‘join’ function should only join the entries
of the array and not modify the entries themselves.

···

----- Original Message -----
From: “Daniel Carrera” dcarrera@math.umd.edu

On Fri, Mar 28, 2003 at 06:00:59PM +0900, Yukihiro Matsumoto wrote:

#I want to do this
puts list.join(“,”){|item| item.downcase}

#but I have to do this
puts list.collect{|item| item.downcase}.join(“,”)

Hmm, I feel like this filtering is not part of “join” operation.
But I’m interested in how others think.

matz.


That was collect', not collect!'; the array entries would not be modified.

Matz comment was interesting. Do I feel like it is part of the join
operation? No, I don’t, but that’s not the question I would ask.

Currently, the block is totally ignored by #join:

irb(main):001:0> arr = [‘A’, ‘B’]
[“A”, “B”]
irb(main):002:0> arr.join(“,”){|item| item.downcase}
“A,B”

Is this the most meaningful (dare I say “expected”?) behavior? I think the
above is perfectly readable, clear code.

So to me, the real question is, “Does it make more sense to ignore the block
or to use it as outlined above?” (I like the idea.)

Chris (the blockophile who would probably add a block to #reverse if given a
chance :slight_smile:

Uh, no it doesn’t strike me as what ‘join’ is supposed to do.
Intuitively, I think that a ‘join’ function should only join the entries
of the array and not modify the entries themselves.

I would agree - otherwise you’re combining the functionality of ‘join’ with
‘collect’. What might be useful is for join to take a block which decides
what separator to use based on the value of the previous item,

That’d be cool.

eg:
(1…10).to_a.join { |i|
i % 2 == 0 ? " " : “-”
} # => “1-2 3-4 5-6 7-8 9-10”
This is a trivial example, and I can’t think of a real example off the top of
my head,

Set the path according to the OS.

path = directories.joing { |i|
CONFIG[“host_os”] =~ /Win/ ? “%” : “:”
}

···

On Fri, Mar 28, 2003 at 06:14:11PM +0900, Tim Bates wrote:

On Fri, 28 Mar 2003 7:37 pm, Daniel Carrera wrote:


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

depone: depone (di-POHN) verb tr., intr.
To declare under oath.

I don’t have any use for this yet, too, but you could even pass both
the last and the next item to the block.

Regards,
Pit

···

On 28 Mar 2003 at 18:14, Tim Bates wrote:

What might be useful is for join to take a block which decides
what separator to use based on the value of the previous item, eg:
(1…10).to_a.join { |i|
i % 2 == 0 ? " " : “-”
} # => “1-2 3-4 5-6 7-8 9-10”
This is a trivial example, and I can’t think of a real example off the top of
my head, so feel free to reject it on the grounds that it may not be useful,
but this just occurred to me and I thought I’d throw it out there…

“Chris Pine” nemo@hellotree.com schrieb im Newsbeitrag
news:075801c2f53a$c63a6820$6401a8c0@MELONBALLER…

Chris (the blockophile who would probably add a block to #reverse if
given a
chance :slight_smile:

:slight_smile: You can do that, since you can modify classes at all times. But I
would not put it into the std lib. It seems to awkward to me.

robert

Ooops. Cancel that. Stupid example. I just realized that it doesn’t use
‘i’ at all. I’m sure that there’s a good use for this feature somewhere
though. :slight_smile:

···

On Fri, Mar 28, 2003 at 06:27:27PM +0900, Daniel Carrera wrote:

Set the path according to the OS.

path = directories.joing { |i|
CONFIG[“host_os”] =~ /Win/ ? “%” : “:”
}


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

depone: depone (di-POHN) verb tr., intr.
To declare under oath.

Wait, how is that different from this?
path = directories.join(CONFIG[“host_os”] =~ /Win/ ? “%” : “:”)

Tim Bates

···

On Fri, 28 Mar 2003 7:57 pm, Daniel Carrera wrote:

Set the path according to the OS.

path = directories.joing { |i|
CONFIG[“host_os”] =~ /Win/ ? “%” : “:”
}


tim@bates.id.au

out of curiosity, why would a block to #join be awkward? It’s wholly
additive to the library, it doesn’t break any existing code, and
anything you write could just as easily not use it.

It seems a rather invisible useful addition to me.

···

— Robert Klemme bob.news@gmx.net wrote:

“Chris Pine” nemo@hellotree.com schrieb im Newsbeitrag
news:075801c2f53a$c63a6820$6401a8c0@MELONBALLER…

Chris (the blockophile who would probably add a block to #reverse
if
given a
chance :slight_smile:

:slight_smile: You can do that, since you can modify classes at all times.
But I
would not put it into the std lib. It seems to awkward to me.


Do you Yahoo!?
Yahoo! Platinum - Watch CBS’ NCAA March Madness, live on your desktop!
http://platinum.yahoo.com

What, something wrong with File.join?

···

Daniel Carrera (dcarrera@math.umd.edu) wrote:

This is a trivial example, and I can’t think of a real example off the top of
my head,

Set the path according to the OS.

path = directories.joing { |i|
CONFIG[“host_os”] =~ /Win/ ? “%” : “:”
}


Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

As I said, stupid example, brain slip.

···

On Fri, Mar 28, 2003 at 06:30:55PM +0900, Tim Bates wrote:

On Fri, 28 Mar 2003 7:57 pm, Daniel Carrera wrote:

Set the path according to the OS.

path = directories.joing { |i|
CONFIG[“host_os”] =~ /Win/ ? “%” : “:”
}

Wait, how is that different from this?
path = directories.join(CONFIG[“host_os”] =~ /Win/ ? “%” : “:”)


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

depone: depone (di-POHN) verb tr., intr.
To declare under oath.

Hi,

···

In message “Re: Array question” on 03/03/29, Michael Campbell michael_s_campbell@yahoo.com writes:

out of curiosity, why would a block to #join be awkward? It’s wholly
additive to the library, it doesn’t break any existing code, and
anything you write could just as easily not use it.

Somehow, I feel feeping creaturism here.

						matz.

To me, it’s not obvious that join with a block should do collect-and-join.
Why not select-and-join, for example?

Regards,

Brian.

···

On Sat, Mar 29, 2003 at 12:50:01AM +0900, Michael Campbell wrote:

out of curiosity, why would a block to #join be awkward? It’s wholly
additive to the library, it doesn’t break any existing code, and
anything you write could just as easily not use it.

It seems a rather invisible useful addition to me.

Yeah, I didn’t see that one until afterwards.

Tim Bates

···

On Fri, 28 Mar 2003 8:08 pm, Daniel Carrera wrote:

As I said, stupid example, brain slip.


tim@bates.id.au