FEATURE: Add len attribute and substr sub module for strings lib.

This commit is contained in:
Jeremy Wall 2019-04-12 21:13:24 -05:00
parent a8de207c9d
commit 76e8510f15
3 changed files with 40 additions and 2 deletions

View File

@ -48,9 +48,9 @@ let tail = func(list) => reduce(
).tail;
// Enumerates the provided list with optional start and step parameters for the
// enumeration. Prodices a list of pairs with the numeration and the list item.
// enumeration. Produces a list of pairs with the enumeration and the list item.
//
// enumerate{list=["a","b","c"]}.result == [[0, "a"], [1, "b"], [2, "c"]]
// enumerate{list=["a","b","c"]} == [[0, "a"], [1, "b"], [2, "c"]]
let enumerate = module{
// Where to start the enumeration.
start = 0,
@ -104,6 +104,7 @@ let slice = module {
};
// zips two lists together.
// The result list is only as long as the shortest list.
//
// zip{list1=[0,2,4],list2=[1,3,5]}.result == [[0, 1], [2, 3], [4, 5]]
let zip = module{

View File

@ -1,6 +1,8 @@
let ops = module {
str="",
} => {
let len = import "std/lists.ucg".len(mod.str);
let split_on = module{
on=" ",
str=mod.str,
@ -35,4 +37,19 @@ let ops = module {
mod.str
)
);
let substr = module{
str = mod.str,
start = 0,
end = len,
} => (result) {
let reducer = func(acc, char) => acc{
counter = acc.counter + 1,
str = select ((acc.counter >= mod.start) && (acc.counter <= mod.end)), acc.str, {
true = acc.str + char,
},
};
let result = reduce(
reducer, {counter = 0, str = ""}, mod.str).str;
};
};

View File

@ -21,4 +21,24 @@ assert asserts.equal{
assert asserts.equal{
left = str_class.split_at(3),
right = {left="foo", right=" bar"},
};
assert asserts.equal{
left = str_class.len,
right = 7,
};
assert asserts.equal{
left = str_class.substr{start=1},
right = "oo bar",
};
assert asserts.equal{
left = str_class.substr{end=5},
right = "foo ba",
};
assert asserts.equal{
left = str_class.substr{end=8},
right = "foo bar",
};