about summary refs log tree commit diff stats
path: root/break.tlv
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-03-27 10:53:41 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-03-27 10:53:41 -0700
commitdd7da2c62ae1f53069e5bf07ea92861218da0c41 (patch)
tree113f9d3ba7093c18a70535b099546a8603cc5209 /break.tlv
parent455870d1147e1f3e2249b9462ccd7ddd945dcd10 (diff)
downloadteliva-dd7da2c62ae1f53069e5bf07ea92861218da0c41.tar.gz
helper: count
Diffstat (limited to 'break.tlv')
-rw-r--r--break.tlv16
1 files changed, 16 insertions, 0 deletions
diff --git a/break.tlv b/break.tlv
index 34cbe78..36041e6 100644
--- a/break.tlv
+++ b/break.tlv
@@ -236,6 +236,7 @@
     >end
 - __teliva_timestamp: original
   count_letters:
+    >-- TODO: handle unicode
     >function count_letters(s)
     >  local result = {}
     >  for i=1,s:len() do
@@ -249,6 +250,21 @@
     >  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