Skip to content

Option additions #57

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(option): add flat
  • Loading branch information
glennsl committed Mar 7, 2023
commit e300c0fdf63e7fa7bd58c1696494fa108e260ac4
8 changes: 8 additions & 0 deletions src/Core__Option.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
import * as Curry from "rescript/lib/es6/curry.js";
import * as Caml_option from "rescript/lib/es6/caml_option.js";

function flat(opt) {
if (opt !== undefined) {
return Caml_option.valFromOption(opt);
}

}

function filter(opt, p) {
var p$1 = Curry.__1(p);
if (opt !== undefined && p$1(Caml_option.valFromOption(opt))) {
Expand Down Expand Up @@ -107,6 +114,7 @@ function cmp(a, b, f) {
}

export {
flat ,
filter ,
forEach ,
getExn ,
Expand Down
6 changes: 6 additions & 0 deletions src/Core__Option.res
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

let flat = opt =>
switch opt {
| Some(v) => v
| None => None
}

let filterU = (opt, p) =>
switch opt {
| Some(x) as some if p(. x) => some
Expand Down
12 changes: 12 additions & 0 deletions src/Core__Option.resi
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ let someString: option<string> = Some("hello")
```
*/

/**
`flat(value)` flattens a nested `option` value to a single level..

## Examples

```rescript
Option.float(Some(Some(10))) // Some(10)
Option.flat(Some(None)) // None
```
*/
let flat: option<option<'a>> => option<'a>

/**
`filter(opt, f)` applies `f` to `opt`, if `f` returns `true`, then it returns `Some(value)`, otherwise returns `None`.

Expand Down