Last active
July 9, 2021 01:16
-
-
Save dbourguignon/1d7e94daaddd3b2e133ae1fe17aad92f to your computer and use it in GitHub Desktop.
Revisions
-
dbourguignon revised this gist
Dec 28, 2018 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -6,6 +6,7 @@ PREMIUM = ->(val) { val + 10 } DISCOUNT = ->(val) { val * 0.90 } ROUND_TO_CENT = ->(val) { val.round(2) } # One presenter PRESENT = ->(val) { val.to_f } # Pre-define some rule set for some pricing scenarios -
dbourguignon revised this gist
Dec 28, 2018 . 1 changed file with 14 additions and 24 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,34 +1,24 @@ require "bigdecimal" # List of our individual pricing rules TAX = ->(val) { val + val*0.05 } FEE = ->(val) { val + 1 } PREMIUM = ->(val) { val + 10 } DISCOUNT = ->(val) { val * 0.90 } ROUND_TO_CENT = ->(val) { val.round(2) } PRESENT = ->(val) { val.to_f } # Pre-define some rule set for some pricing scenarios REGULAR_SET = [FEE, TAX, ROUND_TO_CENT, PRESENT] PREMIUM_SET = [FEE, PREMIUM, TAX, ROUND_TO_CENT, PRESENT] DISCOUNTED_SET = [FEE, DISCOUNT, TAX, ROUND_TO_CENT, PRESENT] def apply_rules(rules:, base_price:) rules.inject(:>>).call(base_price) end amount = BigDecimal(100) puts "regular: #{apply_rules(rules: REGULAR_SET, base_price: amount)}" # => 106.05 puts "premium: #{apply_rules(rules: PREMIUM_SET, base_price: amount)}" # => 116.55 puts "discounted: #{apply_rules(rules: DISCOUNTED_SET, base_price: amount)}" # => 95.45 -
dbourguignon revised this gist
Dec 28, 2018 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ # This lambda take one argument and return the same prefixed by "hello " greet = ->(val) { "hello #{val}" } # This lambda take one argument and return the upcased version -
dbourguignon revised this gist
Dec 28, 2018 . 1 changed file with 15 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,17 @@ # This lambda take one argument and return the same prefixed by "hello " greet = ->(val) { "hello #{val}" } # This lambda take one argument and return the upcased version upper = ->(val) { val.upcase } # So you can do puts greet["world"] # => hello world puts upper["world"] # => WORLD present = greet >> upper puts present["world"] # => HELLO WORLD present = greet << upper puts present["world"] # => hello WORLD -
dbourguignon revised this gist
Dec 23, 2018 . 1 changed file with 10 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,6 @@ require "bigdecimal" # List of our individual pricing rules PRICE_RULE_DEFINTIONS = { tax: ->(val) { val + val*0.05 }, fee: ->(val) { val + 1 }, @@ -8,10 +9,16 @@ round_to_cent: ->(val) { val.round(2) } } # Some helper to pick a rule def rule(name) PRICE_RULE_DEFINTIONS[name] end # Pre-define some rule set for some pricing scenarios REGULAR = %i(fee tax round_to_cent) PREMIUM = %i(fee premium tax round_to_cent) DISCOUNTED = %i(fee discount tax round_to_cent) def price_calculator_for(rules:) rules.map(&method(:rule)).inject(:>>) end @@ -22,6 +29,6 @@ def price_calculator_for(rules:) amount = BigDecimal(100) puts "regular pay: #{regular.call(amount).to_f}" # => 106.05 puts "premium pay: #{premium.call(amount).to_f}" # => 116.55 puts "discounted pay: #{discounted.call(amount).to_f}" # => 95.45 -
dbourguignon renamed this gist
Dec 23, 2018 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
dbourguignon revised this gist
Dec 23, 2018 . 2 changed files with 27 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ require "bigdecimal" PRICE_RULE_DEFINTIONS = { tax: ->(val) { val + val*0.05 }, fee: ->(val) { val + 1 }, premium: ->(val) { val + 10 }, discount: ->(val) { val * 0.90 }, round_to_cent: ->(val) { val.round(2) } } def rule(name) PRICE_RULE_DEFINTIONS[name] end def price_calculator_for(rules:) rules.map(&method(:rule)).inject(:>>) end regular = price_calculator_for(rules: %i(fee tax round_to_cent)) premium = price_calculator_for(rules: %i(fee premium tax round_to_cent)) discounted = price_calculator_for(rules: %i(fee discount tax round_to_cent)) amount = BigDecimal(100) puts "regulat pay: #{regular.call(amount).to_f}" puts "premium pay: #{premium.call(amount).to_f}" puts "discounted pay: #{discounted.call(amount).to_f}" File renamed without changes. -
dbourguignon created this gist
Dec 23, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ add_title = ->(val) { "value: #{val}" } titleize = ->(val) { val.split(' ').map(&:capitalize).join(' ') } present = add_title >> titleize puts present["hello world"] # => Val: Hello World