This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
waysToChange :: Int -> [Int] -> Int | |
waysToChange 0 _ = 1 | |
waysToChange _ [] = 0 | |
waysToChange amount denominations@(denomination:xs) | |
| amount < 0 = 0 | |
| otherwise = | |
(waysToChange (amount - denomination) denominations) + | |
(waysToChange amount xs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActiveRecordScopeExtensions | |
def self.union(left, right) | |
active_record_klass = left.klass | |
arel_union = [left, right].map do |scope| | |
scope.select("#{active_record_klass.table_name}.id") | |
end.reduce(&:union) | |
temporary_table_name = TemporaryTableName.new([active_record_klass.table_name, "union"].join("_")).to_s | |
active_record_klass.joins("INNER JOIN (#{arel_union.to_sql}) as #{temporary_table_name} ON #{temporary_table_name}.id = #{active_record_klass.table_name}.id") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[3] pry(main)> ActiveRecordScopeExtensions.union(User.where(name: "Bob"), User.where(name: "Alice")).to_sql | |
=> "SELECT \"users\".* FROM \"users\" INNER JOIN (( SELECT users.id FROM \"users\" WHERE \"users\".\"name\" = 'Bob' UNION SELECT users.id FROM \"users\" WHERE \"users\".\"name\" = 'Alice' )) as users_union_b96cf9af7d77c501088feac76f259ba6 ON users_union_b96cf9af7d77c501088feac76f259ba6.id = users.id" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[6] pry(main)> left = User.where(name: "Bill") | |
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."name" = $1 [["name", "Bill"]] | |
=> [] | |
[7] pry(main)> $ left.union | |
Error: Couldn't locate a definition for left.union! | |
[8] pry(main)> left.respond_to?(:union) | |
=> true | |
[9] pry(main)> $ left.method_missing | |
From: /Users/pdswan/code/scratch/rails4_union/rails4/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/relation/delegation.rb @ line 91: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[14] pry(main)> left.arel.to_sql | |
=> "SELECT \"users\".* FROM \"users\" WHERE \"users\".\"name\" = $1" | |
[15] pry(main)> left.to_sql | |
=> "SELECT \"users\".* FROM \"users\" WHERE \"users\".\"name\" = 'Bill'" | |
[16] pry(main)> |
OlderNewer