sanctuary-js / sanctuary

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

array: add S.findMaybe #697

Closed davidchambers closed 3 years ago

davidchambers commented 3 years ago

With find one sometimes applies a transformation to determine whether an element is suitable only to apply the transformation a second time if a suitable element is found:

> S.chain (S.parseInt (16))
.         (S.find (S.compose (S.isJust) (S.parseInt (16)))
.                 (['A', 'B', 'C']))
Just (10)

With findMaybe the transformation is provided just once:

> S.findMaybe (S.parseInt (16)) (['A', 'B', 'C'])
Just (10)

As is often the case, the function's type is obvious but its name is not. What do you think of the name findMaybe? It follows the precedent set by map and mapMaybe, although the correspondence is imprecise.

davidchambers commented 3 years ago

Thanks for the review, Aldwin. I will leave this pull request open for a few days to give others the opportunity to comment.

gabejohnson commented 3 years ago

In PureScript, this function is called findMap.

There's another (more general) function oneOfMap which uses some Plus f instead of Maybe. That would probably be less useful to the average user though.

davidchambers commented 3 years ago

Thanks, Gabe! findMap is a good name. Better than findMaybe, do you think? I am not attached to findMaybe.

gabejohnson commented 3 years ago

I like findMap because it suggests that you're likely modifying the value you find. I don't have any real attachment though, just thought it useful to point toward some prior art.

davidchambers commented 3 years ago

Superseded by #698