Skip to content

Commit 8014b43

Browse files
committed
feat(option): add zip
1 parent 16e4a84 commit 8014b43

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/Core__Option.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ function cmp(a, b, f) {
128128
}
129129
}
130130

131+
function zip(a, b) {
132+
if (a !== undefined && b !== undefined) {
133+
return [
134+
Caml_option.valFromOption(a),
135+
Caml_option.valFromOption(b)
136+
];
137+
}
138+
139+
}
140+
131141
export {
132142
some ,
133143
flat ,
@@ -144,5 +154,6 @@ export {
144154
isNone ,
145155
eq ,
146156
cmp ,
157+
zip ,
147158
}
148159
/* No side effect */

src/Core__Option.res

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,9 @@ let cmpU = (a, b, f) =>
125125
}
126126

127127
let cmp = (a, b, f) => cmpU(a, b, (. x, y) => f(x, y))
128+
129+
let zip = (a, b) =>
130+
switch (a, b) {
131+
| (Some(a), Some(b)) => Some(a, b)
132+
| _ => None
133+
}

src/Core__Option.resi

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,15 @@ cmp(None, None, clockCompare) // 0
290290
```
291291
*/
292292
let cmp: (option<'a>, option<'b>, ('a, 'b) => int) => int
293+
294+
/**
295+
`zip(a, b)` combines the values contained in `a` and `b` if both are `Some`, returns `None` otherwise..
296+
297+
## Examples
298+
299+
```rescript
300+
Option.zip(Some(1), Some(2)) // Some(1, 2)
301+
Option.zip(Some(1), None) // None
302+
```
303+
*/
304+
let zip: (option<'a>, option<'b>) => option<('a, 'b)>

0 commit comments

Comments
 (0)