aboutsummaryrefslogtreecommitdiff
path: root/core/collections.nom
diff options
context:
space:
mode:
Diffstat (limited to 'core/collections.nom')
-rw-r--r--core/collections.nom24
1 files changed, 3 insertions, 21 deletions
diff --git a/core/collections.nom b/core/collections.nom
index 5f00358..99c84ca 100644
--- a/core/collections.nom
+++ b/core/collections.nom
@@ -11,7 +11,6 @@ use "core/operators.nom"
# Indexing
test:
assume ((2 nd to last in [1, 2, 3, 4, 5]) is 4)
-
compile [..]
%index st to last in %list, %index nd to last in %list
%index rd to last in %list, %index th to last in %list
@@ -25,7 +24,6 @@ parse [first in %list] as %list.1
# Membership testing
test:
assume (3 is in [1, 2, 3, 4, 5])
-
action [%item is in %list, %list contains %item, %list has %item]:
for %key = %value in %list:
if (%key is %item): return (yes)
@@ -33,7 +31,6 @@ action [%item is in %list, %list contains %item, %list has %item]:
test:
assume (99 isn't in [1, 2, 3])
-
action [..]
%item isn't in %list, %item is not in %list, %list doesn't contain %item
%list does not contain %item, %list doesn't have %item, %list does not have %item
@@ -44,12 +41,11 @@ action [..]
test:
assume ({x:no} has key "x")
-
parse [%list has key %index, %list has index %index] as (%list.%index != (nil))
+
test:
assume ({x:no} doesn't have key "y")
assume (not ({x:no} doesn't have key "x"))
-
parse [..]
%list doesn't have key %index, %list does not have key %index
%list doesn't have index %index, %list does not have index %index
@@ -66,7 +62,6 @@ test:
assume ((last in %list) is 5)
remove index 1 from %list
assume ((first in %list) is 2)
-
compile [..]
append %item to %list, add %item to %list, to %list add %item, to %list append %item
..to (Lua "table.insert(\(%list as lua expr), \(%item as lua expr))")
@@ -86,7 +81,6 @@ compile [remove index %index from %list] to (..)
# List Comprehension
test:
assume (((% * %) for % in [1, 2, 3]) == [1, 4, 9])
-
parse [%expression for %item in %iterable] as (..)
result of:
%comprehension = []
@@ -108,13 +102,11 @@ parse [..]
test:
assume (((% * %) for % in 1 to 3) == [1, 4, 9])
-
parse [%expression for %var in %start to %stop] as (..)
%expression for %var in %start to %stop via 1
test:
assume (("\%k,\%v" for %k = %v in {x:1}) == ["x,1"])
-
parse [..]
%expression for %key = %value in %iterable
%expression for %key %value in %iterable
@@ -128,7 +120,6 @@ parse [..]
# Dict comprehensions
test:
assume (((% * %) = % for % in [1, 2, 3]) == {1:1, 4:2, 9:3})
-
parse [%key = %value for %item in %iterable, %key %value for %item in %iterable] as
..(..)
result of:
@@ -139,7 +130,6 @@ parse [%key = %value for %item in %iterable, %key %value for %item in %iterable]
test:
assume ((%k = (%v * %v) for %k = %v in {x:1, y:2, z:3}) == {x:1, y:4, z:9})
-
parse [..]
%key = %value for %src_key = %src_value in %iterable
%key %value for %src_key %src_value in %iterable
@@ -164,7 +154,6 @@ parse [..]
test:
assume (((% * %) = % for % in 1 to 3) == {1:1, 4:2, 9:3})
-
parse [..]
%key = %value for %item in %start to %stop
%key %value for %item in %start to %stop
@@ -172,7 +161,6 @@ parse [..]
test:
assume (([[1, 2], [3, 4]] flattened) == [1, 2, 3, 4])
-
action [%lists flattened]:
%flat = []
for %list in %lists:
@@ -181,15 +169,14 @@ action [%lists flattened]:
test:
assume ((entries in {x:1}) == [{key:"x", value:1}])
-
parse [entries in %dict] as ({key:%k, value:%v} for %k = %v in %dict)
+
test:
assume ((keys in {x:1}) == ["x"])
-
parse [keys in %dict, keys of %dict] as (%k for %k = %v in %dict)
+
test:
assume ((values in {x:1}) == [1])
-
parse [values in %dict, values of %dict] as (%v for %k = %v in %dict)
# Metatable stuff
@@ -197,13 +184,11 @@ test:
%t = {}
set %t 's metatable to {__tostring:[%] -> "XXX"}
assume ("\%t" == "XXX")
-
compile [set %dict 's metatable to %metatable] to (..)
Lua "setmetatable(\(%dict as lua expr), \(%metatable as lua expr));"
test:
assume (({} with fallback % -> (% + 1)).10 == 11)
-
compile [%dict with fallback %key -> %value] to (..)
Lua value ".."
setmetatable(\(%dict as lua expr), {__index=function(self, \(%key as lua expr))
@@ -222,7 +207,6 @@ test:
%keys = {1:999, 2:0, 3:50}
sort %x by % = %keys.%
assume (%x == [2, 3, 1])
-
compile [sort %items] to (Lua "table.sort(\(%items as lua expr));")
parse [sort %items by %item = %key_expr, sort %items by %item -> %key_expr] as (..)
do:
@@ -233,7 +217,6 @@ parse [sort %items by %item = %key_expr, sort %items by %item -> %key_expr] as (
test:
assume ((sorted [3, 1, 2]) == [1, 2, 3])
-
action [%items sorted, sorted %items]:
%copy = (% for % in %items)
sort %copy
@@ -247,7 +230,6 @@ parse [%items sorted by %item = %key, %items sorted by %item -> %key] as (..)
test:
assume ((unique [1, 2, 1, 3, 2, 3]) == [1, 2, 3])
-
action [unique %items]:
%unique = []
%seen = {}