CoNarrative / precept

A declarative programming framework
MIT License
657 stars 33 forks source link

Add defsub macro #42

Closed alex-dixon closed 7 years ago

alex-dixon commented 7 years ago

Currently working on this in branch defsub. Will need to copy code out in order to merge because branch also contains work on nested macros #40 that is not essential to its implementation.

I've written the macro but it isn't working for some (probably small) reason. The following is the current subscription syntax written with defrule and the new proposed defsub macro that will expand to it:

(def-tuple-rule subs-footer-controls
  {:group :report}
  [:exists [?e ::sub/request :footer]]
  [[_ :done-count ?done-count]]
  [[_ :active-count ?active-count]]
  [[_ :ui/visibility-filter ?visibility-filter]]
  =>
  (insert!
    [?e ::sub/response
        {:active-count ?active-count
         :done-count ?done-count
         :visibility-filter ?visibility-filter}]))
(defsub :footer
   [[_ :done-count ?done-count]]
   [[_ :active-count ?active-count]]
   [[_ :ui/visibility-filter ?visibility-filter]]
   =>
   {:active-count ?active-count
    :done-count ?done-count
    :visibility-filter ?visibility-filter})

Note this should also remove the need to require the spec.sub namespace in rules (or anywhere else within an application).

Calling macroexpand on the macro I've written for this appears to yield identical output to the def-tuple-rule version. For some reason however the new macro version does not fire.

alex-dixon commented 7 years ago

Working in CLJS as of 66e941e44170e7c06706d52902c5d4c2e7158f27. Supports let in RHS as well as just a map. Have tests for both. Need to add a test to make sure we support the following:

(println "foo")
{:some-prop "some-val"}
alex-dixon commented 7 years ago

Opening support for "anything goes" in the RHS as separate issue. This one was for implementing defsub and we've done that 🎉 .