clojure

2026-05-19T05:45:43.100229Z

Is there a technological criterion to decide when to use for vs. map/filter/etc. ? I can see that it's useful for ergonomics e.g. it can soften the blow if one is used to python, but I am specifically not talking about these dx-adjacent criteria "No" is a valid answer πŸ˜…

p-himik 2026-05-19T06:08:04.447889Z

The intersection is only when iterating over a single flat collection, that's it. for allows iterating over nested collections, early breaks, intermediate lets. map allows iterating over multiple collections at once, plus it can create transducers.

πŸ’― 3
2026-05-19T06:22:59.622889Z

Perfect, exactly the answer I was hoping for, thanks :)

igrishaev 2026-05-20T12:32:52.981629Z

The for output is a bit noisy. One benefit of for and doseq is, they provide cartesian production for nested fields, let me provide an example:

igrishaev 2026-05-20T12:35:16.467349Z

(def data {:foo '[a b c]
           :bar '[d e f]})

(for [[k items]
      data

      item
      items]
  [k item])

([:foo a] [:foo b] [:foo c] [:bar d] [:bar e] [:bar f])

igrishaev 2026-05-20T12:36:08.667289Z

Im my code, I have for expressions with 10-15 levels when traversing nested combos of map/vector/map/map/etc

igrishaev 2026-05-20T12:38:13.694199Z

(def data {:foo '[[1 2 3] [4 5 6] [7 8 9]]
           :bar '[[a b c] [d e f] [h i j]]})

  (for [[k lists]
        data

        items
        lists

        item
        items]

    [k item])

([:foo 1]
 [:foo 2]
 [:foo 3]
 [:foo 4]
 [:foo 5]
 [:foo 6]
 [:foo 7]
 [:foo 8]
 [:foo 9]
 [:bar a]
 [:bar b]
 [:bar c]
 [:bar d]
 [:bar e]
 [:bar f]
 [:bar h]
 [:bar i]
 [:bar j])

2026-05-20T15:43:22.367609Z

for is sql, build some indices, do a query, get results https://gist.github.com/hiredman/7d17d8d2b58c41ce95bf2db305b0f427

shaunlebron 2026-05-20T16:50:43.334059Z

oh wow, for is definitely sql

cjohansen 2026-05-19T07:04:55.283859Z

When mapping over a single collection I some times use map, sometimes for, depending on what's more ergonomic for my use case. I generally don't use any of the clever parts of for because I think it easily results in code that's too dense/hard to understand.

hrtmt brng 2026-05-19T09:35:29.326329Z

For is like a small programming language. I would always use it where it makes sense and where map does not directly work. Especially when you need the :let or :when behaviour, for is good. For me it is a good way to implement things that by their nature are not functional, but you want to implement them functionally. Map is more for problems, that are already functional by their nature.

2026-05-19T09:53:41.605349Z

Haskellers would disagree, but yeah, I think I get the point^^

seancorfield 2026-05-19T12:15:08.979229Z

@p-himik That's probably the best explanation I've ever read for why to use one over the other. Thank you!

πŸ™‚ 1
dpsutton 2026-05-19T12:48:28.077069Z

what’s the early break support in for? I know there are some keyword args for it that are seen very rarely. Is that one of those?

p-himik 2026-05-19T12:53:19.302439Z

Yep, :while. The docstring has an example.

πŸ‘ 1
p-himik 2026-05-19T12:54:38.868449Z

Oh, I forgot to mention Cartesian products that for allows easily.

⭐ 1
lassemaatta 2026-05-19T13:26:07.254499Z

one argument I've heard against for is that its macroexpansion is surprisingly verbose. Hard to say when that might have actual significance, but fwiw I use map for something like 99% use cases and grab for mostly when I need cartesian products, like mentioned above. Feels like most uses of for I see in our legacy code would be, dare I say, simpler if represented as map + filter etc, which also opens up the possibility to go for transducers.

seancorfield 2026-05-19T13:37:46.980489Z

We have quite a few for expressions in our code at work that could be plain map but are probably easier to read as (for [x coll] expr) than (map #(expr-of-%) coll) or (map (fn [x] expr) coll) ISTR having a discussion with @hiredman about this at some point. For example:

(for [news feed]
          (assoc news
                 :profileid       id
                 :profileuuid     uuid
                 :profileusername profile-name))
compared to
(map #(assoc %
                     :profileid       id
                     :profileuuid     uuid
                     :profileusername profile-name)
             feed)

seancorfield 2026-05-19T13:39:02.683419Z

Certainly, if you have (for [x coll] (f x)) then (map f coll) makes more sense.

2026-05-19T14:33:47.090609Z

On my mind map is sequence[s] transformation, while for is more sequence "generation". I find map simpler since the resulting cardinality is more predictable (always the same, or the shortest input one). I use for more like for sequence "generation", where I use some sequences as inputs to drive the generation and the for sub-language to generate something, but the focus is on the output, while for map the focus is on the inputs transformation. Although that is more a semantic thing, so maybe not what you were asking for

πŸ‘πŸ» 1
πŸ‘πŸΌ 1
shaunlebron 2026-05-19T14:48:19.873309Z

Applying for's keyword modifiers to cond is really interesting too https://github.com/Engelberg/better-cond

emccue 2026-05-19T14:14:39.212939Z

If anyone is looking for a project: I could really use an ATProto sdk in Clojure - https://atproto.com/sdks (or Java - just something JVM)

valerauko 2026-05-20T14:16:37.443109Z

should be a great fit for #clojuredart considering how much of atproto tooling is in flutter

benoit 2026-05-20T14:25:42.417819Z

It covers enough ground to develop an app on top of at proto, it may not be production grade just yet but that may not be too far. I don't have much time to focus on it these days but feel free to evaluate and submit PRs for any missing pieces or even take over evolution/maintenance.

2026-05-19T14:17:16.752119Z

i think @me1740 has been working on https://github.com/atproto-clj/atproto-clj, which is discussed in #atproto

2026-05-19T14:17:32.426629Z

i can't speak to how far along it is, i just remember him posting about it last year

emccue 2026-05-19T14:30:54.693549Z

Well @me1740 - lets count this as me annoying you to continue.

πŸ˜„ 1