Skip to content

Commit

Permalink
Add naturalOrder util function (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriehSchneier authored Jan 13, 2022
1 parent e04d6d5 commit bbfef12
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
14 changes: 14 additions & 0 deletions contrib/util.arrai
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ let reduceFlat = \arr \fn \val reduce(arr, \z \i reduce(i, fn, z), val);

# Returns a sequence with any offset and holes removed.
let noOffset = \s s rank (:.@);

# Explore constructs a dependency graph by starting at source and calling step
# to find adjacent nodes. Deps is the graph constructed so far.
# Self-edges are ignored.
Expand Down Expand Up @@ -95,6 +96,18 @@ let rec _topsort = \graph \sorted \sources
};
let topsort = \graph _topsort(graph, [], unimported(graph));

# NaturalOrder can be used to do a natural ordering of a set of strings
let naturalOrder =
let segment = //re.compile(`\d+|\D+`).match;
let digit = //re.compile(`\d`).match;
\v
let segments = segment(v) >> (.(0) rank (:.@));
let modCheck = cond {
segments && digit(segments(0)): 0,
_: 1,
};
segments >>> \i \w cond { (i % 2 = modCheck): //eval.value(w), _: w };

(
:explore,
:filterTree,
Expand All @@ -106,4 +119,5 @@ let topsort = \graph _topsort(graph, [], unimported(graph));
:reduceObj,
:topsort,
:unimported,
:naturalOrder,
)
6 changes: 6 additions & 0 deletions contrib/util_test.arrai
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ let _concat = \z \k \v $`${z}+${k}${v}`;
let inline = {:time:2020-06-09:};
util.invokeMacro(time, '2020-06-09') = inline = (day: 9, month: 6, year: 2020)
,

naturalOrder:
let input = {'a2','a10', 'a10a', 'a10b', 'a2a', 'a', '1', '12a', '12', '21', '21a1', '2', '', 'ab', 'a20b1c'};
let ordered = ['', '1', '2', '12', '12a', '21', '21a1', 'a', 'a2', 'a2a', 'a10', 'a10a', 'a10b', 'a20b1c', 'ab'];
(input orderby util.naturalOrder(.)) = ordered
,
)

0 comments on commit bbfef12

Please sign in to comment.