REFACTOR: refactor unnecessary modules into functions.

This commit is contained in:
Jeremy Wall 2019-02-04 19:48:51 -06:00
parent 3c91f48415
commit cccef4554d
3 changed files with 14 additions and 39 deletions

View File

@ -14,21 +14,9 @@ let str_join = module{
let result = reduce(joiner, {sep=mod.sep, out=""}, (mod.list)).out; let result = reduce(joiner, {sep=mod.sep, out=""}, (mod.list)).out;
}; };
let len = module{ let len = func(list) => reduce(func(acc, item) => acc + 1, 0, list);
list = NULL,
} => {
let counter = func (acc, item) => acc + 1;
let result = reduce(counter, 0, (mod.list)); let reverse = func(list) => reduce(func (acc, item) => [item] + acc, [], list);
};
let reverse = module{
list = NULL,
} => {
let reducer = func (acc, item) => [item] + acc;
let result = reduce(reducer, [], (mod.list));
};
let enumerate = module{ let enumerate = module{
start = 0, start = 0,
@ -50,10 +38,11 @@ let zip = module{
list1 = NULL, list1 = NULL,
list2 = NULL, list2 = NULL,
} => { } => {
let counter = func (acc, item) => acc + 1; let len = import "std/lists.ucg".len;
let len1 = reduce(counter, 0, (mod.list1));
let len2 = reduce(counter, 0, (mod.list2)); let len1 = len(mod.list1);
let len2 = len(mod.list2);
let rng = select (len1 >= len2), NULL, { let rng = select (len1 >= len2), NULL, {
true = 0:(len1 - 1), true = 0:(len1 - 1),

View File

@ -11,12 +11,12 @@ assert asserts.equal{
}; };
assert asserts.equal{ assert asserts.equal{
left=list.len{list=[0, 1, 2, 3]}.result, left=list.len([0, 1, 2, 3]),
right=4, right=4,
}; };
assert asserts.equal{ assert asserts.equal{
left = list.reverse{list=[0, 1, 2, 3]}.result, left = list.reverse([0, 1, 2, 3]),
right = [3, 2, 1, 0], right = [3, 2, 1, 0],
}; };

View File

@ -10,9 +10,7 @@ let fields = module{
// First we check that mod.tpl is a tuple. // First we check that mod.tpl is a tuple.
assert_tuple(mod.tpl); assert_tuple(mod.tpl);
let reducer = func (acc, field, value) => acc + [field]; let result = reduce(func (acc, field, value) => acc + [field], [], (mod.tpl));
let result = reduce(reducer, [], (mod.tpl));
}; };
// return a list of the values in a tuple. // return a list of the values in a tuple.
@ -23,9 +21,7 @@ let values = module{
// First we check that mod.tpl is a tuple. // First we check that mod.tpl is a tuple.
assert_tuple(mod.tpl); assert_tuple(mod.tpl);
let reducer = func (acc, field, value) => acc + [value]; let result = reduce(func (acc, field, value) => acc + [value], [], (mod.tpl));
let result = reduce(reducer, [], (mod.tpl));
}; };
let iter = module{ let iter = module{
@ -35,9 +31,7 @@ let iter = module{
// First we check that mod.tpl is a tuple. // First we check that mod.tpl is a tuple.
assert_tuple(mod.tpl); assert_tuple(mod.tpl);
let reducer = func (acc, field, value) => acc + [[field, value]]; let result = reduce(func (acc, field, value) => acc + [[field, value]], [], (mod.tpl));
let result = reduce(reducer, [], (mod.tpl));
}; };
let strip_nulls = module{ let strip_nulls = module{
@ -47,9 +41,7 @@ let strip_nulls = module{
// First we check that mod.tpl is a tuple. // First we check that mod.tpl is a tuple.
assert_tuple(mod.tpl); assert_tuple(mod.tpl);
let filterer = func (name, value) => value != NULL; let result = filter(func (name, value) => value != NULL, (mod.tpl));
let result = filter(filterer, (mod.tpl));
}; };
let has_fields = module{ let has_fields = module{
@ -62,9 +54,7 @@ let has_fields = module{
let fs = lib.fields{tpl=mod.tpl}.result; let fs = lib.fields{tpl=mod.tpl}.result;
let reducer = func (acc, f) => acc && (f in fs); let result = reduce(reducer = func (acc, f) => acc && (f in fs), true, (mod.fields));
let result = reduce(reducer, true, (mod.fields));
}; };
let field_type = module{ let field_type = module{
@ -86,8 +76,6 @@ let field_type = module{
false = fail "@ is not a string" % (mod.type), false = fail "@ is not a string" % (mod.type),
}; };
let has_field = lib.has_fields{tpl=mod.tpl, fields=[mod.field]}.result;
let it = lib.iter{tpl=mod.tpl}.result; let it = lib.iter{tpl=mod.tpl}.result;
let reducer = func (acc, l) => acc && (select l.0 == mod.field, NULL, { let reducer = func (acc, l) => acc && (select l.0 == mod.field, NULL, {
@ -95,7 +83,5 @@ let field_type = module{
false = true, // if this isn't the field then we propagate true false = true, // if this isn't the field then we propagate true
}); });
let is_type = reduce(reducer, true, it); let result = lib.has_fields{tpl=mod.tpl, fields=[mod.field]}.result && reduce(reducer, true, it);
let result = has_field && is_type;
}; };