Fork me on GitHub
#hoplon
<
2023-07-25
>
Lidor Cohen14:07:14

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?

dave14:07:25

There are a number of considerations here. I think it all depends on what you're trying to do.

dave14:07:12

To answer the question of literally how to extract a value from your cell, use @ (`deref`)

dave14:07:38

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.

Lidor Cohen15:07:38

of course I do need to keep reactiveness so no deref please

dave15:07:40

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

Lidor Cohen15:07:47

just reactive filter

Lidor Cohen15:07:14

what if a single cell is not an option for me?

Lidor Cohen15:07:29

I have a list of cells

dave15:07:24

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

dave15:07:56

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

alandipert17:07:11

((formula vec) xs) gives you back a formula cell with a vector inside if that's what you're after

alandipert17:07:57

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

alandipert17:07:31

If you want reactive vectors with cell elements you need to get into lower level Cell deftype and helper functions

alandipert17:07:00

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

Lidor Cohen17:07:11

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.

alandipert17:07:20

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

Lidor Cohen17:07:15

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

alandipert17:07:06

Hard mode is the best mode metal

Lidor Cohen17:07:44

Expert plus FTW 😁 clojure-spin

😆 4
dave18:07:06

We've done this kind of thing a few times at Kevel. In retrospect, I think we should have just had a single cell whose value was a map, instead of a map containing cells. But because we did have that latter thing, it was useful to do this kind of thing.

dave18:07:25

This kind of thing meaning, convert it into a single cell whose value is a map.

dave18:07:56

In general in Hoplon, I've found that the less cells you have, the better day you'll have.