|
2227 | 2227 | impl: maybeToNullable
|
2228 | 2228 | };
|
2229 | 2229 |
|
| 2230 | + //# maybeToLeft :: b -> Maybe a -> Either a b |
| 2231 | + //. |
| 2232 | + //. Converts a Maybe to an Either. Nothing becomes a Right (containing the |
| 2233 | + //. first argument); a Just becomes a Left. |
| 2234 | + //. |
| 2235 | + //. See also [`eitherToMaybe`](#eitherToMaybe) and |
| 2236 | + // [`maybeToRight`](#maybeToRight). |
| 2237 | + //. |
| 2238 | + //. ```javascript |
| 2239 | + //. > S.maybeToLeft ('No negative numbers') (S.find (S.lt (0)) ([0, 1, 2])) |
| 2240 | + //. Right ('No negative numbers') |
| 2241 | + //. |
| 2242 | + //. > S.maybeToLeft ('No negative numbers') (S.find (S.lt (0)) ([-1, 0, 1])) |
| 2243 | + //. Left (-1) |
| 2244 | + //. ``` |
| 2245 | + function maybeToLeft(x) { |
| 2246 | + return maybe (Right (x)) (Left); |
| 2247 | + } |
| 2248 | + _.maybeToLeft = { |
| 2249 | + consts: {}, |
| 2250 | + types: [b, $.Maybe (a), $.Either (a) (b)], |
| 2251 | + impl: maybeToLeft |
| 2252 | + }; |
| 2253 | + |
| 2254 | + //# maybeToRight :: a -> Maybe b -> Either a b |
| 2255 | + //. |
| 2256 | + //. Converts a Maybe to an Either. Nothing becomes a Left (containing the |
| 2257 | + //. first argument); a Just becomes a Right. |
| 2258 | + //. |
| 2259 | + //. See also [`eitherToMaybe`](#eitherToMaybe) and |
| 2260 | + // [`maybeToLeft`](#maybeToLeft). |
| 2261 | + //. |
| 2262 | + //. ```javascript |
| 2263 | + //. > S.maybeToRight ('Expecting an integer') (S.parseInt (10) ('xyz')) |
| 2264 | + //. Left ('Expecting an integer') |
| 2265 | + //. |
| 2266 | + //. > S.maybeToRight ('Expecting an integer') (S.parseInt (10) ('42')) |
| 2267 | + //. Right (42) |
| 2268 | + //. ``` |
| 2269 | + function maybeToRight(x) { |
| 2270 | + return maybe (Left (x)) (Right); |
| 2271 | + } |
| 2272 | + _.maybeToRight = { |
| 2273 | + consts: {}, |
| 2274 | + types: [a, $.Maybe (b), $.Either (a) (b)], |
| 2275 | + impl: maybeToRight |
| 2276 | + }; |
| 2277 | + |
2230 | 2278 | //. ### Either
|
2231 | 2279 | //.
|
2232 | 2280 | //. The Either type represents values with two possibilities: a value of type
|
|
2487 | 2535 | impl: encase
|
2488 | 2536 | };
|
2489 | 2537 |
|
| 2538 | + //# eitherToMaybe :: Either a b -> Maybe b |
| 2539 | + //. |
| 2540 | + //. Converts an Either to a Maybe. A Left becomes Nothing; a Right becomes |
| 2541 | + //. a Just. |
| 2542 | + //. |
| 2543 | + //. See also [`maybeToRight`](#maybeToRight). |
| 2544 | + //. |
| 2545 | + //. ```javascript |
| 2546 | + //. > S.eitherToMaybe (S.Left ('Cannot divide by zero')) |
| 2547 | + //. Nothing |
| 2548 | + //. |
| 2549 | + //. > S.eitherToMaybe (S.Right (42)) |
| 2550 | + //. Just (42) |
| 2551 | + //. ``` |
| 2552 | + function eitherToMaybe(either) { |
| 2553 | + return either.isLeft ? Nothing : Just (either.value); |
| 2554 | + } |
| 2555 | + _.eitherToMaybe = { |
| 2556 | + consts: {}, |
| 2557 | + types: [$.Either (a) (b), $.Maybe (b)], |
| 2558 | + impl: eitherToMaybe |
| 2559 | + }; |
| 2560 | + |
2490 | 2561 | //. ### Logic
|
2491 | 2562 |
|
2492 | 2563 | //# and :: Boolean -> Boolean -> Boolean
|
|
0 commit comments