More readable array querying

A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

All,
I've been playing with some code to make querying an array more readable.
Basically, it lets you write one of the following:

puts [1,2,3].first that {|x| x.even? }
puts [1,2,3].first that is :even

instead of this:

puts [1,2,3].find {|x| x.even? }

The code is below. I'm curious whether you'd find it useful, interesting,
worthwhile, etc., and if there are improvements you can think of. Either
way, it was a fun little exercise. I love the flexibility I get from writing
in ruby!

module Kernel
def that proc=nil, &block
p = proc.nil? ? block : proc
lambda { |x| x.instance_eval &p }
end

def is message
lambda { |x| x.send "#{message}?".to_sym }
end
end

class Array
alias :eek:ld_first first

def first func=nil
return old_first if func.nil?
each do |x|
return x if func.call x
end
nil
end
end
 
R

Ryan Davis

All,
I've been playing with some code to make querying an array more = readable.
Basically, it lets you write one of the following:
=20
puts [1,2,3].first that {|x| x.even? }
puts [1,2,3].first that is :even
=20
instead of this:
=20
puts [1,2,3].find {|x| x.even? }
=20
The code is below. I'm curious whether you'd find it useful, = interesting,
worthwhile, etc., and if there are improvements you can think of. = Either
way, it was a fun little exercise. I love the flexibility I get from = writing
in ruby!

I doubt I would ever want to use this. The amount of readability it adds =
is completely offset by how much slower it is:

# of iterations =3D 1000000
user system total real
null_time 0.090000 0.000000 0.090000 ( 0.095464)
ary.find 1.680000 0.010000 1.690000 ( 1.717772)
ary.first that 18.580000 1.140000 19.720000 ( 23.080667)
ary.first that is 21.770000 0.260000 22.030000 ( 30.326090)
def that proc=3Dnil, &block
p =3D proc.nil? ? block : proc

p =3D proc || block
def is message
lambda { |x| x.send "#{message}?".to_sym }

no need for to_sym at all.
def first func=3Dnil
return old_first if func.nil?
each do |x|
return x if func.call x
end

Any reason why you're not using find?=20

Also, if you don't match, you're going to return self, which would be =
wrong imo.
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

Thanks for the helpful feedback.

All,
I've been playing with some code to make querying an array more readable.
Basically, it lets you write one of the following:

puts [1,2,3].first that {|x| x.even? }
puts [1,2,3].first that is :even

instead of this:

puts [1,2,3].find {|x| x.even? }

The code is below. I'm curious whether you'd find it useful, interesting,
worthwhile, etc., and if there are improvements you can think of. Either
way, it was a fun little exercise. I love the flexibility I get from writing
in ruby!

I doubt I would ever want to use this. The amount of readability it adds is
completely offset by how much slower it is:

# of iterations = 1000000
user system total real
null_time 0.090000 0.000000 0.090000 ( 0.095464)
ary.find 1.680000 0.010000 1.690000 ( 1.717772)
ary.first that 18.580000 1.140000 19.720000 ( 23.080667)
ary.first that is 21.770000 0.260000 22.030000 ( 30.326090)
def that proc=nil, &block
p = proc.nil? ? block : proc

p = proc || block
def is message
lambda { |x| x.send "#{message}?".to_sym }

no need for to_sym at all.
def first func=nil
return old_first if func.nil?
each do |x|
return x if func.call x
end

Any reason why you're not using find?

Also, if you don't match, you're going to return self, which would be wrong
imo.
 
W

w_a_x_man

[Note:  parts of this message were removed to make it a legal post.]

All,
I've been playing with some code to make querying an array more readable.
Basically, it lets you write one of the following:

puts [1,2,3].first that {|x| x.even? }
puts [1,2,3].first that is :even

instead of this:

puts [1,2,3].find {|x| x.even? }

The code is below. I'm curious whether you'd find it useful, interesting,
worthwhile, etc., and if there are improvements you can think of. Either
way, it was a fun little exercise. I love the flexibility I get from writing
in ruby!

module Kernel
  def that proc=nil, &block
    p = proc.nil? ? block : proc
    lambda { |x| x.instance_eval &p }
  end

  def is message
    lambda { |x| x.send "#{message}?".to_sym }
  end
end

class Array
  alias :eek:ld_first first

  def first func=nil
    return old_first if func.nil?
    each do |x|
      return x if func.call x
    end
    nil
  end
end

Which is more readable?

2 + 2 = 4
Two added to two is equal to four.

And if you're interested in readability, why do you say
lambda instead of proc?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top