aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-08-30 14:36:25 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2018-08-30 14:36:25 -0700
commitee10b788e30a06d6da2676e1af49da9c094f07ee (patch)
tree77194aa84d884dca0f080fea276f130c271a372b /examples
parent8d3e9358d531a89d940bd349325008f64b74a55f (diff)
Updated to use method call syntax.
Diffstat (limited to 'examples')
-rw-r--r--examples/how_do_i.nom38
1 files changed, 4 insertions, 34 deletions
diff --git a/examples/how_do_i.nom b/examples/how_do_i.nom
index 0cf1d60..1f1b683 100644
--- a/examples/how_do_i.nom
+++ b/examples/how_do_i.nom
@@ -79,7 +79,10 @@ say %my_list.1
# List entries can be modified like this:
%my_list.1 = "ONE!!!"
-say (size of %my_list)
+
+# Or appended to/removed from:
+%my_list::add "extra item"
+%my_list::pop
# How do I define a dictionary/hash map?
%my_dict = {x:99, y:101}
@@ -310,36 +313,3 @@ parse [best of %items where %item_var has score %key_expr] as (..)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
say (best of [2, -3, 4, -8] where %x has score (%x * %x))
-# The first example generates code that looks like:
-
- A_best_of_1_according_to_2 = function(items, key_fn)
- local best, best_key = nil, nil
- for _, item in ipairs(items) do
- local key = key_fn(item)
- if best == nil or key > best_key then
- best, best_key = item, key
- end
- end
- return best
- end
- print(A_best_of_1_according_to_2({2,-3,4,-8}, function(x)
- return x * x
- end))
-
- But the second example produces something more like:
-
- print((function()
- local best, best_key = nil, nil
- for _, x in ipairs({1,-2,3,-4}) do
- local key = x * x
- if best == nil or key > best_key then
- best, best_key = x, key
- end
- end
- return best
- end)())
-
- Notice that the second example has inlined the key function, so that in the inner loop,
- the code only has to do a multiplication, instead of calling a function that does the
- multiplication. In addition to being more efficient, it's also more powerful and
- flexible for the language to allow you to define syntax that manipulates expressions.