List comprehension and default argument name

Hello ruby-talk,

some suggestions for ruby:

  1. list comprehension

{ |x<-0…n| x*2 }
{ |n, x<-0…n, y<-0…x| [x,y] }

  1. default argument name to block/method

map {2} list # same as map {|x| x2} list
map {
.lc} list # same as map {|x| x.lc} list
def x2 => _2 # def x2(x); x2; end

···


Best regards,
Bulat mailto:bulatz@integ.ru

Hi,

  1. list comprehension

{ |x<-0…n| x*2 }

(0…n).map{|x|x*2}

{ |n, x<-0…n, y<-0…x| [x,y] }

I’m not sure how this works.

  1. default argument name to block/method

Hmm. I don’t think it’s good.

						matz.
···

In message “list comprehension and default argument name” on 02/11/25, “Bulat Ziganshin” bulatz@integ.ru writes:

some suggestions for ruby:

  1. list comprehension
    { |x<-0…n| x*2 }
    { |n, x<-0…n, y<-0…x| [x,y] }

Why not do this another way?

(0…5).collect { |x| x * 2 }
[0, 2, 4, 6, 8]

The second one would be mildly harder:

(0…5).collect { |x| (0…x).collect { |y| [x, y] } }
[[[0, 0]], [[1, 0], [1, 1]], [[2, 0], [2, 1], [2, 2]], [[3, 0], [3,
1], [3, 2], [3, 3]], [[4, 0], [4, 1], [4, 2], [4, 3], [4, 4]]]

That is, if I understand your second one.

Obviously, I’m using n = 5.

  1. default argument name to block/method
    map {2} list # same as map {|x| x2} list
    map {
    .lc} list # same as map {|x| x.lc} list
    def x2 => _2 # def x2(x); x2; end

I’m afraid that I don’t see the point of these, except obfuscation.
All you’ve done here is replace ‘|x| x’ with ‘_’, and that does
nothing to help readability, IMO.

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.11.25 at 11.53.13

···

On Mon, 25 Nov 2002 15:32:38 +0900, Bulat Ziganshin wrote:

some suggestions for ruby:

  1. list comprehension

{ |x<-0…n| x*2 }
{ |n, x<-0…n, y<-0…x| [x,y] }

  1. default argument name to block/method

map {2} list # same as map {|x| x2} list
map {
.lc} list # same as map {|x| x.lc} list
def x2 => _2 # def x2(x); x2; end

I proposed both of these 2-3 months ago and they were not popular. I would now
not propose the first one - it is rather obfuscated. In its place I would
propose Enumerable#mapf (/#collectf) (“map function”). Your first example
couldn’t be done using it, but the second one would be
list.mapf(:lc)

I still think list comprehensions are really nice, but they didn’t strike a
chord in the community and I have never actually needed them.

Gavin

···

From: “Bulat Ziganshin” bulatz@integ.ru

Hi –

···

On Tue, 26 Nov 2002, Gavin Sinclair wrote:

From: “Bulat Ziganshin” bulatz@integ.ru

some suggestions for ruby:

  1. list comprehension

{ |x<-0…n| x*2 }
{ |n, x<-0…n, y<-0…x| [x,y] }

  1. default argument name to block/method

map {2} list # same as map {|x| x2} list
map {
.lc} list # same as map {|x| x.lc} list
def x2 => _2 # def x2(x); x2; end

I proposed both of these 2-3 months ago and they were not popular.
I would now not propose the first one - it is rather obfuscated.
In its place I would propose Enumerable#mapf (/#collectf) (“map
function”). Your first example couldn’t be done using it, but the
second one would be
list.mapf(:lc)

That’s been proposed and rejected; see
http://www.rubygarden.org/article.php?sid=127. Matz’s comment was:

It’s too functional so that I’m afraid it would not work well with
OO nature of Ruby. Maybe it’s matter of notation.

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hello Austin,

Monday, November 25, 2002, 8:00:10 PM, you wrote:

  1. list comprehension
    { |x<-0…n| x*2 }
    { |n, x<-0…n, y<-0…x| [x,y] }
    map {2} list # same as map {|x| x2} list
    map {
    .lc} list # same as map {|x| x.lc} list
    def x2 => _2 # def x2(x); x2; end

only reason of my suggestions is compactness of FP-style code

···


Best regards,
Bulat mailto:bulatz@integ.ru

Hello dblack,

Tuesday, November 26, 2002, 2:56:37 AM, you wrote:

  1. list comprehension
    Matz’s comment was: It’s too functional

of course :slight_smile:

···


Best regards,
Bulat mailto:bulatz@integ.ru

FP?

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.11.27 at 09.10.28

···

On Wed, 27 Nov 2002 13:49:36 +0900, Bulat Ziganshin wrote:

Hello Austin,
Monday, November 25, 2002, 8:00:10 PM, you wrote:

  1. list comprehension
    { |x<-0…n| x*2 }
    { |n, x<-0…n, y<-0…x| [x,y] }
    map {2} list # same as map {|x| x2} list
    map {
    .lc} list # same as map {|x| x.lc} list
    def x2 => _2 # def x2(x); x2; end
    only reason of my suggestions is compactness of FP-style code

Functional Programming

– Nikodemus

···

On Wed, 27 Nov 2002, Austin Ziegler wrote:

FP?

Austin Ziegler wrote:

···

On Wed, 27 Nov 2002 13:49:36 +0900, Bulat Ziganshin wrote:

only reason of my suggestions is compactness of FP-style code

FP?

functional programming (think LISP).