Skip to content

Instantly share code, notes, and snippets.

@guaracy
Created January 21, 2019 19:12
Show Gist options
  • Save guaracy/b7d10bdb38b9ac14fb8ef576e5c38dff to your computer and use it in GitHub Desktop.
Save guaracy/b7d10bdb38b9ac14fb8ef576e5c38dff to your computer and use it in GitHub Desktop.
https://drivy.engineering/ruby-lambda-composition/
Red []
GREET: func[val][rejoin ["hello " val]]
UPPER: func[val][uppercase val]
print GREET "world"
print UPPER "world"
PRESENT: func[val][upper greet val]
print PRESENT "world"
PRESENT: func[val][greet upper val]
print PRESENT "world"
TAX: func[val][val * 5% + val]
FEE: func[val][val + 1]
PREMIUM: func[val][val + 10]
DISCOUNT: func[val][val * 90%]
ROUND-TO-CENT: func[val][round/to val 0.01]
PRESENT: func[val][pad/left val 7]
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]
apply-rules: func[b v][
do append reverse copy b v
]
amount: 100
print["regular....:" apply-rules REGULAR-SET amount] ; 106.05
print["premium....:" apply-rules PREMIUM-SET amount] ; 116.55
print["discounted.:" apply-rules DISCOUNTED-SET amount] ; 95.45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment