Additional methods for Array

I’m wondering what other people’s thoughts are about adding these
methods. They are found in PHP but not in Ruby at the moment. I’m not
submitting an RCR because I don’t feel strongly about these myself, but
since Ruby’s class library are usually “battery-included”, there might
be interest to include the methods below as well. Each of these can be
implemented with a few lines of Ruby.

Array#shuffle -> nil
shuffle an array. perhaps add an argument to specify “how much” do
we want to shuffle?

Array#pick(n=1) -> anArray (method name?)
pick one or more random elements from array.

Array#pick(n=1) { |x,y,z…| } (syntax? method name?)
feed block with random element(s) from the array.

Perhaps it would be nice too if there is a random pick method with
weighting, but I cannot propose the decent syntax for it.

Array#chunk(n) -> anArray
split an array into chunks, each containing at most n elements.

Any other “useful” methods people want for Array?

···


dave

Maybe Array#%() can be a synonim (% - the modulo operator)

How about enabling the user to define custom operators? Like in sml and
prolog? (The charset of the operators is given, and one can define
operators like <<< -± whatever…)

What do you think?

Gergo

···

On 1214, David Garamond wrote:

Array#chunk(n) → anArray
split an array into chunks, each containing at most n elements.


±[ Kontra, Gergelykgergely@mcl.hu PhD student Room IB113 ]---------+

http://www.mcl.hu/~kgergely “Olyan langesz vagyok, hogy |
Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom” |
±- Magyar php mirror es magyar php dokumentacio: http://hu.php.net --+

David Garamond wrote:

Any other “useful” methods people want for Array?

I’ve found the need for #rotate recently…

I would suggest that you may want to implement these in pure Ruby and add
them to Gavin Sinclair’s Standard Library Extensions library available on
RubyForge. If they become popular enough, then people can note that in
feedback and that would weigh heavily on their utility and need.

-austin

···

On Sun, 14 Dec 2003 01:10:55 +0900, David Garamond wrote:

I’m wondering what other people’s thoughts are about adding these
methods. They are found in PHP but not in Ruby at the moment. I’m not
submitting an RCR because I don’t feel strongly about these myself, but
since Ruby’s class library are usually “battery-included”, there might be
interest to include the methods below as well. Each of these can be
implemented with a few lines of Ruby.

Array#shuffle → nil
Array#pick(n=1) → anArray (method name?)
Array#pick(n=1) { |x,y,z…| } (syntax? method name?) feed block with
Array#chunk(n) → anArray


austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.12.13
* 18.37.13

Array#chunk(n) → anArray
split an array into chunks, each containing at most n elements.
Maybe Array#%() can be a synonim (% - the modulo operator)

This isn’t completely intuitive, to me. (Yes, I get where it comes from;
it’s still not completely intuitive.)

How about enabling the user to define custom operators? Like in sml and
prolog? (The charset of the operators is given, and one can define
operators like <<< -± whatever…)

What do you think?

I think that Matz has demurred on this matter; I also think that it would
make parsing unnecessarily difficult. I would personally find it more useful
to be able to specify argument sides, e.g.:

def mul(left, right); x * y ; end

x mul y

But even that is of limited utility.

-austin

···

On Sun, 14 Dec 2003 01:33:55 +0900, KONTRA Gergely wrote:

On 1214, David Garamond wrote:

austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.12.13
* 18.38.57

I’ve highlighted the relevant messages as a reminder for me to add
(some of) these.

Cheers,
Gavin

···

On Sunday, December 14, 2003, 10:38:44 AM, Austin wrote:

On Sun, 14 Dec 2003 01:10:55 +0900, David Garamond wrote:

I’m wondering what other people’s thoughts are about adding these
methods. They are found in PHP but not in Ruby at the moment. I’m not
submitting an RCR because I don’t feel strongly about these myself, but
since Ruby’s class library are usually “battery-included”, there might be
interest to include the methods below as well. Each of these can be
implemented with a few lines of Ruby.

Array#shuffle → nil
Array#pick(n=1) → anArray (method name?)
Array#pick(n=1) { |x,y,z…| } (syntax? method name?) feed block with
Array#chunk(n) → anArray

I would suggest that you may want to implement these in pure Ruby and add
them to Gavin Sinclair’s Standard Library Extensions library available on
RubyForge. If they become popular enough, then people can note that in
feedback and that would weigh heavily on their utility and need.

How about enabling the user to define custom operators? Like in sml and
prolog? (The charset of the operators is given, and one can define
operators like <<< -± whatever…)

I know how operators work in Prolog (or at least in Sicstus Prolog), I
don’t know sml. But how would you handle precendence of operators? Prolog
let’s you declare an operator, its associativity (to the
right/left/neither), and its precendence as a number. What happens when
two libraries define the same new operator, but want to give it different
precendence or associativity? In Prolog, that would be no problem, it
would just override the previous declarations, but the other library’s
code would no longer parse. Though I defined my own operators in Prolog a
few times, I generally refrain from doing so because I don’t like the
ambiguity. I would be very interested in learning a solution to this
problem though.

Peter