How to abstract `reg-sub` in reframe

172 views Asked by At

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 :)

2

There are 2 answers

1
alex314159 On BEST ANSWER

Why not

(reg-sub
 :say-hello
 (fn [ db [ _ person say-hi ]
   (str (get-in db [ say-hi ]) "hello " person)
 )
0
Eugene Pakhomov On

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).