Skip to content

Instantly share code, notes, and snippets.

@JonRowe
Forked from fables-tales/gist:6436357
Last active December 22, 2015 07:19
Show Gist options
  • Save JonRowe/6437213 to your computer and use it in GitHub Desktop.
Save JonRowe/6437213 to your computer and use it in GitHub Desktop.
1.9.3p448 :002 > a = [-> { puts 1; true }, -> { puts 2; false}, -> { puts 3; false }]
=> [#<Proc:0x007fe5c41a8f68@(irb):2 (lambda)>, #<Proc:0x007fe5c41a8f18@(irb):2 (lambda)>, #<Proc:0x007fe5c41a8ef0@(irb):2 (lambda)>]
1.9.3p448 :003 > a.all?(&:call)
1
2
=> false
1.9.3p448 :004 > a = [-> { puts 1; false }, -> { puts 2; true}, -> { puts 3; false }]
=> [#<Proc:0x007fe5c4276620@(irb):4 (lambda)>, #<Proc:0x007fe5c42765d0@(irb):4 (lambda)>, #<Proc:0x007fe5c42765a8@(irb):4 (lambda)>]
1.9.3p448 :005 > a.any?(&:call)
1
2
=> true
require 'benchmark'
computed = 'aaaa_aaaaaa'
presented = 'aaaaaaaaaaa'
count_a, count_b = 0, 0
computed.chars.zip(presented.chars).map {|x,y| x == y }.all? { |c| count_a += 1; c }
computed.chars.zip(presented.chars).inject(true) { |value, (x,y)| count_b += 1; value && (x == y) }
puts "overall #{presented.size}"
puts "map.all? #{count_a}"
puts "inject #{count_b}"
# =>
# overall 11
# map.all? 5
# inject 11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment