Skip to content

Instantly share code, notes, and snippets.

@gurgelrenan
Forked from thatrubylove/6.rb
Last active December 30, 2015 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gurgelrenan/7824171 to your computer and use it in GitHub Desktop.
Save gurgelrenan/7824171 to your computer and use it in GitHub Desktop.
#Some examples with lambdas
sum = ->(num_list) { num_list.reduce(:+) }
square = ->(number ) { number * number }
squares = ->(num_list) { num_list.map {|num| square.(num) } }
sum_squares = ->(num_list) { sum.(squares.(num_list)) }
square_sum = ->(num_list) { square.(sum.(num_list)) }
gem 'minitest'
require 'minitest/autorun'
describe "sum_squares" do
it { assert_equal 385, sum_squares.(1..10) }
end
describe "square_sum" do
it { assert_equal 3025, square_sum.(1..10) }
end
describe "answer to the solution" do
it { assert_equal 25164150, square_sum.(1..100) - sum_squares.(1..100) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment