|
| 1 | +(* Copyright (C) 2017 Authors of BuckleScript |
| 2 | + * |
| 3 | + * This program is free software: you can redistribute it and/or modify |
| 4 | + * it under the terms of the GNU Lesser General Public License as published by |
| 5 | + * the Free Software Foundation, either version 3 of the License, or |
| 6 | + * (at your option) any later version. |
| 7 | + * |
| 8 | + * In addition to the permissions granted to you by the LGPL, you may combine |
| 9 | + * or link a "work that uses the Library" with a publicly distributed version |
| 10 | + * of this file to produce a combined library or application, then distribute |
| 11 | + * that combined work under the terms of your choosing, with no requirement |
| 12 | + * to comply with the obligations normally placed on you by section 4 of the |
| 13 | + * LGPL version 3 (or the corresponding section of a later version of the LGPL |
| 14 | + * should you choose to use a later version). |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Lesser General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Lesser General Public License |
| 22 | + * along with this program; if not, write to the Free Software |
| 23 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) |
| 24 | + |
| 25 | + |
| 26 | +let getExn = function |
| 27 | + | Some x -> x |
| 28 | + | None -> [%assert "getExn"] |
| 29 | + |
| 30 | +let mapWithDefaultU opt default f = match opt with |
| 31 | + | Some x -> (f x [@bs]) |
| 32 | + | None -> default |
| 33 | + |
| 34 | +let mapWithDefault opt default f = mapWithDefaultU opt default (fun[@bs] x -> f x) |
| 35 | + |
| 36 | +let mapU opt f = match opt with |
| 37 | + | Some x -> Some (f x [@bs]) |
| 38 | + | None -> None |
| 39 | + |
| 40 | +let map opt f = mapU opt (fun[@bs] x -> f x) |
| 41 | + |
| 42 | +let flatMapU opt f = match opt with |
| 43 | + | Some x -> (f x [@bs]) |
| 44 | + | None -> None |
| 45 | + |
| 46 | +let flatMap opt f = flatMapU opt (fun[@bs] x -> f x) |
| 47 | + |
| 48 | +let getWithDefault opt default = match opt with |
| 49 | + | Some x -> x |
| 50 | + | None -> default |
| 51 | + |
| 52 | +let isSome = function |
| 53 | + | Some _ -> true |
| 54 | + | None -> false |
| 55 | + |
| 56 | +let isNone = function |
| 57 | + | Some _ -> false |
| 58 | + | None -> true |
| 59 | + |
| 60 | +let eqU a b f = match (a, b) with |
| 61 | + | (Some a, Some b) -> f a b [@bs] |
| 62 | + | (None, Some _) |
| 63 | + | (Some _, None) -> false |
| 64 | + | (None, None) -> true |
| 65 | + |
| 66 | +let eq a b f = eqU a b (fun[@bs] x y -> f x y) |
| 67 | + |
| 68 | +let cmpU a b f = match (a, b) with |
| 69 | + | (Some a, Some b) -> f a b [@bs] |
| 70 | + | (None, Some _) -> -1 |
| 71 | + | (Some _, None) -> 1 |
| 72 | + | (None, None) -> 0 |
| 73 | + |
| 74 | +let cmp a b f = cmpU a b (fun[@bs] x y -> f x y) |
0 commit comments