Skip to content

Commit

Permalink
refactor(transducers): use arrow fns
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 5, 2019
1 parent 6eba241 commit e9f0542
Show file tree
Hide file tree
Showing 33 changed files with 267 additions and 244 deletions.
11 changes: 8 additions & 3 deletions packages/transducers/src/func/binary-search.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Comparator } from "@thi.ng/api/api";
import { Comparator, Fn } from "@thi.ng/api/api";

/**
* Returns the supposed index of `x` in pre-sorted array-like collection
Expand All @@ -12,7 +12,12 @@ import { Comparator } from "@thi.ng/api/api";
* @param x
* @returns index of `x`, else `-index` if item could not be found
*/
export function binarySearch<A, B>(arr: ArrayLike<A>, key: (x: A) => B, cmp: Comparator<B>, x: A) {
export const binarySearch = <A, B>(
arr: ArrayLike<A>,
key: Fn<A, B>,
cmp: Comparator<B>,
x: A
) => {
const kx = key(x);
let low = 0;
let high = arr.length - 1;
Expand All @@ -28,4 +33,4 @@ export function binarySearch<A, B>(arr: ArrayLike<A>, key: (x: A) => B, cmp: Com
}
}
return -low;
}
};
6 changes: 3 additions & 3 deletions packages/transducers/src/func/compr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ import { Reduced } from "../reduced";
* @param rfn
* @param fn
*/
export function compR<A, B, C>(rfn: Reducer<A, B>, fn: (acc: A, x: C) => A | Reduced<A>) {
return <Reducer<A, C>>[rfn[0], rfn[1], fn];
}
export const compR =
<A, B, C>(rfn: Reducer<A, B>, fn: (acc: A, x: C) => A | Reduced<A>): Reducer<A, C> =>
[rfn[0], rfn[1], fn];
5 changes: 2 additions & 3 deletions packages/transducers/src/func/constantly.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export function constantly<T>(x: T): (...args: any[]) => T {
return () => x;
}
export const constantly =
<T>(x: T): (...args: any[]) => T => () => x;
29 changes: 15 additions & 14 deletions packages/transducers/src/func/deep-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,20 @@ import { TransformSpec } from "../api";
*
* @param spec transformation spec
*/
export function deepTransform(spec: TransformSpec): (x: any) => any {
if (isFunction(spec)) {
return <any>spec;
}
const mapfns = Object.keys(spec[1] || {}).reduce(
(acc, k) => (acc[k] = deepTransform((<any>spec)[1][k]), acc),
{}
);
return (x) => {
const res = { ...x };
for (let k in mapfns) {
res[k] = mapfns[k](res[k]);
export const deepTransform =
(spec: TransformSpec): (x: any) => any => {
if (isFunction(spec)) {
return <any>spec;
}
return spec[0](res);
const mapfns = Object.keys(spec[1] || {}).reduce(
(acc, k) => (acc[k] = deepTransform((<any>spec)[1][k]), acc),
{}
);
return (x) => {
const res = { ...x };
for (let k in mapfns) {
res[k] = mapfns[k](res[k]);
}
return spec[0](res);
};
};
}
6 changes: 3 additions & 3 deletions packages/transducers/src/func/delay.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function delay<T>(x: T, t: number) {
return new Promise((resolve) => setTimeout(() => resolve(x), t));
}
export const delay =
<T>(x: T, t: number) =>
new Promise((resolve) => setTimeout(() => resolve(x), t));
12 changes: 6 additions & 6 deletions packages/transducers/src/func/ensure-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { ensureIterable } from "./ensure-iterable";
*
* @param x
*/
export function ensureArray(x: any): any[] {
return isArray(x) ? x : [...ensureIterable(x)];
}
export const ensureArray =
(x: any): any[] =>
isArray(x) ? x : [...ensureIterable(x)];

export function ensureArrayLike(x: any): ArrayLike<any> {
return isArrayLike(x) ? x : [...ensureIterable(x)];
}
export const ensureArrayLike =
(x: any): ArrayLike<any> =>
isArrayLike(x) ? x : [...ensureIterable(x)];
13 changes: 7 additions & 6 deletions packages/transducers/src/func/ensure-iterable.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";

export function ensureIterable(x: any): IterableIterator<any> {
if (!(x != null && x[Symbol.iterator])) {
illegalArgs(`value is not iterable: ${x}`);
}
return x;
}
export const ensureIterable =
(x: any): IterableIterator<any> => {
if (!(x != null && x[Symbol.iterator])) {
illegalArgs(`value is not iterable: ${x}`);
}
return x;
};
8 changes: 6 additions & 2 deletions packages/transducers/src/func/fuzzy-match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import { equiv } from "@thi.ng/equiv";
* @param query
* @param eq
*/
export function fuzzyMatch<T>(domain: ArrayLike<T>, query: ArrayLike<T>, eq: Predicate2<any> = equiv) {
export const fuzzyMatch = <T>(
domain: ArrayLike<T>,
query: ArrayLike<T>,
eq: Predicate2<any> = equiv
) => {
const nd = domain.length;
const nq = query.length;
if (nq > nd) {
Expand All @@ -35,4 +39,4 @@ export function fuzzyMatch<T>(domain: ArrayLike<T>, query: ArrayLike<T>, eq: Pre
return false;
}
return true;
}
};
2 changes: 1 addition & 1 deletion packages/transducers/src/func/identity.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export function identity<T>(x: T) { return x; }
export const identity = <T>(x: T) => x;
6 changes: 3 additions & 3 deletions packages/transducers/src/func/key-selector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { renamer } from "./renamer";

export function keySelector(keys: PropertyKey[]) {
return renamer(keys.reduce((acc, x) => (acc[x] = x, acc), {}));
}
export const keySelector =
(keys: PropertyKey[]) =>
renamer(keys.reduce((acc, x) => (acc[x] = x, acc), {}));
20 changes: 10 additions & 10 deletions packages/transducers/src/func/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
*
* @param src source data
*/
export function lookup1d<T>(src: T[]) {
return (i: number) => src[i];
}
export const lookup1d =
<T>(src: T[]) => (i: number) => src[i];

/**
* Returns function accepting a single `[x, y]` index tuple,
Expand All @@ -28,9 +27,9 @@ export function lookup1d<T>(src: T[]) {
* @param src source data
* @param width number of items along X (columns)
*/
export function lookup2d<T>(src: T[], width: number) {
return (i: number[]) => src[i[0] + i[1] * width];
}
export const lookup2d =
<T>(src: T[], width: number) =>
(i: number[]) => src[i[0] + i[1] * width];

/**
* Same as `lookup2d()`, but for 3D data. The index ordering of the
Expand All @@ -41,7 +40,8 @@ export function lookup2d<T>(src: T[], width: number) {
* @param width number of items along X (columns)
* @param height number of items along Y (rows)
*/
export function lookup3d<T>(src: T[], width: number, height: number) {
const stridez = width * height;
return (i: number[]) => src[i[0] + i[1] * width + i[2] * stridez];
}
export const lookup3d =
<T>(src: T[], width: number, height: number) => {
const stridez = width * height;
return (i: number[]) => src[i[0] + i[1] * width + i[2] * stridez];
};
5 changes: 2 additions & 3 deletions packages/transducers/src/func/peek.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
*
* @param x
*/
export function peek<T>(x: ArrayLike<T>) {
return x[x.length - 1];
}
export const peek =
<T>(x: ArrayLike<T>) => x[x.length - 1];
5 changes: 3 additions & 2 deletions packages/transducers/src/func/random-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ import { take } from "../xform/take";
* @param prefix
* @param syms
*/
export const randomID = (len = 4, prefix = "", syms = "abcdefghijklmnopqrstuvwxyz") =>
[prefix, ...take(len, choices(syms))].join("");
export const randomID =
(len = 4, prefix = "", syms = "abcdefghijklmnopqrstuvwxyz") =>
[prefix, ...take(len, choices(syms))].join("");
81 changes: 41 additions & 40 deletions packages/transducers/src/func/renamer.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
import { IObjectOf } from "@thi.ng/api/api";

export function renamer(kmap: IObjectOf<PropertyKey>) {
const ks = Object.keys(kmap);
const [a2, b2, c2] = ks;
const [a1, b1, c1] = ks.map((k) => kmap[k]);
switch (ks.length) {
case 3:
return (x) => {
const res: any = {};
let v;
v = x[c1], v !== undefined && (res[c2] = v);
v = x[b1], v !== undefined && (res[b2] = v);
v = x[a1], v !== undefined && (res[a2] = v);
return res;
};
case 2:
return (x) => {
const res: any = {};
let v;
v = x[b1], v !== undefined && (res[b2] = v);
v = x[a1], v !== undefined && (res[a2] = v);
return res;
};
case 1:
return (x) => {
const res: any = {};
let v = x[a1];
v !== undefined && (res[a2] = v);
return res;
};
default:
return (x) => {
let k, v;
const res: any = {};
for (let i = ks.length - 1; i >= 0; i--) {
k = ks[i], v = x[kmap[k]], v !== undefined && (res[k] = v);
}
return res;
};
}
}
export const renamer =
(kmap: IObjectOf<PropertyKey>) => {
const ks = Object.keys(kmap);
const [a2, b2, c2] = ks;
const [a1, b1, c1] = ks.map((k) => kmap[k]);
switch (ks.length) {
case 3:
return (x) => {
const res: any = {};
let v;
v = x[c1], v !== undefined && (res[c2] = v);
v = x[b1], v !== undefined && (res[b2] = v);
v = x[a1], v !== undefined && (res[a2] = v);
return res;
};
case 2:
return (x) => {
const res: any = {};
let v;
v = x[b1], v !== undefined && (res[b2] = v);
v = x[a1], v !== undefined && (res[a2] = v);
return res;
};
case 1:
return (x) => {
const res: any = {};
let v = x[a1];
v !== undefined && (res[a2] = v);
return res;
};
default:
return (x) => {
let k, v;
const res: any = {};
for (let i = ks.length - 1; i >= 0; i--) {
k = ks[i], v = x[kmap[k]], v !== undefined && (res[k] = v);
}
return res;
};
}
};
23 changes: 12 additions & 11 deletions packages/transducers/src/func/shuffle.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
export function shuffleN(buf: any[], n: number) {
const l = buf.length;
n = n < l ? n : l;
while (--n >= 0) {
const a = (Math.random() * l) | 0;
const b = (Math.random() * l) | 0;
const t = buf[a];
buf[a] = buf[b];
buf[b] = t;
}
}
export const shuffleN =
(buf: any[], n: number) => {
const l = buf.length;
n = n < l ? n : l;
while (--n >= 0) {
const a = (Math.random() * l) | 0;
const b = (Math.random() * l) | 0;
const t = buf[a];
buf[a] = buf[b];
buf[b] = t;
}
};
63 changes: 32 additions & 31 deletions packages/transducers/src/func/swizzler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,35 @@
*
* @param order indices
*/
export function swizzler<T>(order: string | PropertyKey[]): (x: T) => any[] {
const [a, b, c, d, e, f, g, h] = order;
switch (order.length) {
case 0:
return () => [];
case 1:
return (x) => [x[a]];
case 2:
return (x) => [x[a], x[b]];
case 3:
return (x) => [x[a], x[b], x[c]];
case 4:
return (x) => [x[a], x[b], x[c], x[d]];
case 5:
return (x) => [x[a], x[b], x[c], x[d], x[e]];
case 6:
return (x) => [x[a], x[b], x[c], x[d], x[e], x[f]];
case 7:
return (x) => [x[a], x[b], x[c], x[d], x[e], x[f], x[g]];
case 8:
return (x) => [x[a], x[b], x[c], x[d], x[e], x[f], x[g], x[h]];
default:
return (x) => {
const res = [];
for (let i = order.length - 1; i >= 0; i--) {
res[i] = x[order[i]];
}
return res;
};
}
}
export const swizzler =
<T>(order: string | PropertyKey[]): (x: T) => any[] => {
const [a, b, c, d, e, f, g, h] = order;
switch (order.length) {
case 0:
return () => [];
case 1:
return (x) => [x[a]];
case 2:
return (x) => [x[a], x[b]];
case 3:
return (x) => [x[a], x[b], x[c]];
case 4:
return (x) => [x[a], x[b], x[c], x[d]];
case 5:
return (x) => [x[a], x[b], x[c], x[d], x[e]];
case 6:
return (x) => [x[a], x[b], x[c], x[d], x[e], x[f]];
case 7:
return (x) => [x[a], x[b], x[c], x[d], x[e], x[f], x[g]];
case 8:
return (x) => [x[a], x[b], x[c], x[d], x[e], x[f], x[g], x[h]];
default:
return (x) => {
const res = [];
for (let i = order.length - 1; i >= 0; i--) {
res[i] = x[order[i]];
}
return res;
};
}
};
7 changes: 5 additions & 2 deletions packages/transducers/src/func/weighted-random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { tuples } from "../iter/tuples";
* @param choices
* @param weights
*/
export function weightedRandom<T>(choices: ArrayLike<T> & Iterable<T>, weights?: ArrayLike<number> & Iterable<number>) {
export const weightedRandom = <T>(
choices: ArrayLike<T> & Iterable<T>,
weights?: ArrayLike<number> & Iterable<number>
) => {
const n = choices.length;
const opts = [...tuples(choices, weights || repeat(1))].sort((a, b) => b[1] - a[1]);
let total = 0, i, r, sum;
Expand All @@ -27,4 +30,4 @@ export function weightedRandom<T>(choices: ArrayLike<T> & Iterable<T>, weights?:
}
}
};
}
};
Loading

0 comments on commit e9f0542

Please sign in to comment.