From b4c23abbfa05b0bc4df8180948f65f9ffc9c4c3e Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Wed, 27 Nov 2019 22:17:35 -0600 Subject: [PATCH] DEV: Make list slices faster for larger lists. --- std/lists.ucg | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/std/lists.ucg b/std/lists.ucg index 8907db6..e739ac8 100644 --- a/std/lists.ucg +++ b/std/lists.ucg @@ -82,25 +82,21 @@ let slice = module { let list_len = list.len(mod.list); let end = select (mod.end is "null", mod.end) => { - true = list_len, + true = list_len - 1, }; + let start = mod.start; // ensure some invariants - (mod.start >= 0) || fail "Slice must be positive"; - (mod.start <= list_len) || fail "Slice start cannot be larger than the list len of @" % (list_len); + (start >= 0) || fail "Slice must be positive"; + (start <= list_len) || fail "Slice start cannot be larger than the list len of @" % (list_len); (end <= list_len) || fail "Slice end cannot be larger than list len of @" % (list_len); - let reducer = func(acc, item) => acc{ - count = acc.count + 1, - list = select ((acc.count >= mod.start) && (acc.count <= end), acc.list) => { - true = acc.list + [item], - }, - }; + let reducer = func(acc, item) => acc + [mod.list.(item)]; let result = reduce( reducer, - {count=mod.start, list=[]}, - mod.list).list; + [], + start:end); }; // zips two lists together.