From a858775a6831e5d880d9c0f88470f12a7e9b91f9 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 9 Oct 2017 20:17:38 -0700 Subject: Fixing up sorting more. --- lib/collections.nom | 10 ++++++++++ lib/utils.nom | 8 +++----- 2 files changed, 13 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/collections.nom b/lib/collections.nom index 43dfb08..4d459b7 100644 --- a/lib/collections.nom +++ b/lib/collections.nom @@ -1,4 +1,5 @@ require "lib/metaprogramming.nom" +require "lib/utils.nom" require "lib/control_flow.nom" require "lib/operators.nom" @@ -116,6 +117,15 @@ compile [%expression for %var in %iterable] to: |end)(game, setmetatable({}, {__index=vars})) parse [%expression for all %iterable] as: %expression for % in %iterable +rule [%items sorted] =: + %copy = (% for all %items) + sort %copy + %copy +rule [%items sorted by %key] =: + %copy = (% for all %items) + sort %copy by %key + %copy + # TODO: maybe make a generator/coroutine? #.. Dict comprehensions can be accomplished okay by doing: diff --git a/lib/utils.nom b/lib/utils.nom index 2e469c5..55df1b7 100644 --- a/lib/utils.nom +++ b/lib/utils.nom @@ -87,11 +87,9 @@ compile [max of %items by %value_expr] to: | return \(%value_expr as lua) |end) compile [sort %items] to: "table.sort(\(%items as lua))" -compile [sort %items by %key] to: ".." - |nomsu.utils.sort(\(%items as lua), function(x) - | local ret, vars = nil, {['']=x}; - | \(%key as lua statements) - | return ret +rule [sort %items by %key] =: lua expr ".." + |nomsu.utils.sort(\(%items), function(x) + | return (\(%key))(nomsu, {['']=x}); |end) # String utilities -- cgit v1.2.3