'How to abstract `reg-sub` in reframe
in my code ,there is duplication like this:
(reg-sub
:hello-john
(fn [ db [ _ say-hi ]
(str (get-in db [ say-hi ]) "hello John")
)
(reg-sub
:hello-jack
(fn [ db [ _ say-hi ]
(str (get-in db [ say-hi ]) "hello Jack")
)
this pattern is quite tedious and I try to factor out with following code in sub.cljs:
(for [ [x y] [[:hello-john "hello John"]
[:hello-jack "hello Jack"]] ]
(reg-sub
x
(fn [ db [ _ say-hi ]
(str (get-in db [ say-hi ]) y ))
)
But it desn't work as expect. Thanks for reading this appreciate any help :)
Solution 1:[1]
Why not
(reg-sub
:say-hello
(fn [ db [ _ person say-hi ]
(str (get-in db [ say-hi ]) "hello " person)
)
Solution 2:[2]
Your second code block is missing the closing ".
Another thing is that for is lazy - it won't be evaluated by itself. Replace it with doseq.
Finally, a minor thing - don't use (get-in db [say-hi]), instead use (get db say-hi). And if say-hi in your case is always a keyword, you can use (say-hi db).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | alex314159 |
| Solution 2 | Eugene Pakhomov |
