2019-04-15 22:14:18 -05:00
|
|
|
// Wraps a string and provides operations for that string.
|
|
|
|
//
|
|
|
|
// * len - property representing the length of the string in characters.
|
|
|
|
//
|
|
|
|
// * str - property the wrapped string.
|
|
|
|
//
|
|
|
|
// * split_on - module that splits the string on a character.
|
|
|
|
// - `on` field represents the character to split on.
|
|
|
|
// - `str` field defaults to the wrapped string. you can override this
|
|
|
|
// if desired.
|
|
|
|
//
|
|
|
|
// * split_at - function splits the wrapped string at an character index.
|
|
|
|
//
|
|
|
|
// * substr - module that returns a substr of the wrapped string.
|
|
|
|
// - `start` field is the index at which the substr starts (defaults to 0)
|
|
|
|
// - `end` field is the index at which the substr ends (defaults to end of string)
|
2019-02-18 20:15:20 -06:00
|
|
|
let ops = module {
|
|
|
|
str="",
|
2019-04-15 22:14:18 -05:00
|
|
|
} => ({len=len,
|
|
|
|
str=str,
|
|
|
|
split_on=split_on,
|
|
|
|
split_at=split_at,
|
|
|
|
substr=substr,
|
|
|
|
}) {
|
2019-04-12 21:13:24 -05:00
|
|
|
let len = import "std/lists.ucg".len(mod.str);
|
2019-04-15 22:14:18 -05:00
|
|
|
let str = mod.str;
|
2019-04-12 21:13:24 -05:00
|
|
|
|
2019-02-18 20:15:20 -06:00
|
|
|
let split_on = module{
|
|
|
|
on=" ",
|
|
|
|
str=mod.str,
|
2019-02-24 08:16:07 -06:00
|
|
|
} => (result) {
|
2019-02-18 20:15:20 -06:00
|
|
|
let splitter = func(acc, char) => acc{
|
|
|
|
out = select char == mod.on, acc.out, {
|
|
|
|
true = acc.out + [acc.buf],
|
|
|
|
},
|
|
|
|
buf = select char != mod.on, "", {
|
|
|
|
true = acc.buf + char,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let accumulated = reduce(splitter, {out=[], buf=""}, mod.str);
|
|
|
|
|
|
|
|
let result = accumulated.out + [accumulated.buf];
|
|
|
|
};
|
2019-04-15 22:14:18 -05:00
|
|
|
|
2019-02-18 20:15:20 -06:00
|
|
|
let split_at = func(idx) => filter(
|
|
|
|
func(name, val) => name != "counter",
|
|
|
|
reduce(
|
|
|
|
func(acc, char) => acc{
|
|
|
|
counter = acc.counter + 1,
|
|
|
|
left = select acc.counter < idx, acc.left, {
|
|
|
|
true = acc.left + char,
|
|
|
|
},
|
|
|
|
right = select acc.counter >= idx, acc.right, {
|
|
|
|
true = acc.right + char,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{counter = 0, left = "", right = ""},
|
|
|
|
mod.str
|
|
|
|
)
|
|
|
|
);
|
2019-04-12 21:13:24 -05:00
|
|
|
|
|
|
|
let substr = module{
|
|
|
|
str = mod.str,
|
|
|
|
start = 0,
|
|
|
|
end = len,
|
|
|
|
} => (result) {
|
2019-04-24 19:30:10 -05:00
|
|
|
let pkg = mod.pkg();
|
2019-04-12 21:13:24 -05:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
};
|
2019-04-24 19:30:10 -05:00
|
|
|
let result = pkg.ops{str=reduce(
|
2019-04-15 22:14:18 -05:00
|
|
|
reducer, {counter = 0, str = ""}, mod.str).str};
|
2019-04-12 21:13:24 -05:00
|
|
|
};
|
2019-02-18 20:15:20 -06:00
|
|
|
};
|