Fork me on GitHub
#specter
<
2017-03-08
>
schmee21:03:31

how do I remove all keys where the value is ”_" from a hash like this?

{:dim1 "slice1", :dim2 "slice2", :dim3 “_”}

schmee21:03:39

I’ve tried various incantations of selected?, subselect and filterer, and I can’t figure out what goes where

schmee21:03:47

or if that is even the right approach

tolitius21:03:55

@schmee:

boot.user=> (setval [MAP-VALS #(= % "_")] NONE data)
{:dim1 "slice1", :dim2 "slice2"}

schmee21:03:08

neat, thanks!

schmee21:03:48

feel like cheating to use setval, I’m so used to immutability that anything involving “set” makes me nervous 😄

schmee21:03:52

is there a way to do it with select though?

schmee21:03:47

to reformulate, “select all keys that are not ”_”"

tolitius21:03:44

boot.user=> (select [ALL #(-> % val (not= "_"))] data)
[[:dim1 "slice1"] [:dim2 "slice2"]]
but this is not the right solution, since it does not return you a map + this would likely be better solved with creating a separate navigator.. which I did not wrap my head around yet

schmee21:03:27

yeah, this is where I ended up as well

schmee21:03:45

it seems there is no way to keep the hash when using select

tolitius21:03:00

I don't think setval is wrong though

tolitius21:03:28

since you are removing / filtering the map entries

schmee21:03:21

indeed, but sometimes you want filter and sometimes you want remove, so I hope there is something equivalent in Specter 🙂

tolitius21:03:05

do (source remove), it is filter underneath

tolitius21:03:31

i.e. (filter (complement pred))

tolitius21:03:12

i.e. setval does not mutate the original data structure

schmee21:03:26

I know, and the setval approach works just fine, but aesthetically having both filter and remove can make the code read nicer

schmee21:03:09

which is why I want to use specter at all in this case, otherwise I’d just do (into {} (remove #(= "_" (val %)) data))

schmee21:03:31

I’m hunting for use-cases in my own code where Specter makes sense, that’s why I’m trying to shoehorn it a bit in this case

tolitius21:03:11

makes sense. I think select will always return you a sequence though

nathanmarz22:03:06

@schmee keep in mind that the setval approach will maintain the type of map and also be significantly faster than the (into {} ...) approach