beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
2021-06-19T00:09:28.349800Z

Like assuming there was also other forms inside the let and you only wanted to eval super-hard-calc ?

seancorfield 2021-06-19T00:13:57.350Z

Instead of copying the form into an RCF and wrapping it in let etc, I tend to just add a def inside the RCF to (temporarily) define a global, then I can eval the original form in-situ.

➕ 1
seancorfield 2021-06-19T00:17:03.350200Z

I have a hotkey for turning parts of let bindings into global def’s too. Consider:

(let [x (super-hard-calc y)
      z (another-hard-calc x y)]
  (do-stuff-to z))
I’ll manually create a def for y in an RCF and then I can eval that first expression — but I can also now highlight x (super-hard-calc y), hit a hotkey, and x is now globally defined with that value. Then I can repeat for z (another-hard-calc x y) and then I can eval (do-stuff-to x) in-situ.

💯 1
❤️ 1
1
walterl 2021-06-19T00:25:03.350500Z

Thanks @seancorfield. Sounds like an interesting feature to try and add (if I'm not overlooking existing functionality)!

walterl 2021-06-19T00:26:29.350700Z

@didibus Yes, but not necessarily. The question is really about evaluating a single form that include symbols will need definition. E.g. I want to eval only (super-hard-calc x), but that will fail, because x has no value unless I eval the surrounding let too.

walterl 2021-06-19T00:30:32.350900Z

vim-fireplace actually has an almost-solution: https://github.com/tpope/vim-fireplace/blob/master/doc/fireplace.txt#L215-L225 > "Bring up a |command-line-window| with innermost form at the cursor prepopulated." Implying that the form can be edited (symbols replaced with values) before actually being eval'ed. But I've found editing in that window quite difficult.

2021-06-19T00:35:07.351200Z

Generally I just edit x to be whatever value I want and then I eval that

2021-06-19T00:36:35.351400Z

(let [x (foo (bar (inc (something))))]
  (super-hard-calc 10))
And I put my cursor after (super-hard-calc 10) and I do eval-last-sexp

2021-06-19T00:36:56.351600Z

When I'm done I just put x back

2021-06-19T00:37:32.351800Z

And sometimes if I don't want to forget what it was, Then I use the discard reader:

(super-hard-calc 10 #_x)

2021-06-19T00:41:14.352100Z

I do the same thing in the let if I want to see some things flow through:

(let [a 10 #_(get-foo)
      b (inc a)
      c (super-hard-calc (+ a b))]
  c)

walterl 2021-06-19T00:44:39.352400Z

Hmmm, yeah. Maybe I should KISS is better 👍

2021-06-19T00:49:33.352600Z

Ya, for anything more complicated then that, I use the Cider debugger: https://docs.cider.mx/cider/debugging/debugger.html

Kenneth Gitere 2021-06-19T07:59:31.355100Z

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

roelof 2021-06-19T09:02:29.357Z

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

roelof 2021-06-20T21:02:20.389200Z

thanks for the answer

Kenneth Gitere 2021-06-19T09:56:47.359700Z

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

emilaasa 2021-06-19T11:58:58.359800Z

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

emilaasa 2021-06-19T12:00:26.360Z

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

Kenneth Gitere 2021-06-19T12:15:10.360200Z

alright. I can work with that

practicalli-john 2021-06-19T12:35:24.370500Z

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/

roelof 2021-06-19T13:00:08.370800Z

Thanks

nathansmutz 2021-06-19T14:41:41.371Z

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

stagmoose 2021-06-19T15:21:42.374100Z

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.

nathansmutz 2021-06-19T15:45:08.374300Z

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

dpsutton 2021-06-19T16:34:52.374500Z

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

👍 1
dpsutton 2021-06-19T16:36:09.374700Z

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

seancorfield 2021-06-19T16:59:52.374900Z

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

seancorfield 2021-06-19T17:00:37.375100Z

See https://clojure.org/guides/faq#qmark_bang for more about this convention.

simongray 2021-06-19T17:52:03.375300Z

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

simongray 2021-06-19T17:54:19.375500Z

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

simongray 2021-06-19T17:56:06.375700Z

https://purelyfunctional.tv/guide/reagent/#form-2

👍 1
roelof 2021-06-19T18:21:54.376Z

thanks

roelof 2021-06-19T18:34:37.376200Z

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

paulocuneo 2021-06-19T21:28:47.379100Z

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

Matheus Silva 2021-06-19T21:34:04.379300Z

Exactly that, very thanks!

👍 1
olaf 2021-06-19T23:05:06.382200Z

(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?

2021-06-19T23:12:09.382300Z

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.

olaf 2021-06-19T23:21:08.382500Z

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! @colinkahn

2021-06-19T23:23:07.382700Z

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

🙏 1
2021-06-19T23:25:23.383Z

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.

🙌 1
practicalli-john 2021-06-19T23:55:02.383800Z

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.

practicalli-john 2021-06-19T23:56:11.384Z

https://guide.clojure.style/#idioms