Fork me on GitHub
#beginners
<
2021-04-30
>
Eddie01:04:02

I’m failing to find a documented example of a return type-hint + a docstring on a function. The compiler tells me this is wrong:

(defn foo ^Long
  "I do nothing" 
  [^Long x] x)

;; Syntax error: Metadata can only be applied to IMetas
It compiles if the type hint is moved after the docstring, but I’m not sure if that is annotating the correct thing. Any advice for dong this right?

alexmiller01:04:51

That’s correct - it goes before the arg vector

🙏 2
Eddie01:04:24

Gotcha. Thanks @alexmiller!

Michael Lan02:04:35

where’s the log for this slack?

seancorfield03:04:34

The former is purely a readonly searchable index/log. The latter is also a chat community, that has a one-way mirror of nearly all channels here and is searchable with no limits (because it is on Zulip’s free open source plan).

Mattias08:04:27

Whoa. Nice. For some reason I earlier just assumed information was gone, unless I captured it somehow. Didn’t occur to me someone was saving things. Things I learn. 🙂

Edward Ciafardini03:04:55

Going through the Reagent documentation and an example is provided for updating state using "on-change" for an input.

(ns example
  (:require [reagent.core :as r]))
(defn atom-input [value]
  [:input {:type "text"
           :value @value
           :on-change #(reset! value (-> % .-target .-value))}])

(defn shared-state []
  (let [val (r/atom "foo")]
    (fn []
      [:div
       [:p "The value is now: " @val]
       [:p "Change it here: " [atom-input val]]])))
This mostly makes sense to me, but I am struggling picking apart this specific line:
:on-change #(reset! value (-> % .-target .-value))}])
Searching the web for "->" and ".-" has not yielded great results as you might imagine

seancorfield03:04:33

.-foo is a JavaScript field selection in ClojureScript. -> is the “thread first” macro.

👍 2
seancorfield03:04:17

(-> % .-target .-value) will macroexpand to (.-value (.-target %)) if that helps: get the target field from the anonymous function’s argument, and then get the value field from that.

seancorfield03:04:44

(you may also find the #reagent channel helpful, if you’re not already a member)

valerauko09:04:57

I made a namespace that should generate a java class with (:gen-class This worked all right until I made an edit to the namespace in question. Since then (even if I revert the changes to the code that worked earlier) lein just refuses to aot it anymore, giving me java.lang.ClassNotFoundException What can cause this?

valerauko09:04:29

I've tried the "usual" candidates like clearing target and even wiping my .m2

valerauko09:04:10

Even if I explicitly try to run lein compile app.class-namespace

valerauko09:04:00

Hmm it finally re-compiled it once I removed every reference to the class from other places in the source first

Juλian (he/him)10:04:16

is there a way to test side-effects of functions, for example the output of println? or should I make the function return the string instead of printing and call println with on the result of the function?

delaguardo10:04:28

(with-out-str (fn-with-side-effects)) ;; => "printed string"

delaguardo10:04:46

but I would try to avoid side effects as much as I can. so return a string to be printed sound right for me

Juλian (he/him)10:04:14

it's a text based game, so printing is an essential part of it 😅

Juλian (he/him)10:04:27

but thanks for the answer

delaguardo10:04:21

I don’t meant to say “avoid it completely” ) I should say “keep it as far from core functionality as possible”.

Juλian (he/him)11:04:41

just had a thought during my lunch run.. maybe I want to reuse the code for a different interface, for example a webpage. that would be easier then, too. thanks for the hint

dvingo15:04:14

it is often recommended in designing clojure programs to have a pure core and then a "runner" that performs the side effects. There are some really great talks about this: https://www.youtube.com/watch?v=ZQkIWWTygio https://www.youtube.com/watch?v=JvGXyNXky0Q see also, re-frame's dispatch

Krishan V13:04:49

Hey everyone, I am looking to dissoc some keys from a list of maps. How do I go about passing the current iteration of a map function to a dissoc?

(def final-map (map (dissoc <I need the current iteration here>[:uselessKey1 :uselessKey2]) (vals items)]))

manutter5113:04:41

You want an anonymous function there: (def final-map (map #(dissoc % :uselessKey1 :uselessKey2) (vals items)]))

🙌 2
Krishan V14:04:32

What is the significance of # in this context? I see this a lot in Clojure but can't find a doc for this. What is the terminology?

manutter5114:04:58

That’s one of the two ways of writing an anonymous function. It’s basically just a shorthand way to throw together a quick anonymous function for use with things like map and filter and reduce and so on.

manutter5114:04:13

The % means the first arg to the function, and if you have more args than that, the second arg is %2 and the third is %3 and so on.

manutter5113:04:05

(Also no square brackets around the keys you want to dissoc from the map)