Skip to content

Commit

Permalink
standardize common helpers across all apps
Browse files Browse the repository at this point in the history
In particular, I merged take_out in anagrams.tlv with all_but in
graphviz.
  • Loading branch information
akkartik committed Mar 19, 2022
1 parent ad0ce8b commit 7859317
Show file tree
Hide file tree
Showing 9 changed files with 883 additions and 62 deletions.
118 changes: 99 additions & 19 deletions anagrams.tlv
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,36 @@
>end
>
>-- TODO: backport utf-8 support from Lua 5.3
- __teliva_timestamp: original
debugy:
>debugy = 5
- __teliva_timestamp: original
dbg:
>-- helper for debug by print; overlay debug information towards the right
>-- reset debugy every time you refresh screen
>function dbg(window, s)
> local oldy = 0
> local oldx = 0
> oldy, oldx = window:getyx()
> window:mvaddstr(debugy, 60, s)
> debugy = debugy+1
> window:mvaddstr(oldy, oldx, '')
>end
- __teliva_timestamp: original
check:
>function check(x, msg)
> if x then
> Window:addch('.')
> else
> print('F - '..msg)
> print(' '..str(x)..' is false/nil')
> teliva_num_test_failures = teliva_num_test_failures + 1
> -- overlay first test failure on editors
> if teliva_first_failure == nil then
> teliva_first_failure = msg
> end
> end
>end
- __teliva_timestamp: original
check_eq:
>function check_eq(x, expected, msg)
Expand All @@ -90,8 +120,13 @@
> if b[k] ~= v then
> return false
> end
> return true
> end
> for k, v in pairs(b) do
> if a[k] ~= v then
> return false
> end
> end
> return true
> end
> return a == b
>end
Expand Down Expand Up @@ -221,6 +256,67 @@
> table.insert(l, 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)
> while #lines > 0 do
> table.remove(lines)
> end
>end
- __teliva_timestamp: original
zap:
>function zap(target, src)
> clear(target)
> append(target, src)
>end
- __teliva_timestamp: original
menu:
>-- To show app-specific hotkeys in the menu bar, add hotkey/command
Expand Down Expand Up @@ -317,27 +413,11 @@
> local result = {}
> for i=1, #s do
> if i == 1 or s[i] ~= s[i-1] then
> append(result, combine(s[i], gather(take_out(s, i))))
> append(result, combine(s[i], gather(all_but(s, i))))
> end
> end
> return result
>end
- __teliva_timestamp:
>Mon Feb 21 18:06:20 2022
take_out:
>function take_out(s, i)
> if i < 1 then return s:sub(1) end
> return s:sub(1, i-1) .. s:sub(i+1)
>end
>
>function test_take_out()
> check_eq(take_out('', 0), '', 'take_out: empty')
> check_eq(take_out('abc', 0), 'abc', 'take_out: invalid low index')
> check_eq(take_out('abc', 4), 'abc', 'take_out: invalid high index')
> check_eq(take_out('abc', 1), 'bc', 'take_out: first index')
> check_eq(take_out('abc', 3), 'ab', 'take_out: final index')
> check_eq(take_out('abc', 2), 'ac', 'take_out: middle index')
>end
- __teliva_timestamp: original
__teliva_note:
>basic version
Expand Down Expand Up @@ -439,7 +519,7 @@
> local result = {}
> for i=1, #s do
> if i == 1 or s[i] ~= s[i-1] then
> local subresult = gather(take_out(s, i))
> local subresult = gather(all_but(s, i))
> if subresult == nil then return nil end -- interrupted
> append(result, combine(s[i], subresult))
> end
Expand Down
68 changes: 67 additions & 1 deletion break.tlv
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,13 @@
> if b[k] ~= v then
> return false
> end
> return true
> end
> for k, v in pairs(b) do
> if a[k] ~= v then
> return false
> end
> end
> return true
> end
> return a == b
>end
Expand Down Expand Up @@ -251,6 +256,67 @@
> table.insert(l, 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)
> while #lines > 0 do
> table.remove(lines)
> end
>end
- __teliva_timestamp: original
zap:
>function zap(target, src)
> clear(target)
> append(target, src)
>end
- __teliva_timestamp: original
menu:
>-- To show app-specific hotkeys in the menu bar, add hotkey/command
Expand Down
Loading

0 comments on commit 7859317

Please sign in to comment.