From 9c102c6f74f8968001e843af7edeb4f324bf104a Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 11 Apr 2024 12:50:31 -0700 Subject: [PATCH] a helper to add patterns without increasing indent --- 0020-load_results | 27 ++++++++++++++------------- 0029-do_if_match | 6 ++++++ 2 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 0029-do_if_match diff --git a/0020-load_results b/0020-load_results index a75bb08..6e5ffe8 100644 --- a/0020-load_results +++ b/0020-load_results @@ -4,20 +4,21 @@ load_results = function(filename) local f, err = App.open_for_reading(filename) if err then error(err) end for line in f:lines() do - local subject, object = line:match('^%s*(%S+)%s+beat%s+(%S+)%s*$') - table.insert(Global_state.results, {subject, object}) - if subject == nil then + local _ = -- turn rest of loop (a single giant expr) into a statement to satisfy the Lua parser + do_if_match(line, '^%s*(%S+)%s+beat%s+(%S+)%s*$', function(subject, object) + table.insert(Global_state.results, {subject, object}) + if results[subject] == nil then + results[subject] = {} + end + results[subject][object] = 2 + if results[object] == nil then + results[object] = {} + end + results[object][subject] = 0 + end) + or error('incorrect format: '..line) - end - if results[subject] == nil then - results[subject] = {} - end - results[subject][object] = 2 - if results[object] == nil then - results[object] = {} - end - results[object][subject] = 0 end f:close() return results -end \ No newline at end of file +end diff --git a/0029-do_if_match b/0029-do_if_match new file mode 100644 index 0000000..7054753 --- /dev/null +++ b/0029-do_if_match @@ -0,0 +1,6 @@ +do_if_match = function(line, pat, f) + local captures = {line:match(pat)} + if #captures == 0 then return end + f(unpack(captures)) + return true +end \ No newline at end of file