weavejester / meta-merge

A standalone implementation of Leiningen's meta-merge function
104 stars 5 forks source link

Displace? #1

Closed drusellers closed 8 years ago

drusellers commented 9 years ago

Looking at the readme I'm not clear what ^:displace is doing.

(meta-merge {:a [:b :c]} {:a ^:displace [:d]})
=> {:a [:b :c]}

I tried at the repl

(meta-merge {:a [:b :c :d]} {:a ^:displace [:d]})
=> {:a [:b :c :d]}

But that didn't do anything either. I also looked at the tests for displace but its the same example and looking at the code I can see it has to do with priority but its not clear to my dense block head exactly what is going down.

thank you in advance.

weavejester commented 9 years ago

^:displace is the opposite of ^:replace. With replace, the map to the right takes precedence, while with displace the map to the left takes precedence.

A good use for ^:displace to create defaults. Essentially you're marking a value to be overridden, to have the lowest priority.

drusellers commented 9 years ago

ok, so using that in the repl now I can see. Would you be ok if I added this to the README.md via a PR?

(meta-merge {:a [:b :c] :b [1 2]} {:a ^:displace [:d]})
=> {:a [:b :c] :b [1 2]}
(meta-merge {:b [1 2]} {:a ^:displace [:d]})
=> {:a [:d] :b [1 2]}