# listkit: helpers for working with lists. # Pure: it needs no permissions, so it can be installed with no `allow` line. # # use listkit from "cresset_packages/listkit.crs" # print(listkit.windows([1, 2, 3, 4], 2)) # [[1, 2], [2, 3], [3, 4]] # every run of `size` neighbouring items fn windows(items, size): out = [] if size < 1: return out for i in range(items.len() - size + 1): out.push(items.slice(i, i + size)) return out # pairs of neighbours: [1, 2, 3] becomes [[1, 2], [2, 3]] fn pairwise(items): return windows(items, 2) # move items around the end: rotate([1, 2, 3, 4], 1) is [2, 3, 4, 1] fn rotate(items, steps): if items.len() == 0: return [] k = steps % items.len() return items.slice(k) + items.slice(0, k) fn product(items): total = 1 for x in items: total *= x return total # every pairing of an item from `a` with one from `b` fn cartesian(a, b): out = [] for x in a: for y in b: out.push([x, y]) return out # rows become columns fn transpose(rows): if rows.len() == 0: return [] out = [] for j in range(rows[0].len()): column = [] for row in rows: column.push(row[j]) out.push(column) return out # lists inside lists inside lists, all flattened into one fn flatten_deep(items): out = [] for x in items: if type(x) == "list": out = out + flatten_deep(x) else: out.push(x) return out # a map of each distinct item to how many times it appears fn frequencies(items): counts = {} for x in items: counts[x] = counts.get(x, 0) + 1 return counts # numbers from `start` up to (not including) `stop`, `step` apart; a negative step counts down fn range_step(start, stop, step): out = [] if step == 0: return out n = start while (step > 0 and n < stop) or (step < 0 and n > stop): out.push(n) n += step return out # the item `score` likes best (the first one, when there is a tie), or none for an empty list fn best_by(items, score): best = none best_score = none for x in items: s = score(x) if best_score == none or s > best_score: best = x best_score = s return best test "windows and pairs": assert_eq(windows([1, 2, 3, 4], 2), [[1, 2], [2, 3], [3, 4]]) assert_eq(windows([1, 2], 3), []) assert_eq(windows([1, 2, 3], 0), []) assert_eq(pairwise([1, 2, 3]), [[1, 2], [2, 3]]) test "rotate": assert_eq(rotate([1, 2, 3, 4], 1), [2, 3, 4, 1]) assert_eq(rotate([1, 2, 3, 4], 5), [2, 3, 4, 1]) assert_eq(rotate([1, 2, 3, 4], -1), [4, 1, 2, 3]) assert_eq(rotate([], 3), []) test "product and cartesian": assert_eq(product([2, 3, 4]), 24) assert_eq(product([]), 1) assert_eq(cartesian([1, 2], ["a", "b"]), [[1, "a"], [1, "b"], [2, "a"], [2, "b"]]) test "transpose and flatten": assert_eq(transpose([[1, 2, 3], [4, 5, 6]]), [[1, 4], [2, 5], [3, 6]]) assert_eq(transpose([]), []) assert_eq(flatten_deep([1, [2, [3, [4]]], 5]), [1, 2, 3, 4, 5]) test "frequencies": assert_eq(frequencies(["a", "b", "a"]), {"a": 2, "b": 1}) assert_eq(frequencies([]), {}) test "range with a step": assert_eq(range_step(0, 10, 3), [0, 3, 6, 9]) assert_eq(range_step(5, 0, -2), [5, 3, 1]) assert_eq(range_step(0, 5, 0), []) test "best_by keeps the first of a tie": assert_eq(best_by(["aa", "b", "cc"], s -> s.len()), "aa") assert_eq(best_by([], s -> s), none)