Like assuming there was also other forms inside the let and you only wanted to eval super-hard-calc ?
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.
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.Thanks @seancorfield. Sounds like an interesting feature to try and add (if I'm not overlooking existing functionality)!
@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.
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.
Generally I just edit x to be whatever value I want and then I eval that
(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-sexpWhen I'm done I just put x back
And sometimes if I don't want to forget what it was, Then I use the discard reader:
(super-hard-calc 10 #_x)
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)
Hmmm, yeah. Maybe I should KISS is better 👍
Ya, for anything more complicated then that, I use the Cider debugger: https://docs.cider.mx/cider/debugging/debugger.html
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
https://clojuredocs.org/clojure.core/some#example-542692c6c026201cdc326940
https://groups.google.com/g/clojure/c/apkNXk08Xes/m/CGCQqLMhlHwJ
Is the book clojure for the brave and true still the best way to learn clojure for a beginner ?
thanks for the answer
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.
alright. I can work with that
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/
Thanks
As a help, a REPL app on your phone is great for trying stuff if you're more comfortable reading away from your computer.
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.
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".
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
and destructuring will happily bind nil to bindings that aren't there
If a predicate ends in ?
, the expectation is true
or false
. If it does not end in ?
, then return truthy/falsey is reasonable.
See https://clojure.org/guides/faq#qmark_bang for more about this convention.
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
@jr0cket if I follow your study group and type the same code that you do Do I then learn idiotamic clojure ?
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!
(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?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! @colinkahn
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
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.
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.