Fork me on GitHub
#beginners
<
2021-06-19
>
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

roelof09:06:29

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

practicalli-johnny12:06:24

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. https://clojure.org/community/resources I quite like Getting Clojure as a beginners book these days https://pragprog.com/titles/roclojure/getting-clojure/ And Iโ€™ve creates about 100 videos of coding with Clojure and a few free books which people seem to find useful https://practical.li/

nathansmutz14:06:41

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

roelof18:06:37

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

practicalli-johnny23:06:02

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.

roelof21:06:20

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?

emilaasa11:06:58

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

emilaasa12:06:26

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

nathansmutz15:06:08

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".

seancorfield16:06:52

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

stagmoose15:06:42

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 https://clojure.org/guides/destructuring#_keyword_arguments and still have no clue.

dpsutton16:06:52

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
dpsutton16:06:09

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

simongray17:06:03

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).

simongray17:06:19

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

stagmoose01:06:51

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

zZMathSP21:06:51

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

paulocuneo21:06:47

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 {}))

zZMathSP21:06:04

Exactly that, very thanks!

๐Ÿ‘ 2
olaf23:06:06

(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")]
    [:div
     [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?

colinkahn23:06:09

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.

olaf23:06:08

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

colinkahn23:06:07

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 - https://github.com/reagent-project/reagent/blob/master/doc/CreatingReagentComponents.md#form-2--a-function-returning-a-function https://github.com/reagent-project/reagent/blob/master/doc/CreatingReagentComponents.md#appendix-b---with-let-macro

๐Ÿ™ 2
colinkahn23:06:23

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