Fork me on GitHub
#beginners
<
2022-12-25
>
Vincent Olesen11:12:29

I was solving an AOC puzzle, and wrote a part in two ways:

(count
 (for [point points
       neighbour (neighbours point)
       :when (not (contains? points neighbour))]
   neighbour))
(->> points
     (mapcat neighbours)
     (remove points)
     count)
What would be considered most idiomatic?

Miķelis Vindavs13:12:50

I think it’s a matter of personal preference. Personally, I’d say the second one is more idiomatic and I prefer it. I usually use for loops when there is nested iteration (e.g. (for [x … y …] [x y]))

👍 1
solf13:12:25

Second one, threads are idiomatic

Miķelis Vindavs13:12:24

imo, the threads also make it clearer what is happening from a data flow perspective and don’t introduce a lot of name bindings (“variables”) which means less noise when trying to understand what’s going on

solf13:12:27

You can read the threading guide if you haven't done so yet, it has some important points, such as to when use thread first or thread last (https://clojure.org/guides/threading_macros)

Miķelis Vindavs13:12:22

for example, the first one could have a typo like this

(count
 (for [point points
       neighbour (neighbours point)
       :when (not (contains? point neighbour))]
   neighbour))
which is simply not possible in the second one

Miķelis Vindavs13:12:01

for fun, here’s what our new overlords have to say about the matter

pppaul17:12:54

these 2 solutions are doing very different things (macro expand the for). for is doing a nested iteration on 2 collections. for each point iterate over neighbours. then the condition in the iteration is sorta also an iteration over points. this is really confusing.

pppaul17:12:39

the threaded solution is a lot more clear

pppaul17:12:57

you use for when you want to iterate over permutations of your collections data

Miķelis Vindavs18:12:10

They appear to be doing the same thing

Miķelis Vindavs18:12:01

Because neighbour depends on point, it can't be a product

pppaul18:12:33

the macro expansion of the for solution will show you that it's doing a lot more than the threaded solution

pppaul18:12:02

i looked over the for again, and i just noticed that the second collection is derived from the first. i haven't actually seen something like this before. i wasn't aware for could do that.

pppaul18:12:46

i still think that in this case for is being used inappropriately. it's better to use it for permutations.

Miķelis Vindavs18:12:08

What makes it inappropriate?

pppaul18:12:28

it's not being used for permutation iteration

Miķelis Vindavs18:12:46

Why should it only be used for that?

pppaul18:12:15

because clojure has better tools for those other use cases, like map/filter/reduce

Miķelis Vindavs18:12:02

You can also do permutations with nested maps

pppaul18:12:08

maybe there are other good use cases for for could you show me some examples? i've only seen it be good for permutation stuff

pppaul18:12:53

yeah, that's what for expands into, nested loops, which look like nested maps when you aren't doing filtering

Miķelis Vindavs18:12:39

My point is - you can do the same thing in either, it's just a matter of preference and readability

pppaul18:12:52

yeah, and that's what i'm focused on

pppaul18:12:15

for has better readability for permutations than nested anything

pppaul18:12:25

if you have good examples of for for other use cases, i'm interested. i imagine it's ok with graph stuff, but then there are graph libs that probably just are better to use.

pppaul18:12:35

in this specific use case, the for solution uses 3 nested loops. the threaded solution uses 2

pppaul18:12:21

these are not doing the same thing, and also one is more readable than the other. for is not the right tool for this job, and it's less readable as well.

mister_m23:12:05

Perhaps off-topic, but is there any way I can peek into the java process that is running my CIDER repl to see if it has open file handles, and things of that sort?

chucklehead23:12:52

are you looking for a java-specific method? On Linux (and I think macOS) you should be able to do lsof -c java or lsof -p <PID of CIDER process> to list open handles and sockets. On Windows you could use https://learn.microsoft.com/en-us/sysinternals/downloads/handle or https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer to do the same from CLI or GUI

mister_m00:12:48

lsof works for me