Skip to content

Commit

Permalink
Merge pull request #12 from jonlaing/kujon-refmt
Browse files Browse the repository at this point in the history
Refmt 3.3.3
  • Loading branch information
jonlaing authored Sep 10, 2018
2 parents bb63442 + f79c51b commit de33736
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 218 deletions.
4 changes: 2 additions & 2 deletions src/Applicative.re
Original file line number Diff line number Diff line change
@@ -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);
};

Expand All @@ -9,4 +9,4 @@ module MakeBasic = (M: Basic) => {
let apply = M.apply;
let pure = M.pure;
let (<*>) = M.apply;
};
};
50 changes: 30 additions & 20 deletions src/Dict.re
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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(
Expand All @@ -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);
Expand All @@ -60,28 +69,29 @@ 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))
let fold_right = f => List.fold_right(((k, v), acc) => f(k, v, acc));

let unzip = d => {
let rec loop = (list, xs, ys) =>
Expand Down
10 changes: 5 additions & 5 deletions src/Dict.rei
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -36,16 +36,16 @@ 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;

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
let fold_right: ((string, 'a, 'b) => 'b, t('a), 'b) => 'b;

let unzip: t('a) => (list(string), list('a))
let unzip: t('a) => (list(string), list('a));
8 changes: 4 additions & 4 deletions src/Function.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ 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;
let (||>) = pipe;
};

include Infix;
include Infix;
44 changes: 23 additions & 21 deletions src/Lens.re
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -12,64 +12,66 @@ 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;

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))};

let second = {get: snd, set: (v, a) => (fst(a), v)};
let second = {get: snd, set: (v, a) => (fst(a), v)};
10 changes: 6 additions & 4 deletions src/Logic.re
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
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;

let unless = (pred, f, x) => pred(x) ? x : f(x);

let rec until = (pred, f, x) => pred(x) ? x : until(pred, f, f(x));

let when_ = (pred, f, x) => unless(not_(pred), f, x);
let when_ = (pred, f, x) => unless(not_(pred), f, x);
45 changes: 23 additions & 22 deletions src/Monad.re
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -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 = {
Expand All @@ -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));
}
);
MakeGeneral({
type t('a, 'i, 'j, 'd, 'e) = M.t('a, 'd);
include (M: Basic2 with type t('a, 'd) := M.t('a, 'd));
});
Loading

0 comments on commit de33736

Please sign in to comment.