import { value, func } from "./examples/file/import_from.txt";
func.(value)
value = {
"text": "testing",
"number": 123
};
func = { data =>
text = get(data, "text");
number = get(data, "number");
format("{} {}", text, number)
};
createList = {list, index, till =>
newList = push(list, index);
newIndex = index+1;
if(newIndex == till, newList, {=> createList.(newList, newIndex, till)})
};
// using range(150) is ofcourse better, because this will overflow to stack
createList.([], 0, 150);
Test::new = {=>
{
"y": [1,2,3],
"x": {
"x": 123
}
}
};
Test::get = {map, key =>
inner = get(map, key);
get(inner, key)
};
my_map = Test::new.();
Test::get.(my_map, "x")
// port from the python method used on https://en.wikipedia.org/wiki/Secant_method
secant_method = {f, x0, x1, iterations =>
result = reduce(range(iterations), [x0, x1], {prev_numbers =>
x0 = get(prev_numbers, 0);
x1 = get(prev_numbers, 1);
x2 = x1 - f.(x1) * (x1 - x0) / (f.(x1) - f.(x0));
[x1, x2]
});
get(result, 1)
};
my_func = {x =>
x ** 2 - 612
};
secant_method.(my_func, 10, 30, 5) == 24.738633748750722
MergeSort::sort = {list =>
if(length(list) < 2, list, {=>
lists = MergeSort::split_middle.(list);
left = get(lists, "left");
right = get(lists, "right");
left = MergeSort::sort.(left);
right = MergeSort::sort.(right);
MergeSort::merge.(left, right)
})
};
MergeSort::merge = {left, right =>
result = reduce(range(length(left) + length(right)), {"left": left, "right": right, "sorted": []}, {acc =>
left = get(acc, "left");
right = get(acc, "right");
sorted = get(acc, "sorted");
if(length(left) == 0, {"left": [], "right": [], "sorted": concat(sorted, right)}, {=>
if(length(right) == 0, {"left": [], "right": [], "sorted": concat(sorted, left)}, {=>
left_first = get(left, 0);
right_first = get(right, 0);
if(left_first < right_first, {=>
left = remove(left, 0);
sorted = push(sorted, left_first);
{"left": left, "right": right, "sorted": sorted}
}, {=>
right = remove(right, 0);
sorted = push(sorted, right_first);
{"left": left, "right": right, "sorted": sorted}
})
})
})
});
get(result, "sorted")
};
MergeSort::split_middle = {list =>
middle = length(list) / 2;
lists = reduce(list, {"left": [], "right": []}, {acc, x =>
left = get(acc, "left");
right = get(acc, "right");
if(
length(left) < middle,
{"left": push(left, x), "right": right},
{"left": left, "right": push(right, x)}
);
});
lists
};
data_list = shuffle(range(15));
// check if data is not already sorted
assert(data_list != range(15));
MergeSort::sort.(data_list)