From b56590ddc986ab07ce105fef6c5ab8d48c8599cd Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sun, 27 Mar 2022 11:42:19 -0700 Subject: some more reorg of the standard library Now life.tlv and gemini.tlv are also minimizing how much of the standard library they pull in, just to be easy to read. --- gemini.tlv | 252 ------------------------------------------------------------- 1 file changed, 252 deletions(-) (limited to 'gemini.tlv') diff --git a/gemini.tlv b/gemini.tlv index 15e16f8..72fc387 100644 --- a/gemini.tlv +++ b/gemini.tlv @@ -148,49 +148,6 @@ > end > return tostring(x) >end -- __teliva_timestamp: original - map: - >-- only for arrays - >function map(l, f) - > result = {} - > for _, x in ipairs(l) do - > table.insert(result, f(x)) - > end - > return result - >end -- __teliva_timestamp: original - reduce: - >-- only for arrays - >function reduce(l, f, init) - > result = init - > for _, x in ipairs(l) do - > result = f(result, x) - > end - > return result - >end -- __teliva_timestamp: original - filter: - >function filter(h, f) - > result = {} - > for k, v in pairs(h) do - > if f(k, v) then - > result[k] = v - > end - > end - > return result - >end -- __teliva_timestamp: original - ifilter: - >-- only for arrays - >function ifilter(l, f) - > result = {} - > for _, x in ipairs(l) do - > if f(x) then - > table.insert(result, x) - > end - > end - > return result - >end - __teliva_timestamp: original find_index: >function find_index(arr, x) @@ -214,159 +171,6 @@ > end > return result >end -- __teliva_timestamp: original - sort_letters: - >function sort_letters(s) - > tmp = {} - > for i=1,#s do - > table.insert(tmp, s[i]) - > end - > table.sort(tmp) - > local result = '' - > for _, c in pairs(tmp) do - > result = result..c - > end - > return result - >end - > - >function test_sort_letters(s) - > check_eq(sort_letters(''), '', 'test_sort_letters: empty') - > check_eq(sort_letters('ba'), 'ab', 'test_sort_letters: non-empty') - > check_eq(sort_letters('abba'), 'aabb', 'test_sort_letters: duplicates') - >end -- __teliva_timestamp: original - count_letters: - >-- TODO: handle unicode - >function count_letters(s) - > local result = {} - > for i=1,s:len() do - > local c = s[i] - > if result[c] == nil then - > result[c] = 1 - > else - > result[c] = result[c] + 1 - > end - > end - > return result - >end -- __teliva_timestamp: original - count: - >-- turn an array of elements into a map from elements to their frequency - >-- analogous to count_letters for non-strings - >function count(a) - > local result = {} - > for i, v in ipairs(a) do - > if result[v] == nil then - > result[v] = 1 - > else - > result[v] = result[v] + 1 - > end - > end - > return result - >end -- __teliva_timestamp: original - union: - >function union(a, b) - > for k, v in pairs(b) do - > a[k] = v - > end - > return a - >end -- __teliva_timestamp: original - subtract: - >-- set subtraction - >function subtract(a, b) - > for k, v in pairs(b) do - > a[k] = nil - > end - > return a - >end -- __teliva_timestamp: original - all: - >-- universal quantifier on sets - >function all(s, f) - > for k, v in pairs(s) do - > if not f(k, v) then - > return false - > end - > end - > return true - >end -- __teliva_timestamp: original - to_array: - >-- turn a set into an array - >-- drops values - >function to_array(h) - > local result = {} - > for k, _ in pairs(h) do - > table.insert(result, k) - > end - > return result - >end -- __teliva_timestamp: original - append: - >-- concatenate list 'elems' into 'l', modifying 'l' in the process - >function append(l, elems) - > for i=1,#elems do - > table.insert(l, elems[i]) - > end - >end -- __teliva_timestamp: original - prepend: - >-- concatenate list 'elems' into the start of 'l', modifying 'l' in the process - >function prepend(l, elems) - > for i=1,#elems do - > table.insert(l, i, elems[i]) - > end - >end -- __teliva_timestamp: original - all_but: - >function all_but(x, idx) - > if type(x) == 'table' then - > local result = {} - > for i, elem in ipairs(x) do - > if i ~= idx then - > table.insert(result,elem) - > end - > end - > return result - > elseif type(x) == 'string' then - > if idx < 1 then return x:sub(1) end - > return x:sub(1, idx-1) .. x:sub(idx+1) - > else - > error('all_but: unsupported type '..type(x)) - > end - >end - > - >function test_all_but() - > check_eq(all_but('', 0), '', 'all_but: empty') - > check_eq(all_but('abc', 0), 'abc', 'all_but: invalid low index') - > check_eq(all_but('abc', 4), 'abc', 'all_but: invalid high index') - > check_eq(all_but('abc', 1), 'bc', 'all_but: first index') - > check_eq(all_but('abc', 3), 'ab', 'all_but: final index') - > check_eq(all_but('abc', 2), 'ac', 'all_but: middle index') - >end -- __teliva_timestamp: original - set: - >function set(l) - > local result = {} - > for i, elem in ipairs(l) do - > result[elem] = true - > end - > return result - >end -- __teliva_timestamp: original - set_eq: - >function set_eq(l1, l2) - > return eq(set(l1), set(l2)) - >end - > - >function test_set_eq() - > check(set_eq({1}, {1}), 'set_eq: identical') - > check(not set_eq({1, 2}, {1, 3}), 'set_eq: different') - > check(set_eq({1, 2}, {2, 1}), 'set_eq: order') - > check(set_eq({1, 2, 2}, {2, 1}), 'set_eq: duplicates') - >end - __teliva_timestamp: original clear: >function clear(lines) @@ -374,62 +178,6 @@ > table.remove(lines) > end >end -- __teliva_timestamp: original - zap: - >function zap(target, src) - > clear(target) - > append(target, src) - >end -- __teliva_timestamp: original - mfactorial: - >-- memoized version of factorial - >-- doesn't memoize recursive calls, but may be good enough - >mfactorial = memo1(factorial) -- __teliva_timestamp: original - factorial: - >function factorial(n) - > local result = 1 - > for i=1,n do - > result = result*i - > end - > return result - >end -- __teliva_timestamp: original - memo1: - >-- a higher-order function that takes a function of a single arg - >-- (that never returns nil) - >-- and returns a memoized version of it - >function memo1(f) - > local memo = {} - > return function(x) - > if memo[x] == nil then - > memo[x] = f(x) - > end - > return memo[x] - > end - >end - > - >-- mfactorial doesn't seem noticeably faster - >function test_memo1() - > for i=0,30 do - > check_eq(mfactorial(i), factorial(i), 'memo1 over factorial: '..str(i)) - > end - >end -- __teliva_timestamp: original - num_permutations: - >-- number of permutations of n distinct objects, taken r at a time - >function num_permutations(n, r) - > return factorial(n)/factorial(n-r) - >end - > - >-- mfactorial doesn't seem noticeably faster - >function test_memo1() - > for i=0,30 do - > for j=0,i do - > check_eq(num_permutations(i, j), mfactorial(i)/mfactorial(i-j), 'num_permutations memoizes: '..str(i)..'P'..str(j)) - > end - > end - >end - __teliva_timestamp: original Window: >Window = curses.stdscr() -- cgit 1.4.1-2-gfad0