Fork me on GitHub

Just based on some of the examples in the next.jdbc/plan documentation, I'd probably be inclined toward something like this @clojurians-slack100

user=> (def r-first #(reduce (fn [_ x] (reduced x)) nil %))
user=> (r-first (eduction (comp (map str/upper-case) (filter #(doto % println))) [:foo :bar :baz]))


Very interesting. Thank you!


(I changed some? to #(doto % println) just to verify it was only processing one item of the sequence)


Very interesting use of reduce there 🎉


next.jdbc/plan returns a reducible -- an IReduceInit -- so the natural way to get just the first row of a streaming result set is that (reduce (fn [_ x] (reduced x)) nil the-plan)


"natural" you say? 😜


I think it can be further "reduced" (EDIT: this isn't quite right. see @seancorfield's fix below):

(require '[clojure.string :as str])
(def step
   (filter some?)
   (map str/upper-case)))

(let [rf (step (fn [_ x]
                 (reduced x)))]
  (reduce rf
          [nil nil :foo :bar :baz]))

❤️ 3

I guess that's just a rewrite of @seancorfield’s response?


the eduction threw me off


I hadn't thought of calling the step function on the r-first internals...


yea, my sense was that a transducer makes a step function, and that you just want to keep calling the step function until you get a result.


my intuition tells me it could be simplified even further since you're only grabbing one result max and never have to combine it with previous results. in that sense, reduce might still be overkill


ie. the first argument to the rf function is completely ignored


Yeah, every time I write it -- and I write it more often than I'd like -- I feel like there should be some function like it in core 🙂

💯 6

Ah, but now you've reversed the comp order (but I think you're right: the filter some? should have come first, right?).


yea, (filter some?) is only filtering nils, so it would never get called since str/upper-case would fail before that


OK, I simplified my real-world problem... badly. The function being mapped is more complicated, and it is the output from that map that needs to be filtered, not the inputs.


well, the example should work regardless of the step, so your simplification was useful for making the question easy to follow


@smith.adriane Actually that doesn't work as shown:

user=> (def step
           (filter some?)
           (map str/upper-case)))
user=> (let [rf (step (fn [_ x] (reduced x)))]
          (reduce rf [:foo :bar :baz]))


(you need the nil value for init to make that work)

😬 3

user=> (def step
           (filter some?)
           (map str/upper-case)))
user=> (let [rf (step (fn [_ x] (reduced x)))]
          (reduce rf nil [:foo :bar :baz]))

👍 3
🎉 3

i am having a hard time googling this... in hiccup (re-frame/reagent) - what does: ":>" mean - sometimes i'll see something like: [:> Button {:color "primary"} "button text"] as opposed to: [:Button ..etc] i'm not sure when to use ":>" but it seems like if i don't the styling doesn't get applied


answering my own question (i searched this slack for ":>") - here is a handly link that talks about hiccup interop with react:


so "[:> foo...." translates to "[(reagent/adapt-react-class foo)..."

David Pham06:08:26

I am sorry if this sounds really trivial. Over time, I built a library [more a collection of namespaces] in CLJS. I would like to share it internally at my company. There are several possibilities: using deps.edn local/root, and git. The third possibility is about packaging everything into a JAR and ask the user to maven install it. I wonder if anyone know where I could find the documentation or a tutorial on how to do the JAR process? Also, does what I am saying make sense?


Or a more scalable alternative, create a "company" repository

Jack Arrington17:08:16

This is a very small concern really, but is it stylistically odd in Clojure to insert a line break between "groups" of code inside a function? I feel like I have not seen this in anyone else's code


i do it all the time, especially in let statements

👍 6

Totally acceptable, but also always worth wondering if the function has grown to justify extracting the subgroups into named functions of their own.

💯 3
Jack Arrington18:08:42

@UTF99QP7V yeah, I get that and agree. Just a few cases of unavoidable least for a Clojure newbie like me 🙂

Jack Arrington18:08:51

I will say I've naturally found myself doing it far less in Clojure, I think in part because the language itself is so concise, but also because it provides a richer set of primitives for working on data(structures). In an imperative language I often find myself grouping things into blocks when dealing with complicated loops and/or conditionals just to try and make my intent more clear. Clojure, I'm finding, naturally encourages me to think about what I'm doing instead of how, so the intent is more clear from the start.


I tend to only do it in cond expressions, where the pairs of test/expr are already on separate lines, so I put a blank line after each test/expr pair so you can still see the structure at a glance.

👍 3
Jack Arrington18:08:26

Funnily, a long-ish cond was actually the case that made me ask.


I will occasionally do it in let statements that have distinct groups of (side-effecting) logic followed by a result expr -- so blank lines can make that clearer.


Both things said, they do also say "refactor to a function".


Where I will refactor: if each "chunk" wouldn't require a whole bunch of local data passed in as arguments.


But if making those test/expr and/or chunks into functions produces long argument lists, and each chunk of code is only 2-3 lines, I'll leave them inline (with blank lines).

👍 3

I do think sometimes you learn that a bunch of local data is actually a named Thing in your domain, and predicates and transformations around it give you deeper insight into the underlying problem which can pay off even more later. Obv you can go over the top here but I do try to leave a small portion of my brain looking for these opportunities.


Good advice @UTF99QP7V, yes! Refactoring code can sometimes provide new insights into our data.

💯 3