sanctuary-js / sanctuary

:see_no_evil: Refuge from unsafe JavaScript
https://sanctuary.js.org
MIT License
3.03k stars 94 forks source link

Why are `prop` and `props` throwing? #616

Closed amaury1093 closed 5 years ago

amaury1093 commented 5 years ago

This is more a question, for me learning FP. Why are the functions prop and props throwing an error when the property is not found in the object? This doesn't make prop mappable.

https://github.com/sanctuary-js/sanctuary/blob/ecde7d4a48e93397d56078ea606ba2c4dcd26541/index.js#L3838-L3845

I would have expected a Maybe. fwiw, Ramda returns a | undefined.

Avaq commented 5 years ago

Hello and welcome, @amaurymartiny !

Conceptually, prop and props only operate on records. These are types like the following:

{ name :: String, dob :: Date }

You could use prop ('name') on this value safely, because it must have a name. And you want it to throw if the name is missing, because that would actually be a bug.

What you might be looking for instead are the following functions:

Conceptually, they operate mainly on string maps and other types whose keys are not known beforehand. They can even be used to operate on types whose values are unknown beforehand, by using the first argument: a predicate to ensure the gotten value conforms to expectations.