Fork me on GitHub
#beginners
<
2019-08-19
>
Ashley Smith00:08:18

How do you go about actually including that function though? It's not actually appearing if I do (my-comp)

Ashley Smith00:08:43

I got it, needed to be in a vector and not called as a function

David Pham00:08:43

Tell us if this works :)

Ashley Smith00:08:57

(defn md-editor []
  (r/create-class
    {:component-did-mount
      (fn [comp]
        (println (r/dom-node comp))
        ;(js/SimpleMDE. {"element" (r/dom-node comp)}))
        ;(js/SimpleMDE. (r/dom-node comp)))
     :reagent-render
     (fn []
       [:textarea#foo.textarea])})) ;; this div will get returned by `dom-node` above
so the println prints the component, but neither the calls to SimpleMDE work

👍 4
David Pham05:08:51

You need to convert the argument of SimpleMde with clj->js

Ashley Smith10:08:05

Gosh yeah, you're right. I was so tired last night. Thank you!

Andy Nicholson02:08:39

👋 I'm learning spec atm, and I'm trying to model a pair of dates: ✔️ earliest_departure can't be in the past ✔️ latest_departure can't be in the past latest_departure can't be earlier than earliest_departure How can I model this last one? I need both values to make that check, and it's not clear to me how to do that with spec

valerauko03:08:00

I have specs like this, and they work as intended.

(s/def ::pass
  (s/and string?
         #(re-matches #"\S.+\S" %) ; must start and end with nonspace chars
         #(re-find #"[a-z]" %) ; must contain at least one small letter
         #(re-find #"[A-Z]" %) ; must contain at least one capital letter
         #(re-find #"[0-9]" %) ; must contain at least one number
         #(re-matches #".{8,}" %))) ; must be at least 8 characters long

(s/def ::pass-confirm ::pass)
(s/def ::pass-match #(= (::pass %) (::pass-confirm %)))
from https://github.com/valerauko/kitsune/blob/master/src/kitsune/spec/user.clj

schmee06:08:34

a relationship between two values is a property of the aggregate, not the values themselves. so you could do something like:

(s/def ::date ...)
  (s/def ::earliest-date ::date)
  (s/def ::latest-date ::date)
  (s/def ::foo
    (s/and (s/keys :req [::earliest-date ::latest-date])
           #(< (::earliest-date %) (::latest-date %))))

hamid tsh06:08:05

I have vector of maps and i want to send every map by specific time via http-kit. i did for one map like this and it did work. how can i do the same thing for vector of maps?

andy.fingerhut06:08:50

what do you mean "by specific time"? Do you mean that each map contains within it some start and/or end time by which you want the request to occur?

andy.fingerhut06:08:32

If the time issue does not really matter, and you simply want to do them all as quickly as possible, sequentially, you can use map of a function over the vector, where the function body will look like your code above, performing one of the requests. map will cause the function to be called once on each of your vector elements.

hamid tsh06:08:55

I mean for example by every 100 millisecond get first map of the vector.

alpox10:08:14

Is there a way to tests if a value is an async channel - for the use in spec?

alpox11:08:51

As I didn't find any better, I chose to test for #(satisfies? ReadPort %) (No WritePort as the user will not have to use the writing part of the channel) and I hope that is the best I can do. If there is something better i'm open for suggestions 😄

Jamie Rumbelow11:08:35

okay… macros…. I’d like to transform:

Jamie Rumbelow11:08:01

; I want to transform:

(defquery query-name [query & args]
  (query "blah"))

; Into:

(defn query-name [db & args]
  ((db/query db) "blah"))

Jamie Rumbelow11:08:52

I’d basically like to replace the db object with a prepared query partial function

Jamie Rumbelow11:08:32

(so i don’t have to call (db/query db), or so I don’t have to wrap everything in a let)

alpox12:08:44

@jamie962 I'm not very experienced but I gave it a try:

(defmacro defquery [name argv & body]
  (let [query-sym (first argv)
        fn-args (rest argv)]
   `(defn ~name [db# ~@fn-args]
     (let [~query-sym (db/query db#)]
       ~@body))))
This should work as long as the namespacing for db holds. I'm not exactly sure how the namespace lookup for it works in macros but it should evaluate to a fully qualified symbol when db is available.

Jamie Rumbelow15:08:26

v helpful - thank you

linuss14:08:03

Hey guys, I have a Java-interop question: I'm trying to access an enum within an inner class. I've tried IndicesAliasesRequest$AliasActions$Type/ADD and all other variations I could think of, but nothing works so far. The java code can be seen here: https://github.com/elastic/elasticsearch/blob/c639cfece56d0db265efd104b6fb0746c1ef01f6/server/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java

Alex Miller (Clojure team)14:08:20

that's a private static, you can't access it

Alex Miller (Clojure team)14:08:48

oh sorry it's both a field and an enum value

Alex Miller (Clojure team)14:08:03

what you have seems like what I would use - what error do you get?

linuss14:08:32

Caused by: java.lang.RuntimeException: No such namespace: IndicesAliasesRequest$AliasActions$Type

linuss14:08:52

Oh, it seems like using the full namespace (is that the right word?) might actually work

linuss14:08:03

Not pretty, but no errors:

(def alias-action (-> (IndicesAliasesRequest$AliasActions. org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest$AliasActions$Type/ADD)

Alex Miller (Clojure team)14:08:05

do you have an import for the nested class?

linuss14:08:22

yeah:

[org.elasticsearch.action.admin.indices.alias IndicesAliasesRequest
            IndicesAliasesRequest$AliasActions]

Alex Miller (Clojure team)14:08:31

you need one for the Type too

Alex Miller (Clojure team)14:08:41

the inner class is a separate class

Alex Miller (Clojure team)14:08:04

so include IndicesAliasesRequest$AliasActions$Type there

linuss14:08:28

Ah, that's it! Thanks! 🙂

Alex Miller (Clojure team)14:08:30

you actually don't need the others unless you're actually using them

David Pham18:08:17

@alexmiller In your book, Clojure applied, you recommend to use core.async as the preferred fashion to communicate between components. In a talk, Timothy Baldridge recommended to avoid it whenever possible. What are the factors that should we consider for taking the decision?

Alex Miller (Clojure team)18:08:28

well, my opinions are in Clojure Applied :)

David Pham18:08:37

ok I will follow them then 🙂

Alex Miller (Clojure team)18:08:40

I don't have anything better to say than that :)

David Pham19:08:08

It was just a check if you did not updated your view 🙂

David Pham19:08:31

Sorry for the dumb question

David Pham19:08:06

Thanks a lot for your book by the way, it is a pleasure to follow

4
lispyclouds07:08:31

Hey there @UM1AE6NUD! Welcome here, was nice to meet you in heart of clojure 😄

Clément22:08:27

Ahah agreed! Nice meeting you too @U7ERLH6JX 🙌

Clément20:08:28

Just found out that as maps, keywords can be used as functions

jaihindhreddy20:08:45

symbols act just like keywords in fn position. Vectors can be used as fns, they return value when called with an index. And sets when called with something return the thing if it's present in the set, and nil otherwise.

zane22:08:51

I've been working professionally as a Clojure programmer since 2013 and didn't know that about symbols until this year. 🙂

🙃 8
Clément20:08:35

Is one form preferred over the other to get a map attribute?

noisesmith20:08:08

my rule of thumb is if one is a literal or defined in scope such that you always know the type, use that one as a function

noisesmith20:08:17

if neither are, I'll often use get instead

noisesmith20:08:30

this way you can avoid pointless NPEs for unexpected nulls

noisesmith20:08:17

alternately, if the program isn't valid when foo is nil, use foo as a function to enforce that :D

noisesmith20:08:10

but this is mostly a style concern, and I'd go with the preferences of collaborators

Clément20:08:00

makes sense! thanks for your help 👌

Ashley Smith22:08:37

Hey everyone! I am currently using https://github.com/yogthos/markdown-clj to convert markdown into html, and using reagent like so to render it (I don't like this method but struggled to find much else:)

[:div
                  {:dangerouslySetInnerHTML
                    {:__html (md/md->html (:post-summary p))}}]
I need an idea on how to make sure my data is safe - anything that isn't touched by markdown-clj just gets passed through to reagent, meaning that writing raw html gets passed along and then rendered. I would like to simply write the text of the HTML, rather than render it, as it'd be nice to be able to write HTML tutorials and what now in code blocks and instead of it being rendered allow the text to be passed through. Any ideas? I looked at regex, but this answer suggested that it wouldn't be a good idea: https://stackoverflow.com/questions/11229831/regular-expression-to-remove-html-tags-from-a-string