Fork me on GitHub
Kenneth Gitere07:06:31

Hi. Is there a function that does (first (filter pred coll)) but as just one function? I want a function that returns the first element in a collection/sequence when the predicate is true


Is the book clojure for the brave and true still the best way to learn clojure for a beginner ?


I suggest there has never been a single way. BraveClojure is still a relevant way to learn Clojure (although chapter 2 Emacs config is a little dated) There are many other books, videos or challenges websites like 4Clojure. I quite like Getting Clojure as a beginners book these days And Iโ€™ve creates about 100 videos of coding with Clojure and a few free books which people seem to find useful


As a help, a REPL app on your phone is great for trying stuff if you're more comfortable reading away from your computer.


@U05254DQM if I follow your study group and type the same code that you do Do I then learn idiotamic clojure ?


Many of the videos that show solving the 4Clojure exercises show several different ways of solving each challenge, using more of the clojure.core functions. The more functions you are familiar with from clojure.core, the easier it is to apply valuable abstractions. The Clojure style guide covers a range of idioms in Clojure.


thanks for the answer

Kenneth Gitere09:06:47

Which is more advisable for a predicate function, to return a boolean or a truthy/falsey value?


The clojure core predicate functions return true / false and I think that's what most people would expect from your predicate functions as well.


In mathematical logic a predicate is a statement that can be evaluated to true or false.

Kenneth Gitere12:06:10

alright. I can work with that


On the other hand, where the 'or' operator's mathematical namesake is all about the booleans, people flagrantly use it for "return the first non-nil value in this list".


If a predicate ends in ?, the expectation is true or false. If it does not end in ?, then return truthy/falsey is reasonable.


I am reading re-frame todomvc code and I am confused why "Associative Destructuring" in the function todo-input doesn't have the same name, i.e. I think it should be {:keys [id placeholder on-save]} I've read and still have no clue.


look how they are used there. the keys used there have (when stop (stop)) and it anticipates them being optional. then below in the body of the function there are defaults that are merged in

๐Ÿ‘ 3

and destructuring will happily bind nil to bindings that aren't there


It's a form-2 component, so it returns a rendering function (the weird lambda is actually some stylised fn) and this function uses the entirety of props (the input map).


Well, :title, :on-save and :on-stop are dissoc'd so they don't get updated on rerenders, but everything else in props does.


Thanks for all your reply. I'll look them up.


I'm new to clojure and I'm having a difficulty creating this structure, how can I do that?


I'm not an expert but I would something like

(->> {"36" ["3" "6"]
      "42" ["4" "2"]
      "51" ["5" "1"]
      "89" ["8" "9"]
      "87" ["0" "7"]}
     (mapcat (fn [[k vs]]
               (->> vs
                    (map #(get % 0))
                    (map #(vector % k)))))
     (into {}))


Exactly that, very thanks!

๐Ÿ‘ 2

(defn test-input [input-value input-valid on-change-evt]
  [:input {:type  "text"
           :value @input-value
           :class (str "demo-input" (when (false? input-valid) " input-error"))
           :on-change #(on-change-evt %)}])

(defn is-valid? [x]
  (> 8 (count x)))

(defn demo-fn []
  (let [value-test (r/atom "demo")]
     [test-input value-test
                 (is-valid? @value-test) ;; true
                 #(reset! value-test (-> % .-target .-value))]]))
Hey, if I replace the validation function (is-valid? @value-test) with true I can edit the content of the input, otherwise I can't. The atom is correctly reset so I'm scared could be related of some concurrent update of the DOM. Any clue?


Try using reagent.core/with-let instead of the current let you're initializing the value-test r/atom. That will only initialize the atom once when the component mounts instead of each time it changes.


I've tried to bring the r/atom outside the function and worked ๐Ÿ˜ณ . So is atom reinizialization the problem. Didn't find nothing about it in the example/docs. Thanks! @U0CLLU3QT


Yes, when you deref an r/atom the component tracks its changes, and if you're initializing it in the same component you deref it in it will reset it on the rerender. The r/with-let is like a type two component. See here -

๐Ÿ™ 2

Whether you define the r/atom outside your component or inside a type two component (like the docs I linked to show) depends on the use case. Doing it in a type two component means that each will have their own local state, which you might want if you're planning on having multiple instances of them.

๐Ÿ™Œ 2