Skip to content

Instantly share code, notes, and snippets.

@dbourguignon
Last active July 9, 2021 01:16
Show Gist options
  • Select an option

  • Save dbourguignon/1d7e94daaddd3b2e133ae1fe17aad92f to your computer and use it in GitHub Desktop.

Select an option

Save dbourguignon/1d7e94daaddd3b2e133ae1fe17aad92f to your computer and use it in GitHub Desktop.

Revisions

  1. dbourguignon revised this gist Dec 28, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions proc_composition-2.rb
    Original 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
  2. dbourguignon revised this gist Dec 28, 2018. 1 changed file with 14 additions and 24 deletions.
    38 changes: 14 additions & 24 deletions proc_composition-2.rb
    Original file line number Diff line number Diff line change
    @@ -1,34 +1,24 @@
    require "bigdecimal"

    # List of our individual pricing rules
    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) }
    }

    # Some helper to pick a rule
    def rule(name)
    PRICE_RULE_DEFINTIONS[name]
    end
    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 = %i(fee tax round_to_cent)
    PREMIUM = %i(fee premium tax round_to_cent)
    DISCOUNTED = %i(fee discount tax round_to_cent)
    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 price_calculator_for(rules:)
    rules.map(&method(:rule)).inject(:>>)
    def apply_rules(rules:, base_price:)
    rules.inject(:>>).call(base_price)
    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 "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
    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
  3. dbourguignon revised this gist Dec 28, 2018. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion proc_composition_1.rb
    Original 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
  4. dbourguignon revised this gist Dec 28, 2018. 1 changed file with 15 additions and 4 deletions.
    19 changes: 15 additions & 4 deletions proc_composition_1.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,17 @@
    add_title = ->(val) { "value: #{val}" }
    titleize = ->(val) { val.split(' ').map(&:capitalize).join(' ') }

    present = add_title >> titleize
    # 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 }

    puts present["hello world"] # => Val: Hello World
    # 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
  5. dbourguignon revised this gist Dec 23, 2018. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions proc_composition-2.rb
    Original 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 "regulat pay: #{regular.call(amount).to_f}"
    puts "premium pay: #{premium.call(amount).to_f}"
    puts "discounted pay: #{discounted.call(amount).to_f}"
    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
  6. dbourguignon renamed this gist Dec 23, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. dbourguignon revised this gist Dec 23, 2018. 2 changed files with 27 additions and 0 deletions.
    27 changes: 27 additions & 0 deletions proc-composition-2.rb
    Original 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.
  8. dbourguignon created this gist Dec 23, 2018.
    6 changes: 6 additions & 0 deletions proc_composition_simple1.rb
    Original 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