Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This code executes, where as per the book it should not execute - Ch08, Use Cases #639

Open
technoknol opened this issue Jan 31, 2023 · 1 comment

Comments

@technoknol
Copy link

technoknol commented Jan 31, 2023

I have used this code in ramda for practice and code looks like below:

import { curry, map, compose, prop }  from 'ramda'
import M  from 'ramda-fantasy';
const Maybe = M.Maybe
const Just = Maybe.Just;
const Nothing = Maybe.Nothing;

// withdraw :: Number -> Account -> Maybe(Account)
const withdraw = curry((amount, { balance }) =>
  Maybe.of(balance >= amount ? { balance: balance - amount } : null));

// This function is hypothetical, not implemented here... nor anywhere else.
// updateLedger :: Account -> Account 
const updateLedger = account => account;

// remainingBalance :: Account -> String
const remainingBalance = ({ balance }) => `Your balance is $${balance}`;

// finishTransaction :: Account -> String
const finishTransaction = compose(remainingBalance, updateLedger);

// getTwenty :: Account -> Maybe(String)
const getTwenty = compose(map(finishTransaction), withdraw(20));

const r = getTwenty({ balance: 200.00 }); 
console.log({r});
// Just('Your balance is $180')

const t = getTwenty({ balance: 10.00 });
console.log({t});
// Nothing

last statement const t throws an error like below.

Cannot destructure property 'balance' of 'object null' as it is null.

Can anyone help me write a code so that when withdraw fails finishTransaction will never execute

image

@technoknol
Copy link
Author

I was able to fix this by changing iwthdraw like this:

  const withdraw = curry((amount, { balance }) =>
  balance >= amount ? Maybe.of({ balance: balance - amount }) : Nothing());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant