This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-07-23
Channels
- # announcements (10)
- # babashka (7)
- # beginners (31)
- # boot (4)
- # calva (8)
- # chlorine-clover (19)
- # cider (12)
- # clj-kondo (27)
- # cljsrn (7)
- # clojure (68)
- # clojure-colombia (2)
- # clojure-europe (47)
- # clojure-italy (16)
- # clojure-nl (4)
- # clojure-spec (13)
- # clojure-uk (39)
- # clojurescript (103)
- # code-reviews (8)
- # community-development (2)
- # conjure (38)
- # core-async (37)
- # cursive (2)
- # datascript (1)
- # datomic (31)
- # figwheel-main (22)
- # fulcro (18)
- # jobs (2)
- # jobs-discuss (6)
- # juxt (3)
- # keechma (1)
- # lambdaisland (6)
- # malli (30)
- # meander (20)
- # off-topic (29)
- # reagent (1)
- # reitit (16)
- # shadow-cljs (4)
- # tools-deps (70)
- # xtdb (19)
yeah. i'm a big believer in "stop forcing devs to play compiler/computer in their heads"
Given the code below -
(m/search [{:farm ""}
{:farm "a"}
{:farm ""}]
(m/scan {:farm _ :as ?row})
(assoc ?row :farm "b"))
Is there a better way to rewrite it, I am basically trying to substitute the :farm value for a given collection of hash-maps.Here are two other ways you could write this:
(m/rewrite [{:farm ""}
{:farm "a"}
{:farm ""}]
[{:farm _ & !rest} ...]
[{:farm "b" & !rest} ...])
(m/rewrites [{:farm ""}
{:farm "a"}
{:farm ""}]
(m/scan {:farm _ & ?rest})
{:farm "b" & ?rest})
@U5K8NTHEZ thanks, let me try to wrap my head around rewrite/rewrites, when do I use it, it looks very similar to match/search ?
So both rewrite and rewrites use m/subst
on the right hand side. If you think about pattern matching as deconstructing values, substitution is the constructing of values. Basically it extends many of meanders features for creating things as well as matching on them.
To make it concrete, instead of rewrites I could have just said:
(m/search [{:farm ""}
{:farm "a"}
{:farm ""}]
(m/scan {:farm _ & ?rest})
(m/subst
{:farm "b" & ?rest}))
(m/search [[{:a 1} {:c 3}] [{:b 2} {:d 5}]]
(m/scan [{:as ?row}])
(assoc ?row :z 1))
the above snippet does not work. I have a collection of collections of maps. In each map I want to add a k/v, how do I write it ?(m/rewrites [[{:a 1} {:c 3}] [{:b 2} {:d 5}]]
(m/scan (m/scan {& ?rest}))
{:z 2 & ?rest})
The above adds the k/v, but doesnt maintain the coll structure, it concats all the collections together.(m/rewrite [[{:a 1} {:c 3}] [{:b 2} {:d 5}]]
[(m/cata !xs) ...]
[!xs ...]
{:as ?it}
{:z 2 & ?it})
;; =>
[[{:a 1, :z 2} {:c 3, :z 2}] [{:b 2, :z 2} {:d 5, :z 2}]]
@U06MDAPTP how do I make the above work if my input is [(...)(...)]
ie a vector of seqs instead of a vector of vectors.
@murtaza52 Switch to (m/seqable …)
Actually, you may need to look at the implementation of that and create your own m/sequential
because m/seqable
looks for any seqable?
thing and calls seq
on it.
One rule to rewrite vectors (recursively rewriting their members), and one to rewrite maps.
Meander has some built-in resistance to complex movements, in particular update “in place”. If you find yourself trying to figure out how to do a transform in one fell swoop, break it up in to more focused rules and use m/cata
to drive the traversal, etc.
@U06MDAPTP thanks ! had not used cata before, so that was a wow moment !
Hey folks, I’ve pushed up the some work I’ve been doing to make it possible to dynamically create pattern search interpreters. https://github.com/noprompt/meander/pull/128
It’s not quite ready to merge. In the spirit of including the community to the extent I can, I’m welcoming feedback both here and on the PR.