Skip to content

Commit

Permalink
apply lebab -t arrow-return that cleaned up the code a lot
Browse files Browse the repository at this point in the history
  • Loading branch information
jdfekete committed Dec 31, 2020
1 parent 8912fce commit 3dab80b
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 107 deletions.
8 changes: 2 additions & 6 deletions src/barycenter_order.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,8 @@ export function barycenter(graph, comp, max_iter) {
let neighbors;
let med;

layer1 = comp.filter((n) => {
return graph.outDegree(n) !== 0;
});
layer2 = comp.filter((n) => {
return graph.inDegree(n) !== 0;
});
layer1 = comp.filter(n => graph.outDegree(n) !== 0);
layer2 = comp.filter(n => graph.inDegree(n) !== 0);
if (comp.length < 3) {
return [layer1, layer2, count_crossings(graph, layer1, layer2)];
}
Expand Down
16 changes: 4 additions & 12 deletions src/count_crossings.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ export function count_crossings(graph, north, south) {
const comp = permutation(graph.nodes().length);

if (north === undefined) {
north = comp.filter((n) => {
return graph.outDegree(n) !== 0;
});
south = comp.filter((n) => {
return graph.inDegree(n) !== 0;
});
north = comp.filter(n => graph.outDegree(n) !== 0);
south = comp.filter(n => graph.inDegree(n) !== 0);
}

// Choose the smaller axis
Expand All @@ -42,13 +38,9 @@ export function count_crossings(graph, north, south) {

for (i = 0; i < north.length; i++) {
if (invert) {
n = graph.inEdges(north[i]).map((e) => {
return south_inv[e.target.index];
});
n = graph.inEdges(north[i]).map(e => south_inv[e.target.index]);
} else {
n = graph.outEdges(north[i]).map((e) => {
return south_inv[e.source.index];
});
n = graph.outEdges(north[i]).map(e => south_inv[e.source.index]);
}
n.sort(cmp_number);
southsequence = southsequence.concat(n);
Expand Down
14 changes: 4 additions & 10 deletions src/cuthill_mckee_order.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,10 @@ export function cuthill_mckee(graph, comp) {
perm.push(n);
e = graph
.edges(n)
.map((edge) => {
return graph.other(edge, n).index;
})
.filter((n) => {
return !visited[n] && n in inv;
})
.sort((a, b) => {
// ascending by degree
return graph.degree(a) - graph.degree(b);
});
.map(edge => graph.other(edge, n).index)
.filter(n => !visited[n] && n in inv)
.sort((a, b) => // ascending by degree
graph.degree(a) - graph.degree(b));

e.forEach(queue.push, queue);
}
Expand Down
4 changes: 1 addition & 3 deletions src/fiedler.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ export function fiedler_vector(B, eps) {
const n = B.length;

const // Copy B
Bhat = B.map((row) => {
return row.slice();
});
Bhat = B.map(row => row.slice());

let i;
let j;
Expand Down
19 changes: 6 additions & 13 deletions src/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ export function graph(nodes, links, directed) {
return graph;
};

graph.nodes_indices = () => {
return nodes.map((n) => {
return n.index;
});
};
graph.nodes_indices = () => nodes.map(n => n.index);

graph.generate_nodes = (n) => {
nodes = [];
Expand All @@ -32,11 +28,10 @@ export function graph(nodes, links, directed) {
links = x;
return graph;
};
graph.links_indices = () => {
return links.map((l) => {
return { source: l.source.index, target: l.target.index };
});
};
graph.links_indices = () => links.map(l => ({
source: l.source.index,
target: l.target.index
}));
graph.linkDistance = function (x) {
if (!arguments.length) return linkDistance;
linkDistance = typeof x === 'function' ? x : +x;
Expand Down Expand Up @@ -226,9 +221,7 @@ export function graph(nodes, links, directed) {
comps.push(ccomp);
}
}
comps.sort((a, b) => {
return b.length - a.length;
});
comps.sort((a, b) => b.length - a.length);
return comps;
}

Expand Down
4 changes: 1 addition & 3 deletions src/graph2distmat.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ export function valuemats_reorder(valuemats, leaforder, comps) {
let orders = valuemats.map(leaforder);

if (comps) {
orders = orders.map((d, i) => {
return permute(comps[i], d);
});
orders = orders.map((d, i) => permute(comps[i], d));
}
return orders.reduce(flatten);
}
4 changes: 1 addition & 3 deletions src/hcluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ export function hcluster() {

hcluster.distanceMatrix = function (x) {
if (!arguments.length) return distMatrix;
distMatrix = x.map((y) => {
return y.slice(0);
});
distMatrix = x.map(y => y.slice(0));
return hcluster;
};

Expand Down
4 changes: 1 addition & 3 deletions src/mat2graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ export function mat2graph(mat, directed) {
}
}
return graph(nodes, links, directed)
.linkDistance((l, i) => {
return 1 + max_value - l.value;
})
.linkDistance((l, i) => 1 + max_value - l.value)
.init();
}
4 changes: 1 addition & 3 deletions src/optimal_leaf_order.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ export function optimal_leaf_order() {
optimal_leaf_order.distance_matrix = function (x) {
if (!arguments.length) return distanceMatrix;
// copy
distanceMatrix = x.map((y) => {
return y.slice(0);
});
distanceMatrix = x.map(y => y.slice(0));
return optimal_leaf_order;
};
optimal_leaf_order.distanceMatrix = optimal_leaf_order.distance_matrix; // compatibility
Expand Down
8 changes: 2 additions & 6 deletions src/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ export function order() {
}
if (i0 !== 0) {
perm = permutation(i0).concat(
perm.map((v) => {
return v + i0;
})
perm.map(v => v + i0)
);
}
if (orig.length > j0) {
Expand Down Expand Up @@ -332,9 +330,7 @@ export function order() {
}

function _perm_insert(perm, i, nv) {
perm = perm.map((v) => {
return v < nv ? v : v + 1;
});
perm = perm.map(v => v < nv ? v : v + 1);
perm.splice(i, 0, nv);
return perm;
}
Expand Down
4 changes: 1 addition & 3 deletions src/pcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ export function dicts_to_array(dicts, keys) {
}

function abs_matrix(x) {
return x.map((y) => {
return y.map(Math.abs);
});
return x.map(y => y.map(Math.abs));
}

function pcp_flip_axes(perm, naxes, pcor) {
Expand Down
8 changes: 2 additions & 6 deletions src/sort_order.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { permutation } from './permutation';

export function sort_order(v) {
return permutation(0, v.length).sort((a, b) => {
return v[a] - v[b];
});
return permutation(0, v.length).sort((a, b) => v[a] - v[b]);
}

export const sort_order_ascending = sort_order;

export function sort_order_descending(v) {
return permutation(0, v.length).sort((a, b) => {
return v[b] - v[a];
});
return permutation(0, v.length).sort((a, b) => v[b] - v[a]);
}
28 changes: 7 additions & 21 deletions test/count_crossings-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ function naive_count_crossings(graph, north, south) {
for (i = 0; i < north.length; i++) {
v = north[i];
links = links.concat(
graph.outEdges(v).map((e) => {
return [inv_north[e.target.index], inv_south[e.source.index]];
})
graph.outEdges(v).map(e => [inv_north[e.target.index], inv_south[e.source.index]])
);
}
for (i = 0; i < links.length; i++) {
Expand All @@ -41,18 +39,10 @@ function naive_count_crossings(graph, north, south) {
function dotest(mat) {
const graph = reorder.mat2graph(mat, true),
comps = graph.components(),
comp = comps.reduce((a, b) => {
return a.length > b.length ? a : b;
});
comp.sort((a, b) => {
return a - b;
});
const layer1 = comp.filter((n) => {
return graph.outEdges(n).length != 0;
}),
layer2 = comp.filter((n) => {
return graph.inEdges(n).length != 0;
});
comp = comps.reduce((a, b) => a.length > b.length ? a : b);
comp.sort((a, b) => a - b);
const layer1 = comp.filter(n => graph.outEdges(n).length != 0),
layer2 = comp.filter(n => graph.inEdges(n).length != 0);
//console.time('fast_crossings');
const c1 = reorder.count_crossings(graph, layer1, layer2);
//console.timeEnd('fast_crossings');
Expand Down Expand Up @@ -97,12 +87,8 @@ suite.addBatch({
.directed(true)
.init(),
comp = graph.components()[0],
layer1 = comp.filter((n) => {
return graph.outEdges(n).length != 0;
}),
layer2 = comp.filter((n) => {
return graph.inEdges(n).length != 0;
});
layer1 = comp.filter(n => graph.outEdges(n).length != 0),
layer2 = comp.filter(n => graph.inEdges(n).length != 0);

assert.equal(naive_count_crossings(graph, layer1, layer2), 12);
assert.equal(reorder.count_crossings(graph, layer1, layer2), 12);
Expand Down
4 changes: 1 addition & 3 deletions test/env-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ assert.inDeltaArrayAbs = (actual, expected, delta, message) => {
};

function neg(a) {
return a.map((x) => {
return -x;
});
return a.map(x => -x);
}

assert.inDeltaArrayOrNeg = (actual, expected, delta, message) => {
Expand Down
8 changes: 2 additions & 6 deletions test/graph2mat-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ function remove_zeroes(mat) {
for (i = mat.length - 1; i >= 0; i--) {
const row = mat[i];
if (
row.some((a) => {
return a > 0;
})
row.some(a => a > 0)
) {
//console.log('remove row %d', i);
break;
Expand All @@ -23,9 +21,7 @@ function remove_zeroes(mat) {
}
for (j = mat[0].length - 1; j >= 0; j--) {
if (
mat.some((row) => {
return row[j] != 0;
})
mat.some(row => row[j] != 0)
) {
//console.log('remove column %d', j);
break;
Expand Down
8 changes: 2 additions & 6 deletions test/order-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,11 @@ function objEquiv(a, b) {
}

function toNum(a) {
return a.map((l) => {
return l.charCodeAt(0) - 97;
});
return a.map(l => l.charCodeAt(0) - 97);
}

function toLetter(a) {
return a.map((l) => {
return String.fromCharCode(97 + l);
});
return a.map(l => String.fromCharCode(97 + l));
}

suite.export(module);

0 comments on commit 3dab80b

Please sign in to comment.