about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-12-06 22:10:44 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-12-06 22:10:44 -0800
commit0e8a160eeea072c09fadca8c38d9f45bd7dce63f (patch)
tree2e155a5a5a94cb876b054680f328198c40eee4d2
parent978f698bfd16fad492fc927d9faa9e84c63be7ae (diff)
downloadteliva-0e8a160eeea072c09fadca8c38d9f45bd7dce63f.tar.gz
map/reduce/filter helpers
-rw-r--r--advent.tlv44
1 files changed, 44 insertions, 0 deletions
diff --git a/advent.tlv b/advent.tlv
index f3b9fa9..a1652ac 100644
--- a/advent.tlv
+++ b/advent.tlv
@@ -89,6 +89,50 @@ end
   {
     __teliva_timestamp = [==[
 original]==],
+    map = [==[
+-- only for arrays
+function map(l, f)
+  result = {}
+  for _, x in q(l) do
+    result[#result+1] = f(x)
+  end
+  return result
+end
+]==],
+  },
+  {
+    __teliva_timestamp = [==[
+original]==],
+    reduce = [==[
+-- only for arrays
+function reduce(l, f, init)
+  result = init
+  for _, x in q(l) do
+    result = f(result, x)
+  end
+  return result
+end
+]==],
+  },
+  {
+    __teliva_timestamp = [==[
+original]==],
+    filter = [==[
+-- only for arrays
+function filter(l, f)
+  result = {}
+  for _, x in q(l) do
+    if f(x) then
+      result[#result+1] = x
+    end
+  end
+  return result
+end
+]==],
+  },
+  {
+    __teliva_timestamp = [==[
+original]==],
     window = [==[
 window = curses.stdscr()]==],
   },