summaryrefslogtreecommitdiff
path: root/Tools/LuaMacro/tests/test-forall.lua
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-11-17 23:03:07 +0800
committerchai <chaifix@163.com>2021-11-17 23:03:07 +0800
commit27d6efb5f5a076f825fe2da1875e0cabaf02b4e7 (patch)
tree44f301110bc2ea742908ed92a78eba0803cd3b60 /Tools/LuaMacro/tests/test-forall.lua
parentb34310c631989551054d456eb47aaab5ded266a4 (diff)
+ LuaMacro
Diffstat (limited to 'Tools/LuaMacro/tests/test-forall.lua')
-rw-r--r--Tools/LuaMacro/tests/test-forall.lua33
1 files changed, 33 insertions, 0 deletions
diff --git a/Tools/LuaMacro/tests/test-forall.lua b/Tools/LuaMacro/tests/test-forall.lua
new file mode 100644
index 0000000..0139e97
--- /dev/null
+++ b/Tools/LuaMacro/tests/test-forall.lua
@@ -0,0 +1,33 @@
+require_ 'macro.forall'
+
+def_ dump(t) print '---'; forall val in t do print(val) end
+
+forall x in {10,20,30} do print(x) end
+
+t = {'hello','dolly'}
+print '---'
+forall name in t do print(name) end
+print '---'
+forall x in t if x:match 'o$' do print(x) end
+
+-- a wee bit tautological, but valid!
+print '---'
+forall x in L{x^2 | x in {10,20,30}} do print(x) end
+
+t = L{s:upper() | s in {'one','two','three'} if s ~= 'two'}
+
+dump(t)
+
+forall i = 1,5 do print(i) end
+
+t = L{2*i|i=1,10}
+
+dump(t)
+
+-- identity matrix using nested list comprehensions.
+t = L{L{i==j and 1 or 0 | j=1,3} | i=1,3}
+
+-- note the other form of LCs: using 'for' means that you explicitly want
+-- the generic Lua for-statement.
+ls = L{line for line in io.lines 'test-forall.lua'}
+print('length of this file',#ls)