use method syntax where possible

Perhaps this is a bad idea. It feels arbitrary, what methods Lua happens
to include in string and table objects without having to go through the
respective modules.
This commit is contained in:
Kartik K. Agaram 2022-03-06 16:38:49 -08:00
parent 5758f6c082
commit 6a3098d0e9
9 changed files with 80 additions and 80 deletions

View File

@ -23,7 +23,7 @@
>-- index characters using []
>getmetatable('').__index = function(str,i)
> if type(i) == 'number' then
> return string.sub(str,i,i)
> return str:sub(i,i)
> else
> return string[i]
> end
@ -32,11 +32,11 @@
>-- ranges using (), selected bytes using {}
>getmetatable('').__call = function(str,i,j)
> if type(i)~='table' then
> return string.sub(str,i,j)
> return str:sub(i,j)
> else
> local t={}
> for k,v in ipairs(i) do
> t[k]=string.sub(str,v,v)
> t[k]=str:sub(v,v)
> end
> return table.concat(t)
> end
@ -186,7 +186,7 @@
count_letters:
>function count_letters(s)
> local result = {}
> for i=1,string.len(s) do
> for i=1,s:len() do
> local c = s[i]
> if result[c] == nil then
> result[c] = 1
@ -309,8 +309,8 @@
>Mon Feb 21 18:06:20 2022
take_out:
>function take_out(s, i)
> if i < 1 then return string.sub(s, 1) end
> return string.sub(s, 1, i-1) .. string.sub(s, i+1)
> if i < 1 then return s:sub(1) end
> return s:sub(1, i-1) .. s:sub(i+1)
>end
>
>function test_take_out()
@ -342,7 +342,7 @@
>Sat Mar 5 15:24:00 2022
count_anagrams:
>function count_anagrams(s)
> local result = factorial(string.len(s))
> local result = factorial(s:len())
> local letter_counts = count_letters(s)
> for k, v in pairs(letter_counts) do
> result = result / factorial(v)

View File

@ -23,7 +23,7 @@
>-- index characters using []
>getmetatable('').__index = function(str,i)
> if type(i) == 'number' then
> return string.sub(str,i,i)
> return str:sub(i,i)
> else
> return string[i]
> end
@ -32,13 +32,13 @@
>-- ranges using (), selected bytes using {}
>getmetatable('').__call = function(str,i,j)
> if type(i)~='table' then
> return string.sub(str,i,j)
> return str:sub(i,j)
> else
> local t={}
> for k,v in ipairs(i) do
> t[k]=string.sub(str,v,v)
> t[k]=str:sub(v,v)
> end
> return table.concat(t)
> return t:concat()
> end
>end
>

View File

@ -23,7 +23,7 @@
>-- index characters using []
>getmetatable('').__index = function(str,i)
> if type(i) == 'number' then
> return string.sub(str,i,i)
> return str:sub(i,i)
> else
> return string[i]
> end
@ -32,11 +32,11 @@
>-- ranges using (), selected bytes using {}
>getmetatable('').__call = function(str,i,j)
> if type(i)~='table' then
> return string.sub(str,i,j)
> return str:sub(i,j)
> else
> local t={}
> for k,v in ipairs(i) do
> t[k]=string.sub(str,v,v)
> t[k]=str:sub(v,v)
> end
> return table.concat(t)
> end
@ -158,14 +158,14 @@
render_line:
>function render_line(window, y, line)
> window:mvaddstr(y, 0, '')
> for i=1,string.len(line) do
> for i=1,line:len() do
> window:addstr(line[i])
> end
>end
- __teliva_timestamp: original
render_link:
>function render_link(window, y, line)
> local rendered_line = string.gsub(line, '=>%s*%S*%s*', '')
> local rendered_line = line:gsub('=>%s*%S*%s*', '')
> if trim(rendered_line) == '' then
> rendered_line = line
> end
@ -189,7 +189,7 @@
> y = y+2
>--? dbg(window, state.highlight_index)
> for i, line in pairs(state.lines) do
> if not state.source and string.find(line, '=> ') == 1 then
> if not state.source and line:find('=> ') == 1 then
> if state.highlight_index == 0 or i == state.highlight_index then
> -- highlighted link
> state.highlight_index = i -- TODO: ugly state update while rendering, just for first render after gemini_get
@ -276,7 +276,7 @@
- __teliva_timestamp: original
is_link:
>function is_link(line)
> return string.find(line, '=>%s*%S*%s*') == 1
> return line:find('=>%s*%S*%s*') == 1
>end
- __teliva_timestamp: original
next_link:
@ -384,11 +384,11 @@
> local s, status = tcp:receive()
> if s == nil then break end
> if s == '' then break end
> local header, value = string.match(s, '(.-): (.*)')
> local header, value = s:match('(.-): (.*)')
> if header == nil then
> print(s)
> else
> headers[string.lower(header)] = value
> headers[header:lower()] = value
> print(header, value)
> end
> end
@ -398,7 +398,7 @@
> local s, status = tcp:receive(bytes_remaining)
> if s == nil then break end
> body = body .. s
> bytes_remaining = bytes_remaining - string.len(s)
> bytes_remaining = bytes_remaining - s:len()
> if bytes_remaining <= 0 then break end
> end
> return body
@ -436,7 +436,7 @@
> if line == nil then break end
> table.insert(state.lines, line)
> end
> elseif string.sub(type, 1, 5) == 'text/' then
> elseif type:sub(1, 5) == 'text/' then
> while true do
> local line, err = conn:receive()
> if line == nil then break end
@ -450,7 +450,7 @@
>-- https://tildegit.org/solderpunk/gemini-demo-2
>-- returns an array of lines, containing either the body or just an error
>function gemini_get(url)
> if string.find(url, "://") == nil then
> if url:find("://") == nil then
> url = "gemini://" .. url
> end
> local parsed_url = socket.url.parse(url)
@ -480,7 +480,7 @@
> table.insert(state.lines, err)
> return
> end
> local status, meta = string.match(line, "(%S+) (%S+)")
> local status, meta = line:match("(%S+) (%S+)")
> if status[1] == '2' then
> parse_gemini_body(conn, meta)
> state.url = url

View File

@ -23,7 +23,7 @@
>-- index characters using []
>getmetatable('').__index = function(str,i)
> if type(i) == 'number' then
> return string.sub(str,i,i)
> return str:sub(i,i)
> else
> return string[i]
> end
@ -32,11 +32,11 @@
>-- ranges using (), selected bytes using {}
>getmetatable('').__call = function(str,i,j)
> if type(i)~='table' then
> return string.sub(str,i,j)
> return str:sub(i,j)
> else
> local t={}
> for k,v in ipairs(i) do
> t[k]=string.sub(str,v,v)
> t[k]=str:sub(v,v)
> end
> return table.concat(t)
> end
@ -202,7 +202,7 @@
count_letters:
>function count_letters(s)
> local result = {}
> for i=1,string.len(s) do
> for i=1,s:len() do
> local c = s[i]
> if result[c] == nil then
> result[c] = 1
@ -255,7 +255,7 @@
> end
> end
> h.addstr = function(self, s)
> for i=1,string.len(s) do
> for i=1,s:len() do
> self:addch(s[i])
> end
> end
@ -275,7 +275,7 @@
kbd:
>function kbd(keys)
> local result = {}
> for i=1,string.len(keys) do
> for i=1,keys:len() do
> table.insert(result, keys[i])
> end
> return result
@ -298,7 +298,7 @@
check_screen:
>function check_screen(window, contents, message)
> local x, y = 1, 1
> for i=1,string.len(contents) do
> for i=1,contents:len() do
> check_eq(contents[i], window.scr[y][x], message..'/'..y..','..x)
> x = x+1
> if x > window.scr.w then
@ -438,7 +438,7 @@
- __teliva_timestamp: original
state:
>function state(line, col)
> if line < 1 or line > table.getn(grid) or col < 1 or col > table.getn(grid[1]) then
> if line < 1 or line > #grid or col < 1 or col > #grid[1] then
> return 0
> end
> return grid[line][col]
@ -454,9 +454,9 @@
step:
>function step()
> local new_grid = {}
> for line=1,table.getn(grid) do
> for line=1,#grid do
> new_grid[line] = {}
> for col=1,table.getn(grid[1]) do
> for col=1,#grid[1] do
> local n = num_live_neighbors(line, col)
> if n == 3 then
> new_grid[line][col] = 1

View File

@ -23,7 +23,7 @@
>-- index characters using []
>getmetatable('').__index = function(str,i)
> if type(i) == 'number' then
> return string.sub(str,i,i)
> return str:sub(i,i)
> else
> return string[i]
> end
@ -32,11 +32,11 @@
>-- ranges using (), selected bytes using {}
>getmetatable('').__call = function(str,i,j)
> if type(i)~='table' then
> return string.sub(str,i,j)
> return str:sub(i,j)
> else
> local t={}
> for k,v in ipairs(i) do
> t[k]=string.sub(str,v,v)
> t[k]=str:sub(v,v)
> end
> return table.concat(t)
> end

View File

@ -56,9 +56,9 @@ function character_splitting_task(chanin, chanout)
while true do
local line = chanin:recv()
if line == nil then break end
local linesz = string.len(line)
local linesz = line:len()
for i=1,linesz do
chanout:send(string.sub(line, i, i))
chanout:send(line:sub(i, i))
end
end
chanout:send(nil) -- end of file

View File

@ -23,7 +23,7 @@
>-- index characters using []
>getmetatable('').__index = function(str,i)
> if type(i) == 'number' then
> return string.sub(str,i,i)
> return str:sub(i,i)
> else
> return string[i]
> end
@ -32,11 +32,11 @@
>-- ranges using (), selected bytes using {}
>getmetatable('').__call = function(str,i,j)
> if type(i)~='table' then
> return string.sub(str,i,j)
> return str:sub(i,j)
> else
> local t={}
> for k,v in ipairs(i) do
> t[k]=string.sub(str,v,v)
> t[k]=str:sub(v,v)
> end
> return table.concat(t)
> end
@ -201,7 +201,7 @@
count_letters:
>function count_letters(s)
> local result = {}
> for i=1,string.len(s) do
> for i=1,s:len() do
> local c = s[i]
> if result[c] == nil then
> result[c] = 1
@ -257,7 +257,7 @@
> end
> end
> h.addstr = function(self, s)
> for i=1,string.len(s) do
> for i=1,s:len() do
> self:addch(s[i])
> end
> end
@ -277,7 +277,7 @@
kbd:
>function kbd(keys)
> local result = {}
> for i=1,string.len(keys) do
> for i=1,keys:len() do
> table.insert(result, keys[i])
> end
> return result
@ -299,7 +299,7 @@
check_screen:
>function check_screen(window, contents, message)
> local x, y = 1, 1
> for i=1,string.len(contents) do
> for i=1,contents:len() do
> check_eq(contents[i], window.scr[y][x], message..'/'..y..','..x)
> x = x+1
> if x > window.scr.w then

View File

@ -23,7 +23,7 @@
>-- index characters using []
>getmetatable('').__index = function(str,i)
> if type(i) == 'number' then
> return string.sub(str,i,i)
> return str:sub(i,i)
> else
> return string[i]
> end
@ -32,11 +32,11 @@
>-- ranges using (), selected bytes using {}
>getmetatable('').__call = function(str,i,j)
> if type(i)~='table' then
> return string.sub(str,i,j)
> return str:sub(i,j)
> else
> local t={}
> for k,v in ipairs(i) do
> t[k]=string.sub(str,v,v)
> t[k]=str:sub(v,v)
> end
> return table.concat(t)
> end
@ -235,7 +235,7 @@
> pos = render_text(window, toot, pos, cursor)
> print('')
> window:attron(curses.A_BOLD)
> window:addstr(string.len(toot))
> window:addstr(toot:len())
> window:attroff(curses.A_BOLD)
> end
> window:refresh()
@ -244,7 +244,7 @@
render_delimiter:
>function render_delimiter(window, s, pos, cursor)
> local newpos = pos
> for i=1,string.len(s) do
> for i=1,s:len() do
> if newpos == cursor and i ~= 1 then
> if s[i] == '\n' then
> -- newline at cursor = render extra space in reverse video before jumping to new line
@ -290,7 +290,7 @@
>function render_text(window, s, pos, cursor)
> local newpos = pos
>--? dbg(window, '--')
> for i=1,string.len(s) do
> for i=1,s:len() do
>--? dbg(window, tostring(newpos)..' '..tostring(string.byte(s[i])))
> if newpos == cursor then
>--? dbg(window, 'cursor: '..tostring(cursor))

56
zet.tlv
View File

@ -23,7 +23,7 @@
>-- index characters using []
>getmetatable('').__index = function(str,i)
> if type(i) == 'number' then
> return string.sub(str,i,i)
> return str:sub(i,i)
> else
> return string[i]
> end
@ -32,11 +32,11 @@
>-- ranges using (), selected bytes using {}
>getmetatable('').__call = function(str,i,j)
> if type(i)~='table' then
> return string.sub(str,i,j)
> return str:sub(i,j)
> else
> local t={}
> for k,v in ipairs(i) do
> t[k]=string.sub(str,v,v)
> t[k]=str:sub(v,v)
> end
> return table.concat(t)
> end
@ -201,7 +201,7 @@
count_letters:
>function count_letters(s)
> local result = {}
> for i=1,string.len(s) do
> for i=1,s:len() do
> local c = s[i]
> if result[c] == nil then
> result[c] = 1
@ -259,7 +259,7 @@
> end
> end
> h.addstr = function(self, s)
> for i=1,string.len(s) do
> for i=1,s:len() do
> self:addch(s[i])
> end
> end
@ -279,7 +279,7 @@
kbd:
>function kbd(keys)
> local result = {}
> for i=1,string.len(keys) do
> for i=1,keys:len() do
> table.insert(result, keys[i])
> end
> return result
@ -301,7 +301,7 @@
check_screen:
>function check_screen(window, contents, message)
> local x, y = 1, 1
> for i=1,string.len(contents) do
> for i=1,contents:len() do
> check_eq(contents[i], window.scr[y][x], message..'/'..y..','..x)
> x = x+1
> if x > window.scr.w then
@ -595,7 +595,7 @@
> end
> local y, x = top, left + 1 -- left padding; TODO: indent
> window:mvaddstr(y, x, '')
> for i=1,string.len(s) do
> for i=1,s:len() do
> -- render character
> if i == cursor then
> if s[i] == '\n' then
@ -635,7 +635,7 @@
> end
> end
> end
> if cursor > string.len(s) then
> if cursor > s:len() then
> window:attron(curses.A_REVERSE)
> window:addch(' ')
> window:attroff(curses.A_REVERSE)
@ -676,7 +676,7 @@
- __teliva_timestamp: original
cursor_down:
>function cursor_down(s, old_idx, width)
> local max = string.len(s)
> local max = s:len()
> local i = 1
> -- compute oldcol, the screen column of old_idx
> local oldcol = 0
@ -767,7 +767,7 @@
>initial commit: show/edit zettels
cursor_up:
>function cursor_up(s, old_idx, width)
> local max = string.len(s)
> local max = s:len()
> local i = 1
> -- compute oldcol, the screen column of old_idx
> local oldcol = 0
@ -969,7 +969,7 @@
> end
> local y, x = top, left + 1 -- left padding; TODO: indent
> window:mvaddstr(y, x, '')
> for i=1,string.len(s) do
> for i=1,s:len() do
> if i == cursor then
> cursor_y = y
> cursor_x = x
@ -1010,7 +1010,7 @@
> local bottom = top + view_settings.height
> local left = (render_state.curr_w - 1) * (view_settings.width + view_settings.hmargin)
> local right = left + view_settings.width
> local cursor = string.len(zettels[current_zettel_id].data)+1
> local cursor = zettels[current_zettel_id].data:len()+1
> local quit = false
> curses.curs_set(1)
> while not quit do
@ -2482,7 +2482,7 @@
> end
> local y, x = top, left + 1 -- left padding; TODO: indent
> window:mvaddstr(y, x, '')
> for i=1,string.len(s) do
> for i=1,s:len() do
> if i == cursor then
> cursor_y = y
> cursor_x = x
@ -2744,12 +2744,12 @@
> {'^l', 'line>>'},
> {'^k', 'del to line>>'},
> }
> local old_data = string.sub(zettels[current_zettel_id].data, 1)
> local old_data = zettels[current_zettel_id].data:sub(1)
> local top = (render_state.curr_h - 1) * (view_settings.height + view_settings.vmargin)
> local bottom = top + view_settings.height
> local left = (render_state.curr_w - 1) * (view_settings.width + view_settings.hmargin)
> local right = left + view_settings.width
> local cursor = string.len(zettels[current_zettel_id].data)+1
> local cursor = zettels[current_zettel_id].data:len()+1
> local quit = false
> curses.curs_set(1)
> while not quit do
@ -2791,7 +2791,7 @@
> cursor = cursor-1
> end
> elseif key == 12 then -- ctrl-l
> local max = string.len(prose)
> local max = prose:len()
> while cursor <= max and prose[cursor] ~= '\n' do
> cursor = cursor+1
> end
@ -2799,7 +2799,7 @@
> elseif key == 2 then -- ctrl-b
> -- delete
> elseif key == 11 then -- ctrl-k
> while cursor <= string.len(prose) and prose[cursor] ~= '\n' do
> while cursor <= prose:len() and prose[cursor] ~= '\n' do
> prose = prose:remove(cursor)
> end
> -- exit
@ -2848,33 +2848,33 @@
> end
> elseif key == 12 then -- ctrl-l
> -- to end of line
> local max = string.len(prose)
> local max = prose:len()
> while cursor <= max and prose[cursor] ~= '\n' do
> cursor = cursor+1
> end
> elseif key == 6 then -- ctrl-f
> -- to next word
> local max = string.len(prose)
> while cursor <= max and string.match(prose[cursor], '%w') do
> local max = prose:len()
> while cursor <= max and prose[cursor]:match('%w') do
> cursor = cursor+1
> end
> while cursor <= max and string.match(prose[cursor], '%W') do
> while cursor <= max and prose[cursor]:match('%W') do
> cursor = cursor+1
> end
> elseif key == 2 then -- ctrl-b
> -- to previous word
> if cursor > string.len(prose) then
> cursor = string.len(prose)
> if cursor > prose:len() then
> cursor = prose:len()
> end
> while cursor > 1 and string.match(prose[cursor], '%W') do
> while cursor > 1 and prose[cursor]:match('%W') do
> cursor = cursor-1
> end
> while cursor > 1 and string.match(prose[cursor], '%w') do
> while cursor > 1 and prose[cursor]:match('%w') do
> cursor = cursor-1
> end
> -- delete
> elseif key == 11 then -- ctrl-k
> while cursor <= string.len(prose) and prose[cursor] ~= '\n' do
> while cursor <= prose:len() and prose[cursor] ~= '\n' do
> prose = prose:remove(cursor)
> end
> -- exit
@ -2906,7 +2906,7 @@
> end
> local y, x = top, left + 1 -- left padding; TODO: indent
> window:mvaddstr(y, x, '')
> for i=1,string.len(s) do
> for i=1,s:len() do
> if i == cursor then
> cursor_y = y
> cursor_x = x