Skip to content

Instantly share code, notes, and snippets.

@tgrecojs
Last active October 15, 2021 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgrecojs/6cdeb1a013df616dbb21d880103bdd91 to your computer and use it in GitHub Desktop.
Save tgrecojs/6cdeb1a013df616dbb21d880103bdd91 to your computer and use it in GitHub Desktop.
// 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
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