monet / monet.js

monet.js - Monadic types library for JavaScript
https://monet.github.io/monet.js/
MIT License
1.6k stars 114 forks source link

Error on "List -> find" with single value of null #245

Open ronnelreposo opened 3 years ago

ronnelreposo commented 3 years ago
const xs = [null];
List.fromArray(xs).find((x) => true);

Finding an element with x being null throws an error.

https://codesandbox.io/s/zen-morse-fk96k?file=/src/index.js

ulfryk commented 3 years ago

Hi @ronnelreposo thanks for pointing that out - but it seems that it's an expected behaviour of this library due to some initial decisions made about Maybe monad.

Please see this discussion https://github.com/monet/monet.js/issues/53 especially https://github.com/monet/monet.js/issues/53#issuecomment-212276496

Also you can check https://github.com/monet/monet.js/issues/208

ronnelreposo commented 3 years ago

Hi @ulfryk, thank you for your response. I was also expecting to return None / Nothing, but as I saw the discussions (specially #208), it seems that the only way to avoid this is to sanitize the inputs, filtering all the null/undefined. e.g.

const xs = [null].filter(x => !(x == null || x == undefined))
List.fromArray(xs).find((x) => true);
ulfryk commented 3 years ago

@ronnelreposo - yes .filter(x => x != null) is best you can do.

But maybe we can add .findOptional or sth like this to the List to handle that special ( and probably quite common ) case.