Last active
October 15, 2021 20:34
-
-
Save tgrecojs/6cdeb1a013df616dbb21d880103bdd91 to your computer and use it in GitHub Desktop.
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
// Better way | |
const getValue = ({value}) => value; | |
const add = x => y => x + y; | |
const addTen = add(10); | |
const multiply = x => y => x * y; | |
const displayValue = string => value => `${string}::${value}`; | |
const displayLeverageRatio = displayValue('Leverage Ratio'); | |
const packageValueWithCaption = caption => value => ({caption, value}) | |
const calculateValueAndCreateObject = compose( | |
packageValueWithCaption("Some Value:: "), | |
multiply(2), | |
getValue | |
) | |
calculateValueAndCreateObject({value:1000}) //? ({caption: Some Value, value: 200000}) | |
const calculateSomeVal = compose( | |
displayValue("Some Value:: "), | |
multiply(2), | |
getValue | |
) | |
calculateSomeVal({value:10000}) //? S |
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
const compose = (...fns) => initialValue => fns.reduceRight((val, fn) => fn(val), initialValue) | |
const dummyWrite = (string) => `${string}`; | |
const writeString = ({ string }) => `${string}`; | |
const writeValueWithCaption = ({ calculationForUi, caption }) => | |
`${caption}:: ${calculationForUi}`; | |
const trace = (label) => (value) => { | |
console.log(label + "::", value); | |
return value; | |
}; | |
const multiplyValueByTwo = ({ value }) => value * 2; | |
const subtractByTwo = ({value}) => value - 2; | |
subtractByTwo(1000) //? | |
subtractByTwo({value: 1000}) //? | |
const composedWriteFn = | |
compose( | |
writeValueWithCaption, | |
(x) => ({ ...x, caption: writeString(x) }), | |
(x) => ({ ...x, calculationForUi: x.fn(x) }) //? | |
); | |
composedWriteFn({value: 10000, string: 'new notional value', fn: multiplyValueByTwo}) //? | |
composedWriteFn({ value: 2.8, string: "current leverage ratio", fn: subtractByTwo }); //? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment