2019-02-04 20:29:04 -06:00
|
|
|
// Joins a list into a string with an optional provided separator.
|
|
|
|
// The string will be in the result field of the generated tuple
|
|
|
|
// from the module.
|
2019-01-08 20:32:16 -06:00
|
|
|
let str_join = module{
|
2019-02-04 20:29:04 -06:00
|
|
|
// The default separator is a single space. You can override
|
|
|
|
// this when you call the module if desired.
|
2019-01-08 20:32:16 -06:00
|
|
|
sep=" ",
|
2019-02-04 20:29:04 -06:00
|
|
|
// The list is a required parameter.
|
2019-01-08 20:32:16 -06:00
|
|
|
list=NULL,
|
2019-02-24 08:16:07 -06:00
|
|
|
} => (result) {
|
2019-02-04 20:29:04 -06:00
|
|
|
// The function used by reduce to join the list into a string.
|
2019-11-02 11:01:47 -05:00
|
|
|
let joiner = func (acc, item) => select ((acc.out == ""), NULL) => {
|
2019-01-16 19:07:03 -06:00
|
|
|
true = acc{
|
|
|
|
out="@@" % (acc.out,item),
|
|
|
|
},
|
|
|
|
false = acc{
|
|
|
|
out="@@@" % (acc.out, acc.sep, item),
|
2019-01-08 20:32:16 -06:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-02-04 20:29:04 -06:00
|
|
|
// The resulting joined string. Reference the result field from the
|
|
|
|
// tuple the module produces to get the joined string.
|
2019-01-31 16:33:12 -06:00
|
|
|
let result = reduce(joiner, {sep=mod.sep, out=""}, (mod.list)).out;
|
2019-01-08 20:32:16 -06:00
|
|
|
};
|
|
|
|
|
2019-02-04 20:29:04 -06:00
|
|
|
// Computes the length of the provided list.
|
2019-02-04 19:48:51 -06:00
|
|
|
let len = func(list) => reduce(func(acc, item) => acc + 1, 0, list);
|
2019-01-08 20:32:16 -06:00
|
|
|
|
2019-02-04 20:29:04 -06:00
|
|
|
// Reverses the provided list.
|
2019-02-04 19:48:51 -06:00
|
|
|
let reverse = func(list) => reduce(func (acc, item) => [item] + acc, [], list);
|
2019-01-09 22:33:03 -06:00
|
|
|
|
2019-02-25 20:24:24 -06:00
|
|
|
// head returns the first item in a list as a list of one item.
|
|
|
|
// This function is safe for empty list inputs but is not safe
|
|
|
|
// for NULL inputs.
|
2019-11-02 11:01:47 -05:00
|
|
|
let head = func(list) => select (len(list) > 0, []) => {
|
2019-02-25 20:24:24 -06:00
|
|
|
true = [list.0],
|
|
|
|
};
|
|
|
|
|
|
|
|
// tail returns the tail of a list without the head.
|
|
|
|
// This function is safe for empty lists but is not safe
|
|
|
|
// for NULL inputs.
|
|
|
|
let tail = func(list) => reduce(
|
2019-11-02 11:01:47 -05:00
|
|
|
func (acc, item) => select (acc.count > 0, acc{count=1, tail=[]}) => {
|
2019-02-25 20:24:24 -06:00
|
|
|
true = acc{count = acc.count + 1, tail = acc.tail + [item]},
|
|
|
|
},
|
|
|
|
{count=0},
|
|
|
|
list,
|
|
|
|
).tail;
|
|
|
|
|
2019-02-04 20:29:04 -06:00
|
|
|
// Enumerates the provided list with optional start and step parameters for the
|
2019-04-12 21:13:24 -05:00
|
|
|
// enumeration. Produces a list of pairs with the enumeration and the list item.
|
2019-02-04 20:29:04 -06:00
|
|
|
//
|
2019-04-12 21:13:24 -05:00
|
|
|
// enumerate{list=["a","b","c"]} == [[0, "a"], [1, "b"], [2, "c"]]
|
2019-01-08 20:32:16 -06:00
|
|
|
let enumerate = module{
|
2019-02-04 20:29:04 -06:00
|
|
|
// Where to start the enumeration.
|
2019-01-08 20:32:16 -06:00
|
|
|
start = 0,
|
2019-02-04 20:29:04 -06:00
|
|
|
// The step amount for each enumeration.
|
2019-01-08 20:32:16 -06:00
|
|
|
step = 1,
|
2019-02-04 20:29:04 -06:00
|
|
|
// The list to enumerate.
|
2019-01-08 20:32:16 -06:00
|
|
|
list = NULL,
|
2019-02-24 08:16:07 -06:00
|
|
|
} => (result) {
|
2019-01-24 20:04:40 -06:00
|
|
|
let reducer = func (acc, item) => acc{
|
2019-01-16 19:07:03 -06:00
|
|
|
count = acc.count + acc.step,
|
|
|
|
list=acc.list + [[acc.count, item]],
|
2019-01-08 20:32:16 -06:00
|
|
|
};
|
|
|
|
|
2019-02-18 20:15:54 -06:00
|
|
|
let result = reduce(
|
|
|
|
reducer,
|
|
|
|
{count=mod.start, list=[], step=mod.step},
|
|
|
|
(mod.list)).list;
|
2019-01-10 18:38:14 -06:00
|
|
|
};
|
|
|
|
|
2019-02-27 19:33:14 -06:00
|
|
|
// slice returns a slice of a list starting at index start up to and
|
|
|
|
// including index end, inclusive.
|
|
|
|
let slice = module {
|
|
|
|
start = 0,
|
|
|
|
end = NULL,
|
|
|
|
list = NULL,
|
|
|
|
} => (result) {
|
2019-04-16 20:09:27 -05:00
|
|
|
let list = mod.pkg();
|
2019-02-27 19:33:14 -06:00
|
|
|
|
|
|
|
let list_len = list.len(mod.list);
|
|
|
|
|
2019-11-02 11:01:47 -05:00
|
|
|
let end = select (mod.end is "null", mod.end) => {
|
2019-11-27 22:17:35 -06:00
|
|
|
true = list_len - 1,
|
2019-02-27 19:33:14 -06:00
|
|
|
};
|
2019-11-27 22:17:35 -06:00
|
|
|
let start = mod.start;
|
2019-02-27 19:33:14 -06:00
|
|
|
|
|
|
|
// ensure some invariants
|
2019-11-27 22:17:35 -06:00
|
|
|
(start >= 0) || fail "Slice must be positive";
|
|
|
|
(start <= list_len) || fail "Slice start cannot be larger than the list len of @" % (list_len);
|
2019-02-27 19:33:14 -06:00
|
|
|
(end <= list_len) || fail "Slice end cannot be larger than list len of @" % (list_len);
|
|
|
|
|
2019-11-27 22:17:35 -06:00
|
|
|
let reducer = func(acc, item) => acc + [mod.list.(item)];
|
2019-02-27 19:33:14 -06:00
|
|
|
|
|
|
|
let result = reduce(
|
|
|
|
reducer,
|
2019-11-27 22:17:35 -06:00
|
|
|
[],
|
|
|
|
start:end);
|
2019-02-27 19:33:14 -06:00
|
|
|
};
|
|
|
|
|
2019-02-04 20:29:04 -06:00
|
|
|
// zips two lists together.
|
2019-04-12 21:13:24 -05:00
|
|
|
// The result list is only as long as the shortest list.
|
2019-02-04 20:29:04 -06:00
|
|
|
//
|
|
|
|
// zip{list1=[0,2,4],list2=[1,3,5]}.result == [[0, 1], [2, 3], [4, 5]]
|
2019-01-10 18:38:14 -06:00
|
|
|
let zip = module{
|
|
|
|
list1 = NULL,
|
|
|
|
list2 = NULL,
|
2019-02-24 08:16:07 -06:00
|
|
|
} => (result) {
|
2019-04-16 20:09:27 -05:00
|
|
|
let len = mod.pkg().len;
|
2019-02-04 19:48:51 -06:00
|
|
|
|
2019-02-04 20:29:04 -06:00
|
|
|
// Compute the length of each list.
|
2019-02-04 19:48:51 -06:00
|
|
|
let len1 = len(mod.list1);
|
|
|
|
let len2 = len(mod.list2);
|
2019-01-10 18:38:14 -06:00
|
|
|
|
2019-02-04 20:29:04 -06:00
|
|
|
// Compute the min range of the two lists.
|
2019-11-02 11:01:47 -05:00
|
|
|
let rng = select ((len1 >= len2), NULL) => {
|
2019-01-10 18:38:14 -06:00
|
|
|
true = 0:(len1 - 1),
|
|
|
|
false = 0:(len2 - 1),
|
|
|
|
};
|
|
|
|
|
2019-02-04 20:29:04 -06:00
|
|
|
// The reducer function for the zip operation.
|
2019-01-24 20:04:40 -06:00
|
|
|
let reducer = func (acc, item) => acc{
|
2019-01-16 19:07:03 -06:00
|
|
|
result = acc.result + [[acc.list1.(item), acc.list2.(item)]],
|
|
|
|
idxs = acc.idxs + [item]
|
2019-01-10 18:38:14 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let acc = {
|
|
|
|
list1 = mod.list1,
|
|
|
|
list2 = mod.list2,
|
|
|
|
result = [],
|
|
|
|
idxs = [],
|
|
|
|
};
|
|
|
|
|
2019-02-04 20:29:04 -06:00
|
|
|
// The resulting zipped list.
|
2019-01-31 16:33:12 -06:00
|
|
|
let result = reduce(reducer, acc, rng).result;
|
2019-04-15 22:14:18 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Wraps a list and provides a number of helpful operations.
|
|
|
|
//
|
|
|
|
// * len - property the length of the wrapped list.
|
|
|
|
//
|
|
|
|
// * str_join - function joins list into string with provided separator `sep`.
|
|
|
|
//
|
|
|
|
// * slice - function returns a slice of the list from `start` to `end`.
|
|
|
|
//
|
|
|
|
// * enumerate - function returns a list of pairs of [index, value] from the list.
|
|
|
|
// The returned list is wrapped in the ops module.
|
|
|
|
//
|
|
|
|
// * head - function returns the head of the list as list of one item.
|
|
|
|
//
|
|
|
|
// * tail - function returns the tail of the list.
|
|
|
|
// The returned list is wrapped in the ops module.
|
|
|
|
//
|
|
|
|
// * reverse - function returns the list reversed.
|
|
|
|
// The returned list is wrapped in the ops module.
|
|
|
|
let ops = module{
|
|
|
|
list=NULL,
|
|
|
|
} => ({len=len,
|
|
|
|
str_join=str_join,
|
|
|
|
slice=slice,
|
|
|
|
enumerate=enumerate,
|
|
|
|
tail=tail,
|
|
|
|
head=head,
|
|
|
|
reverse=reverse,
|
|
|
|
list=list,
|
|
|
|
}) {
|
2019-04-24 19:30:10 -05:00
|
|
|
let pkg = mod.pkg();
|
2019-04-15 22:14:18 -05:00
|
|
|
let list = mod.list;
|
2019-04-24 19:30:10 -05:00
|
|
|
let len = pkg.len(mod.list);
|
|
|
|
let str_join = func(sep) => pkg.str_join{list=mod.list, sep=sep};
|
2019-04-15 22:14:18 -05:00
|
|
|
|
2019-04-24 19:30:10 -05:00
|
|
|
let slice = func(start, end) => pkg.slice{start=start, end=end, list=mod.list};
|
|
|
|
let enumerate = func() => pkg.ops{list=pkg.enumerate{start=0, step=1, list=mod.list}};
|
|
|
|
let tail = func() => pkg.ops{list=pkg.tail(mod.list)};
|
|
|
|
let head = func() => pkg.head(mod.list);
|
|
|
|
let reverse = func() => mod.this{list=pkg.reverse(mod.list)};
|
2019-01-08 20:32:16 -06:00
|
|
|
};
|