diff --git a/std/lists.ucg b/std/lists.ucg index 95a2e2a..8eea8cd 100644 --- a/std/lists.ucg +++ b/std/lists.ucg @@ -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{ diff --git a/std/strings.ucg b/std/strings.ucg index adce034..03790ca 100644 --- a/std/strings.ucg +++ b/std/strings.ucg @@ -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; + }; }; \ No newline at end of file diff --git a/std/tests/strings_test.ucg b/std/tests/strings_test.ucg index 85e2b2e..24af532 100644 --- a/std/tests/strings_test.ucg +++ b/std/tests/strings_test.ucg @@ -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", }; \ No newline at end of file