From b1c0aab94c82a0ca226c3b1673c51556d2a01b04 Mon Sep 17 00:00:00 2001 From: kujon Date: Thu, 6 Sep 2018 17:05:29 +0100 Subject: [PATCH] Refmt 3.3.3 --- src/Applicative.re | 2 +- src/Dict.re | 50 +++++++------ src/Dict.rei | 8 +-- src/Function.re | 6 +- src/Lens.re | 42 +++++------ src/Logic.re | 8 ++- src/Monad.re | 45 ++++++------ src/Option.re | 66 ++++++++--------- src/Option.rei | 4 +- src/RList.re | 176 +++++++++++++++++++++++++++------------------ src/Result.re | 49 ++++++------- 11 files changed, 248 insertions(+), 208 deletions(-) diff --git a/src/Applicative.re b/src/Applicative.re index b6ffc73..238bbe3 100644 --- a/src/Applicative.re +++ b/src/Applicative.re @@ -1,6 +1,6 @@ module type Basic = { type t('a); - let apply: (t(('a => 'b)), t('a)) => t('b); + let apply: (t('a => 'b), t('a)) => t('b); let pure: 'a => t('a); }; diff --git a/src/Dict.re b/src/Dict.re index 3697848..0461a84 100644 --- a/src/Dict.re +++ b/src/Dict.re @@ -4,7 +4,7 @@ open Function.Infix; type t('a) = list((string, 'a)); -let findEntry = (k) => fst ||> Util.eq(k); +let findEntry = k => fst ||> Util.eq(k); let get = (k, d) => RList.find(findEntry(k), d) <$> snd; @@ -19,9 +19,9 @@ let unset = (k, d) => RList.reject(findEntry(k), d); let eqProps = (k, d0: t('a), d1: t('a)) => Option.(Some(Util.eq) <*> get(k, d0) <*> get(k, d1) |> default(false)); -let map = (f) => List.map(((k, v)) => (k, f(v))); +let map = f => List.map(((k, v)) => (k, f(v))); -let mapi = (f) => List.map(((k, v)) => (k, f(k, v))); +let mapi = f => List.map(((k, v)) => (k, f(k, v))); let evolve = (e, d) => mapi( @@ -30,28 +30,37 @@ let evolve = (e, d) => | None => v | Some(f) => f(v) }, - d + d, ); let has = (k, d) => get(k, d) <$> Function.true_ |> Option.default(false); -let invert = (d) => +let invert = d => List.fold_left( - (acc, (k, v)) => set(v, get(v, acc) <$> RList.append(k) |> Option.default([k]), acc), + (acc, (k, v)) => + set(v, get(v, acc) <$> RList.append(k) |> Option.default([k]), acc), [], - d + d, ); -let keys = (d) => List.map(fst, d); +let keys = d => List.map(fst, d); -let merge = (d0, d1) => List.fold_left((acc, (k, v)) => set(k, v, acc), d0, d1); +let merge = (d0, d1) => + List.fold_left((acc, (k, v)) => set(k, v, acc), d0, d1); let mergeWithKey = (f, d0, d1) => { let intersect = RList.intersection(keys(d0), keys(d1)); List.map( - (k) => Option.(k, Some(f(k)) <*> get(k, d0) <*> get(k, d1) |> toExn("Error merging Dicts")), - intersect - ) + k => + Option.( + k, + Some(f(k)) + <*> get(k, d0) + <*> get(k, d1) + |> toExn("Error merging Dicts"), + ), + intersect, + ); }; let mergeWith = (f, d0, d1) => mergeWithKey(Function.always(f), d0, d1); @@ -60,25 +69,26 @@ let omit = (ks, d) => RList.reject(((k, _)) => RList.contains(k, ks), d); let pickBy = (pred, d) => List.filter(((k, v)) => pred(k, v), d); -let pick = (ks) => pickBy((k, _) => RList.contains(k, ks)); +let pick = ks => pickBy((k, _) => RList.contains(k, ks)); let project = (ks, ds) => List.map(pick(ks), ds); -let values = (d) => List.map(snd, d); +let values = d => List.map(snd, d); let where = (predD, d) => List.fold_left( - (acc, (k, f)) => acc ? Option.(Some(f) <*> get(k, d) |> default(false)) : false, + (acc, (k, f)) => + acc ? Option.(Some(f) <*> get(k, d) |> default(false)) : false, true, - predD + predD, ); let whereEq = (d0, d1) => map(Util.eq, d0) |> Function.flip(where, d1); -let filter = (f) => List.filter(((_, v)) => f(v)) +let filter = f => List.filter(((_, v)) => f(v)); -let filteri = (f) => List.filter(((k, v)) => f(k, v)) +let filteri = f => List.filter(((k, v)) => f(k, v)); -let fold_left = (f) => List.fold_left((acc, (k, v)) => f(acc, k, v)) +let fold_left = f => List.fold_left((acc, (k, v)) => f(acc, k, v)); -let fold_right = (f) => List.fold_right(((k, v), acc) => f(k, v, acc)) \ No newline at end of file +let fold_right = f => List.fold_right(((k, v), acc) => f(k, v, acc)); \ No newline at end of file diff --git a/src/Dict.rei b/src/Dict.rei index 5dd1159..001af84 100644 --- a/src/Dict.rei +++ b/src/Dict.rei @@ -12,7 +12,7 @@ let map: ('a => 'b, t('a)) => t('b); let mapi: ((string, 'a) => 'b, t('a)) => t('b); -let evolve: (t(('a => 'a)), t('a)) => t('a); +let evolve: (t('a => 'a), t('a)) => t('a); let has: (string, t('a)) => bool; @@ -36,7 +36,7 @@ let project: (list(string), list(t('a))) => list(t('a)); let values: t('a) => list('a); -let where: (t(('a => bool)), t('a)) => bool; +let where: (t('a => bool), t('a)) => bool; let whereEq: (t('a), t('a)) => bool; @@ -44,6 +44,6 @@ let filter: ('a => bool, t('a)) => t('a); let filteri: ((string, 'a) => bool, t('a)) => t('a); -let fold_left: (('a, string, 'b) => 'a, 'a, t('b)) => 'a +let fold_left: (('a, string, 'b) => 'a, 'a, t('b)) => 'a; -let fold_right: ((string, 'a, 'b) => 'b, t('a), 'b ) => 'b \ No newline at end of file +let fold_right: ((string, 'a, 'b) => 'b, t('a), 'b) => 'b; \ No newline at end of file diff --git a/src/Function.re b/src/Function.re index 70a702f..f6cd707 100644 --- a/src/Function.re +++ b/src/Function.re @@ -4,13 +4,13 @@ let compose = (f, g, x) => f(g(x)); let pipe = (f, g, x) => g(f(x)); -let false_ = (x) => always(false, x); +let false_ = x => always(false, x); -let true_ = (x) => always(true, x); +let true_ = x => always(true, x); let flip = (f, a, b) => f(b, a); -let identity = (x) => x; +let identity = x => x; module Infix = { let (<||) = compose; diff --git a/src/Lens.re b/src/Lens.re index 1302e62..c7c6a3d 100644 --- a/src/Lens.re +++ b/src/Lens.re @@ -1,7 +1,7 @@ /* Inspired by: https://github.com/avsm/ocaml-lens */ type t('a, 'b) = { get: 'a => 'b, - set: ('b, 'a) => 'a + set: ('b, 'a) => 'a, }; let make = (getter, setter) => {get: getter, set: setter}; @@ -12,10 +12,11 @@ let set = (l, v, a) => l.set(v, a); let over = (l, f, a) => { let v = l.get(a); - l.set(f(v), a) + l.set(f(v), a); }; -let compose = (l0, l1) => Function.{get: l1.get ||> l0.get, set: l0.set ||> over(l1)}; +let compose = (l0, l1) => + Function.{get: l1.get ||> l0.get, set: l0.set ||> over(l1)}; let (-<<) = compose; @@ -23,52 +24,53 @@ let pipe = (l0, l1) => compose(l1, l0); let (>>-) = pipe; -let optional = (default) => { - get: (a) => - switch a { +let optional = default => { + get: a => + switch (a) { | Some(v) => v | None => default }, - set: (v, _) => Some(v) + set: (v, _) => Some(v), }; let head = { get: RList.head, set: (v, xs) => - switch v { + switch (v) { | None => xs - | Some(a) => RList.tail(xs) |> Option.default([]) |> ((tl) => [a, ...tl]) - } + | Some(a) => RList.tail(xs) |> Option.default([]) |> (tl => [a, ...tl]) + }, }; let tail = { get: RList.tail, set: (v, xs) => - switch v { + switch (v) { | None => xs - | Some(a) => Option.(RList.head(xs) <$> ((hd) => [hd, ...a]) |> default(a)) - } + | Some(a) => + Option.(RList.head(xs) <$> (hd => [hd, ...a]) |> default(a)) + }, }; -let index = (i) => { +let index = i => { get: RList.nth(i), set: (v, xs) => - switch v { + switch (v) { | None => xs | Some(a) => RList.update(a, i, xs) - } + }, }; -let prop = (k) => { +let prop = k => { get: Dict.get(k), set: (v, d) => - switch v { + switch (v) { | None => d | Some(a) => Dict.set(k, a, d) - } + }, }; -let listMap = (l) => {get: List.map(l.get), set: List.map2(l.set)}; +let listMap = l => {get: List.map(l.get), set: List.map2(l.set)}; let first = {get: fst, set: (v, a) => (v, snd(a))}; diff --git a/src/Logic.re b/src/Logic.re index 15d7792..4c88dff 100644 --- a/src/Logic.re +++ b/src/Logic.re @@ -1,14 +1,16 @@ -let allPass = (fs, x) => List.fold_left((acc, f) => acc ? f(x) : acc, true, fs); +let allPass = (fs, x) => + List.fold_left((acc, f) => acc ? f(x) : acc, true, fs); let and_ = (a, b) => a && b; -let anyPass = (fs, x) => List.fold_left((acc, f) => acc ? acc : f(x), false, fs); +let anyPass = (fs, x) => + List.fold_left((acc, f) => acc ? acc : f(x), false, fs); let both = (f, g, x) => f(x) && g(x); let either = (f, g, x) => f(x) || g(x); -let not_ = (f, x) => ! f(x); +let not_ = (f, x) => !f(x); let or_ = (a, b) => a || b; diff --git a/src/Monad.re b/src/Monad.re index c4663bb..33bbe1c 100644 --- a/src/Monad.re +++ b/src/Monad.re @@ -1,6 +1,8 @@ module type General = { type t('a, 'i, 'j, 'd, 'e); - let bind: (t('a, 'i, 'j, 'd, 'e), 'a => t('b, 'j, 'k, 'd, 'e)) => t('b, 'i, 'k, 'd, 'e); + let bind: + (t('a, 'i, 'j, 'd, 'e), 'a => t('b, 'j, 'k, 'd, 'e)) => + t('b, 'i, 'k, 'd, 'e); let fmap: [ | `DefineWithBind | `Custom(('a => 'b, t('a, 'i, 'j, 'd, 'e)) => t('b, 'i, 'j, 'd, 'e)) @@ -25,7 +27,10 @@ module type Basic2 = { type t('a, 'e); let bind: (t('a, 'e), 'a => t('b, 'e)) => t('b, 'e); let return: 'a => t('a, 'e); - let fmap: [ | `DefineWithBind | `Custom(('a => 'b, t('a, 'e)) => t('b, 'e))]; + let fmap: [ + | `DefineWithBind + | `Custom(('a => 'b, t('a, 'e)) => t('b, 'e)) + ]; }; module type Infix2 = { @@ -38,40 +43,36 @@ module MakeGeneral = (M: General) => { let bind = M.bind; let return = M.return; let fmap = - switch M.fmap { - | `DefineWithBind => ((f, m) => M.bind(m, (a) => M.return(f(a)))) + switch (M.fmap) { + | `DefineWithBind => ((f, m) => M.bind(m, a => M.return(f(a)))) | `Custom(f) => f }; let (>>=) = M.bind; let (<$>) = (m, f) => fmap(f, m); - let join = (m) => m >>= ((a) => a); + let join = m => m >>= (a => a); let all = { let rec loop = (vs, ms) => - switch ms { + switch (ms) { | [] => return(List.rev(vs)) - | [t, ...ts] => t >>= ((v) => loop([v, ...vs], ts)) + | [t, ...ts] => t >>= (v => loop([v, ...vs], ts)) }; - (ts) => loop([], ts) + ts => loop([], ts); }; - let rec all_ignore = (ms) => - switch ms { + let rec all_ignore = ms => + switch (ms) { | [] => return() | [t, ...ts] => t >>= (() => all_ignore(ts)) }; }; module MakeBasic = (M: Basic) => - MakeGeneral( - { - type t('a, 'i, 'j, 'd, 'e) = M.t('a); - include (M: Basic with type t('a) := M.t('a)); - } - ); + MakeGeneral({ + type t('a, 'i, 'j, 'd, 'e) = M.t('a); + include (M: Basic with type t('a) := M.t('a)); + }); module MakeBasic2 = (M: Basic2) => - MakeGeneral( - { - type t('a, 'i, 'j, 'd, 'e) = M.t('a, 'd); - include (M: Basic2 with type t('a, 'd) := M.t('a, 'd)); - } - ); \ No newline at end of file + MakeGeneral({ + type t('a, 'i, 'j, 'd, 'e) = M.t('a, 'd); + include (M: Basic2 with type t('a, 'd) := M.t('a, 'd)); + }); \ No newline at end of file diff --git a/src/Option.re b/src/Option.re index 791e62f..a163d5a 100644 --- a/src/Option.re +++ b/src/Option.re @@ -1,71 +1,65 @@ -let some = (a) => Some(a); +let some = a => Some(a); -let none = (_) => None; +let none = _ => None; -let isNone = (o) => - switch o { +let isNone = o => + switch (o) { | None => true | _ => false }; -let isSome = (o) => - switch o { +let isSome = o => + switch (o) { | Some(_) => true | _ => false }; let default = (d, o) => - switch o { + switch (o) { | None => d | Some(a) => a }; -let ofResult = (r) => +let ofResult = r => Belt.Result.( - switch r { + switch (r) { | Ok(a) => Some(a) | Error(_) => None } ); let toExn = (err, o) => - switch o { + switch (o) { | None => raise(Failure(err)) | Some(a) => a }; let firstSome = (a, b) => - switch a { + switch (a) { | None => b | _ => a }; -include - Monad.MakeBasic( - { - type t('a) = option('a); - let bind = (o, f) => - switch o { - | None => None - | Some(a) => f(a) - }; - let return = some; - let fmap = `DefineWithBind; - } - ); +include Monad.MakeBasic({ + type t('a) = option('a); + let bind = (o, f) => + switch (o) { + | None => None + | Some(a) => f(a) + }; + let return = some; + let fmap = `DefineWithBind; +}); -include - Applicative.MakeBasic( - { - type t('a) = option('a); - let apply = (o, a) => - switch o { - | Some(f) => bind(a, (b) => return(f(b))) - | _ => None - }; - let pure = some; - } - ); +include Applicative.MakeBasic({ + type t('a) = option('a); + let apply = (o, a) => + switch (o) { + | Some(f) => bind(a, b => return(f(b))) + | _ => None + }; + let pure = some; +}); module Infix = { let (>>=) = (>>=); diff --git a/src/Option.rei b/src/Option.rei index 46da72c..11127ab 100644 --- a/src/Option.rei +++ b/src/Option.rei @@ -28,13 +28,13 @@ let fmap: ('a => 'b, option('a)) => option('b); let (<$>): (option('a), 'a => 'b) => option('b); -let apply: (option(('a => 'b)), option('a)) => option('b); +let apply: (option('a => 'b), option('a)) => option('b); let pure: 'a => option('a); module Infix: { let (>>=): (option('a), 'a => option('b)) => option('b); let (<$>): (option('a), 'a => 'b) => option('b); - let (<*>): (option(('a => 'b)), option('a)) => option('b); + let (<*>): (option('a => 'b), option('a)) => option('b); let (|?): (option('a), option('a)) => option('a); }; \ No newline at end of file diff --git a/src/RList.re b/src/RList.re index 2b52536..020f51b 100644 --- a/src/RList.re +++ b/src/RList.re @@ -1,10 +1,10 @@ -let head = (xs) => +let head = xs => switch (List.hd(xs)) { | exception _ => None | a => Some(a) }; -let tail = (xs) => +let tail = xs => switch (List.tl(xs)) { | exception _ => None | a => Some(a) @@ -16,11 +16,12 @@ let nth = (i, xs) => | a => Some(a) }; -let init = (xs) => Option.(xs |> List.rev |> tail <$> List.rev); +let init = xs => Option.(xs |> List.rev |> tail <$> List.rev); -let last = (xs) => xs |> List.rev |> head; +let last = xs => xs |> List.rev |> head; -let any = (pred, xs) => List.fold_left((acc, v) => acc ? acc : pred(v), false, xs); +let any = (pred, xs) => + List.fold_left((acc, v) => acc ? acc : pred(v), false, xs); let append = (a, xs) => List.append(xs, [a]); @@ -33,21 +34,22 @@ let take = { | (_, []) => acc | (i, [a, ...b]) => loop(i - 1, b, append(a, acc)) }; - (i, xs) => loop(i, xs, []) + (i, xs) => loop(i, xs, []); }; let takeLast = (i, xs) => List.rev(xs) |> take(i) |> List.rev; let takeWhile = { let rec loop = (pred, xs, acc) => - switch xs { + switch (xs) { | [] => acc | [a, ...b] => pred(a) ? loop(pred, b, append(a, acc)) : acc }; - (pred, xs) => loop(pred, xs, []) + (pred, xs) => loop(pred, xs, []); }; -let takeLastWhile = (pred, xs) => List.rev(xs) |> takeWhile(pred) |> List.rev; +let takeLastWhile = (pred, xs) => + List.rev(xs) |> takeWhile(pred) |> List.rev; let rec drop = (i, xs) => switch (i, xs) { @@ -59,16 +61,17 @@ let rec drop = (i, xs) => let dropLast = (i, xs) => take(List.length(xs) - i, xs); let rec dropWhile = (pred, xs) => - switch xs { + switch (xs) { | [] => [] | [a, ...b] => pred(a) ? dropWhile(pred, b) : xs }; -let dropLastWhile = (pred, xs) => List.rev(xs) |> dropWhile(pred) |> List.rev; +let dropLastWhile = (pred, xs) => + List.rev(xs) |> dropWhile(pred) |> List.rev; let dropRepeatsWith = { let rec loop = (pred, xs, acc) => - switch xs { + switch (xs) { | [] => acc | [a, ...b] when List.length(acc) == 0 => loop(pred, b, append(a, acc)) | [a, ...b] => @@ -77,42 +80,46 @@ let dropRepeatsWith = { loop(pred, b, acc) : loop(pred, b, append(a, acc)) ) }; - (pred, xs) => loop(pred, xs, []) + (pred, xs) => loop(pred, xs, []); }; -let dropRepeats = (xs) => dropRepeatsWith((x, y) => x == y, xs); +let dropRepeats = xs => dropRepeatsWith((x, y) => x == y, xs); let splitAt = (i, xs) => (take(i, xs), takeLast(List.length(xs) - i, xs)); let adjust = (f, i, xs) => { let (a, b) = splitAt(i + 1, xs); - switch a { + switch (a) { | _ when i < 0 => xs | _ when i >= List.length(xs) => xs | [] => b | [a] => [f(a), ...b] | a => Option.( - init(a) >>= ((x) => last(a) <$> f <$> Function.flip(append, x)) <$> concat(b) |> default(xs) + init(a) + >>= (x => last(a) <$> f <$> Function.flip(append, x)) + <$> concat(b) + |> default(xs) ) - } + }; }; let aperature = { let rec loop = (i, xs, acc) => - switch xs { + switch (xs) { | [] => acc | a when List.length(a) < i => acc | [_, ...b] => loop(i, b, append(take(i, xs), acc)) }; - (i, xs) => loop(i, xs, []) + (i, xs) => loop(i, xs, []); }; -let containsWith = (f) => List.exists(f); +let containsWith = f => List.exists(f); -let contains = (x) => containsWith(Util.eq(x)); +let contains = x => containsWith(Util.eq(x)); -let endsWith = (a, xs) => Option.(last(xs) <$> Util.eq(a) |> default(false)); +let endsWith = (a, xs) => + Option.(last(xs) <$> Util.eq(a) |> default(false)); let find = (pred, xs) => switch (List.find(pred, xs)) { @@ -122,11 +129,11 @@ let find = (pred, xs) => let findIndex = { let rec loop = (pred, xs, i) => - switch xs { + switch (xs) { | [] => None | [a, ...b] => pred(a) ? Some(i) : loop(pred, b, i + 1) }; - (pred, xs) => loop(pred, xs, 0) + (pred, xs) => loop(pred, xs, 0); }; let findLast = (pred, xs) => xs |> List.rev |> find(pred); @@ -140,35 +147,44 @@ let findLastIndex = (pred, xs) => let groupWith = { let rec loop = (pred: ('a, 'a) => bool, n, xs, acc) => Option.Infix.( - switch xs { + switch (xs) { | [] => List.length(acc) == 0 ? [[]] : acc | [a] => append([a], acc) | _ when n > List.length(xs) => append(xs, acc) | _ => let (x0, x1) = splitAt(n + 1, xs); Some(pred) <*> last(x0) <*> head(x1) |> Option.default(true) ? - loop(pred, n + 1, xs, acc) : loop(pred, 0, x1, append(x0, acc)) + loop(pred, n + 1, xs, acc) : loop(pred, 0, x1, append(x0, acc)); } ); - (pred, xs) => loop(pred, 0, xs, []) + (pred, xs) => loop(pred, 0, xs, []); }; -let indexOf = (a, xs) => findIndex((x) => a == x, xs); +let indexOf = (a, xs) => findIndex(x => a == x, xs); let insert = (i, x, xs) => { let (a, b) = splitAt(i, xs); - a |> append(x) |> concat(b) + a |> append(x) |> concat(b); }; let insertAll = (i, xs, ys) => { let (a, b) = splitAt(i, ys); - a |> concat(xs) |> concat(b) + a |> concat(xs) |> concat(b); }; let intersperse = (a, xs) => - List.fold_left((acc, v) => List.length(acc) > 0 ? acc |> append(a) |> append(v) : [v], [], xs); + List.fold_left( + (acc, v) => + List.length(acc) > 0 ? acc |> append(a) |> append(v) : [v], + [], + xs, + ); -let join = (j) => List.fold_left((acc, v) => String.length(acc) == 0 ? v : acc ++ j ++ v, ""); +let join = j => + List.fold_left( + (acc, v) => String.length(acc) == 0 ? v : acc ++ j ++ v, + "", + ); let lastIndexOf = (a, xs) => switch (indexOf(a, List.rev(xs))) { @@ -176,9 +192,12 @@ let lastIndexOf = (a, xs) => | Some(a) => Some(List.length(xs) - a - 1) }; -let none = (pred, xs) => ! List.exists(pred, xs); +let none = (pred, xs) => !List.exists(pred, xs); -let partition = (pred, xs) => (List.filter(pred, xs), List.filter((x) => ! pred(x), xs)); +let partition = (pred, xs) => ( + List.filter(pred, xs), + List.filter(x => !pred(x), xs), +); let range = { let rec loop = (inc, s, e, acc) => @@ -186,46 +205,50 @@ let range = { | [] => loop(inc, s, e, [s]) | [a, ..._] => inc(a) > e ? acc : loop(inc, s, e, append(inc(a), acc)) }; - (inc, s, e) => loop(inc, s, e, []) + (inc, s, e) => loop(inc, s, e, []); }; -let rangeInt = (step) => range((x) => x + step); +let rangeInt = step => range(x => x + step); -let rangeFloat = (step) => range((x) => x +. step); +let rangeFloat = step => range(x => x +. step); let reduceWhile = (pred, f, x, xs) => List.fold_left( (acc, v) => - switch acc { + switch (acc) { | (false, _) => acc | (_, a) => pred(a, v) ? (true, f(a, v)) : (false, a) }, (true, x), - xs + xs, ) |> snd; -let reject = (pred) => List.filter((x) => ! pred(x)); +let reject = pred => List.filter(x => !pred(x)); let remove = (i, n, xs) => { let (a, b) = splitAt(i, xs); - a @ drop(n, b) + a @ drop(n, b); }; let repeat = { let rec loop = (a, n, acc) => - switch n { + switch (n) { | 0 => acc | n => loop(a, n - 1, append(a, acc)) }; - (a, n) => loop(a, n, []) + (a, n) => loop(a, n, []); }; let scan = (f: ('a, 'b) => 'a, i: 'a) => Option.( List.fold_left( - (acc, v) => last(acc) <$> Function.flip(f, v) <$> Function.flip(append, acc) |> default([]), - [i] + (acc, v) => + last(acc) + <$> Function.flip(f, v) + <$> Function.flip(append, acc) + |> default([]), + [i], ) ); @@ -233,11 +256,11 @@ let slice = (a, b, xs) => xs |> splitAt(a) |> snd |> splitAt(b) |> fst; let splitEvery = { let rec loop = (n, xs, acc) => - switch xs { + switch (xs) { | [] => acc | xs => loop(n, drop(n, xs), append(take(n, xs), acc)) }; - (n, xs) => loop(n, xs, []) + (n, xs) => loop(n, xs, []); }; let splitWhen = (pred, xs) => @@ -255,19 +278,20 @@ let startsWith = (x, xs) => let times = { let rec loop = (f, n, i, acc) => if (i < n) { - loop(f, n, i + 1, append(f(i), acc)) + loop(f, n, i + 1, append(f(i), acc)); } else { - acc + acc; }; - (f, n) => loop(f, n, 0, []) + (f, n) => loop(f, n, 0, []); }; let uniqWithBy = (eq, f, xs) => List.fold_left( ((acc, tacc), v) => - containsWith(eq(f(v)), tacc) ? (acc, tacc) : (append(v, acc), append(f(v), tacc)), + containsWith(eq(f(v)), tacc) ? + (acc, tacc) : (append(v, acc), append(f(v), tacc)), ([], []), - xs + xs, ) |> fst; @@ -275,7 +299,7 @@ let uniqBy = (f, xs) => uniqWithBy(Util.eq, f, xs); let uniqWith = (eq, xs) => uniqWithBy(eq, Function.identity, xs); -let uniq = (xs) => uniqBy(Function.identity, xs); +let uniq = xs => uniqBy(Function.identity, xs); let unionWith = (f, xs, ys) => concat(ys, xs) |> uniqWith(f); @@ -283,7 +307,7 @@ let union = (xs, ys) => concat(ys, xs) |> uniq; let update = (x, i, xs) => adjust(Function.always(x), i, xs); -let without = (exclude, xs) => reject((x) => contains(x, exclude), xs); +let without = (exclude, xs) => reject(x => contains(x, exclude), xs); let zipWith = { let rec loop = (f, xs, ys, acc) => @@ -291,27 +315,30 @@ let zipWith = { | ([a, ...b], [c, ...d]) => loop(f, b, d, append(f(a, c), acc)) | (_, _) => acc }; - (f, xs, ys) => loop(f, xs, ys, []) + (f, xs, ys) => loop(f, xs, ys, []); }; let differenceWith = { let rec loop = (f, xs, ys, acc) => - switch xs { + switch (xs) { | [] => acc - | [a, ...b] => containsWith(f(a), ys) ? loop(f, b, ys, acc) : loop(f, b, ys, append(a, acc)) + | [a, ...b] => + containsWith(f(a), ys) ? + loop(f, b, ys, acc) : loop(f, b, ys, append(a, acc)) }; - (f, xs, ys) => loop(f, xs, ys, []) @ loop(f, ys, xs, []) + (f, xs, ys) => loop(f, xs, ys, []) @ loop(f, ys, xs, []); }; let difference = (xs, ys) => differenceWith(Util.eq, xs, ys); let intersection = { let rec loop = (xs, ys, acc) => - switch xs { + switch (xs) { | [] => acc - | [a, ...b] => contains(a, ys) ? loop(b, ys, append(a, acc)) : loop(b, ys, acc) + | [a, ...b] => + contains(a, ys) ? loop(b, ys, append(a, acc)) : loop(b, ys, acc) }; - (xs, ys) => loop(xs, ys, []) + (xs, ys) => loop(xs, ys, []); }; let zip = { @@ -321,36 +348,43 @@ let zip = { | (_, []) => acc | ([a, ...b], [c, ...d]) => loop(b, d, append((a, c), acc)) }; - (xs, ys) => loop(xs, ys, []) + (xs, ys) => loop(xs, ys, []); }; let fold_lefti = { let rec loop = (i, f, acc, xs) => - switch xs { + switch (xs) { | [] => acc | [a, ...b] => loop(i + 1, f, f(acc, i, a), b) }; - (f, init, xs) => loop(0, f, init, xs) + (f, init, xs) => loop(0, f, init, xs); }; let fold_righti = { - let xis = (xs) => fold_lefti((acc, i, a) => [(i, a), ...acc], [], xs); - (f, init, xs) => List.fold_left((acc, (i, a)) => f(acc, i, a), init, xis(xs)) + let xis = xs => fold_lefti((acc, i, a) => [(i, a), ...acc], [], xs); + (f, init, xs) => + List.fold_left((acc, (i, a)) => f(acc, i, a), init, xis(xs)); }; let filteri = { let rec loop = (pred, xs, i, acc) => - switch xs { + switch (xs) { | [] => acc - | [a, ...b] => pred(i, a) ? loop(pred, b, i + 1, append(a, acc)) : loop(pred, b, i + 1, acc) + | [a, ...b] => + pred(i, a) ? + loop(pred, b, i + 1, append(a, acc)) : loop(pred, b, i + 1, acc) }; - (pred, xs) => loop(pred, xs, 0, []) + (pred, xs) => loop(pred, xs, 0, []); }; let filter_map = (pred, f, xs) => List.fold_left((acc, x) => pred(x) ? append(f(x), acc) : acc, [], xs); let filter_mapi = (pred, f, xs) => - fold_lefti((acc, i, a) => pred(i, a) ? append(f(i, a), acc) : acc, [], xs); + fold_lefti( + (acc, i, a) => pred(i, a) ? append(f(i, a), acc) : acc, + [], + xs, + ); -let pure = (a) => [a]; \ No newline at end of file +let pure = a => [a]; \ No newline at end of file diff --git a/src/Result.re b/src/Result.re index 2792558..0a8b8be 100644 --- a/src/Result.re +++ b/src/Result.re @@ -1,68 +1,65 @@ open Belt.Result; -let fail = (err) => Error(err); +let fail = err => Error(err); -let isOk = (r) => - switch r { +let isOk = r => + switch (r) { | Ok(_) => true | _ => false }; -let getOk = (r) => - switch r { +let getOk = r => + switch (r) { | Ok(a) => Some(a) | _ => None }; -let getError = (r) => - switch r { +let getError = r => + switch (r) { | Error(err) => Some(err) | _ => None }; -let isError = (r) => - switch r { +let isError = r => + switch (r) { | Error(_) => true | _ => false }; let ofOption = (err, r) => - switch r { + switch (r) { | Some(a) => Ok(a) | None => Error(err) }; let ap = (r, a) => - switch r { + switch (r) { | Ok(f) => Ok(f(a)) | Error(err) => Error(err) }; let result = (okF, errF, r) => - switch r { + switch (r) { | Ok(a) => okF(a) | Error(err) => errF(err) }; let bimap = (okF, errF, r) => - switch r { + switch (r) { | Ok(a) => Ok(okF(a)) | Error(err) => Error(errF(err)) }; -include - Monad.MakeBasic2( - { - type t('a, 'd) = Belt.Result.t('a, 'd); - let bind = (r, f) => - switch r { - | Ok(a) => f(a) - | Error(err) => Error(err) - }; - let return = (a) => Ok(a); - let fmap = `DefineWithBind; - } - ); +include Monad.MakeBasic2({ + type t('a, 'd) = Belt.Result.t('a, 'd); + let bind = (r, f) => + switch (r) { + | Ok(a) => f(a) + | Error(err) => Error(err) + }; + let return = a => Ok(a); + let fmap = `DefineWithBind; +}); module Infix = { let (>>=) = (>>=);