This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-07-25
Channels
- # announcements (1)
- # babashka (15)
- # biff (15)
- # calva (9)
- # cherry (18)
- # cider (43)
- # cljs-dev (1)
- # cljsrn (10)
- # clojure (14)
- # clojure-europe (47)
- # clojurescript (29)
- # clr (5)
- # conjure (1)
- # core-logic (17)
- # datomic (8)
- # emacs (22)
- # fulcro (3)
- # gratitude (1)
- # hoplon (23)
- # humor (1)
- # hyperfiddle (34)
- # jobs (1)
- # kaocha (1)
- # malli (3)
- # nrepl (4)
- # off-topic (18)
- # pathom (12)
- # pedestal (1)
- # polylith (1)
- # portal (17)
- # practicalli (1)
- # re-frame (19)
- # reitit (8)
- # releases (1)
- # rewrite-clj (4)
- # shadow-cljs (15)
- # sql (23)
- # tools-build (4)
Hello, I find myself in a recurring issue which I assume is basic and solved and I'm just missing something: say I want to filter a vector based on the value of the cells inside that vector:
(def xs [(cell true) (cell false) (cell nil) (cell 1)])
how should I go about that?
simple filter will treat any cell as "truthy" so that strait forward way won't work:
(filter (fn [x] x) xs)
and formula also return cell so it wont matter:
(filter (formula identity) xs)
I guess the main issue here is how I extract the value from a cell within a function?There are a number of considerations here. I think it all depends on what you're trying to do.
To answer the question of literally how to extract a value from your cell, use @
(`deref`)
But know that whenever you do that, you're throwing away your ability to react. If the cells continue to change, your code won't react to the changes.
of course I do need to keep reactiveness so no deref please
If you want to keep it reactive, it's more convenient to have a single cell that you can react to, instead of a list of cells, e.g. (defc xs [true false nil 1])
Then you can make a formula over that, e.g. (cell= (filter xs identity))
just reactive filter
what if a single cell is not an option for me?
I have a list of cells
There is a straightforward way to convert a list of cells, or a map where the values are cells, etc. into a single cell whose value is a list, or a map, etc. I don't the details handy at the moment, but that's the general idea
Basically, you write a function that takes the list of cells (or map containing cells, etc.) and it returns a formula cell that reacts to each cell in the input
((formula vec) xs)
gives you back a formula cell with a vector inside if that's what you're after
The tricky part is the elements of that vector won't be cells, but their values, in subsequent formulas that depend on the vector cell
If you want reactive vectors with cell elements you need to get into lower level Cell deftype and helper functions
Hm, (apply (formula vector) xs) might actually be what you need , the previous one would actually result in cell values... But they wouldn't cause the vector to be reactive
I did a reactive-filter
using reduce.
I suspect transducers might come in handy, we're still looking into that.
But I must say I'm quite surprised that this kind of use-case hasn't come up yet, I know composite data-types + reactivity is always a challenge but I was expecting more people will come across these situations.
It's unusual to be working with cells at the domain level like you appear to be. Nothing wrong with it, just a deeper use of the lib
I was just informed by my partner in crime that we have an alpha of reactive-transducer
we'll test it a bit and report back.
P.S
"deeper use" has become my middle name lately, not sure if I'm doing something extremely wrong or extremely right but it's extreme alright... 😅
Hard mode is the best mode