Fork me on GitHub
#clojure
<
2017-02-04
>
qqq16:02:39

what does the ... mean in:

(d/q '[:find [(pull ?e ?selector) ...]
:in $ ?selector
:where [?e :app/title]]
(context: datascript)

schmee16:02:57

qqq it’s a collection binding

schmee16:02:07

search for “collection binding” here: http://docs.datomic.com/query.html

schmee16:02:23

> A collection binding binds a single variable to multiple values passed in as a collection.

qqq16:02:38

https://clojuredocs.org/clojure.core/binding <-- this is NOT a clojure feature, and is a datomic/datascript feature ?

schmee16:02:33

the is a Datalog feature AFAIK

schmee16:02:44

i.e. not clojure

qqq16:02:12

@schmee: got it; thanks

schmee16:02:23

can you define local variables in a defrecord that is accessible to all methods in the body?

schmee16:02:42

e.g. something like

(defrecord Foo [bar]
  (let [thing (compute bar)]
    p/Protocol1
    (method1 [this] (do-something thing))

    p/Protocol2
    (method2 [this] (do-something-else thing))))

pesterhazy17:02:11

you can add another field thing to the defrecord and add a custom contructor

pesterhazy17:02:33

(defn make-foo [bar] (->Foo bar (compute bar)))

schmee17:02:51

or, I could write a super complicated macro which lets me bind stuff in the record body? right? 😄

pesterhazy17:02:20

or just recompute thing every time you use it 🙂

pesterhazy17:02:45

is this because compute is slow?

schmee18:02:03

pesterhazy not really, it’s more of a DRY violation

schmee21:02:21

is it possible (read: worth it) to write unit tests for JDBC code that don’t use a real database?

schmee21:02:35

to use some form of stub/mock for the connection etc.

arrdem22:02:16

Mocking the database is possible, but probably not worth it. Most of the app tests I’ve seen spin up a local sqlite or use a pool of test mysql instances to do functional testing against a real db

eslachance23:02:55

(cross-post from #beginners ) how would I chain str/replace-first? (-> (str/split (message :content) #" ") first (str/replace-first "what here? %1?" #"!" "")) (in other words how do I chain a function with a multiple arity)

eslachance23:02:16

Tried #(str/replace-first %1 #"!" "") it didn't like that.

metametadata23:02:03

@schmee If you need to test that your db-related code really writes/reads as expected than it's better to use the real database for such tests. On the other hand, it is worth to mock/stub the db calls away when, say, you test "business logic" "units". Because for such tests you shouldn't care if they modify the real db instance or even if the value is really written somewhere. Without the real db such tests will be much faster. To make it work the JDBC invoсations should be encapsulated in the dedicated component which is injected as a dependency to your business logic components. Such db layer will be easier to stub/mock than trying to isolate low-level stuff such as JDBC connections.

eggsyntax23:02:59

@eslachance ^ You almost had it there, but you can’t nest function literals (`#()`). Try using the function literal inside a spelled-out anonymous function instead (ie (fn [s] … #( … )))

eslachance23:02:00

hmmm.... why is (-> (str/split "!ping me" #" ") first (fn [s] (str/replace-first s #"!" ""))) giving me CompilerException java.lang.IllegalArgumentException: Parameter declaration first should be a vector, ?

eslachance23:02:40

(as you can tell I like to do fancy things but I'm still kinda new to clojure in some basic aspects and I feel like a noob)

eslachance23:02:51

ooooh wait maybe it's simpler than that. the threading macro inserts the result of the previous as the 2nd element, exactly what I need.

eslachance23:02:54

Smart, clojure. Smart.

schmee23:02:15

metametadata yeah, I think this is what I’ll go for

metametadata23:02:25

@eslachance user=> (macroexpand-1 '(-> (str/split "!ping me" #" ") first (fn [s] (str/replace-first s #"!" "")))) (fn (first (str/split "!ping me" #" ")) [s] (str/replace-first s #"!" ""))

eslachance23:02:34

(-> (str/split "!ping me" #" ") first (str/replace-first #"!" "")) actually works great 😄

schmee23:02:04

right now my API uses a record that encapsulates the DB connection, so I can write unit test for the code that uses that, and integration tests for the record itself

metametadata23:02:18

@eslachance I guess the exception is about (fn (first 🙂

metametadata23:02:51

but you've already figured that out

eslachance23:02:08

I love clojure. most of the time 😄

metametadata23:02:45

@schmee yes, it's similar to what I usually do

arrdem23:02:56

@eslachance what’s your avatar? I don’t recognize it.

eslachance23:02:24

It's the icon for my library, which I know for a fact is probably breaking rules about the Clojure logo

eslachance23:02:39

but it's so damn nice though.

eslachance23:02:27

Anyone wanna comment on how sexy and clean this looks?

(defn handle-message [session message]
  (let [command (-> (str/split (message :content) #" ") first (str/replace-first (config :prefix) ""))
        args (drop 1 (str/split (message :content)))]
    (cond
      (= command "ping") (dithcord/send-message session "pong!" (message :channel_id))
      (= command "say") (dithcord/send-message session (join " " args) (message :channel_id))
      :else nil))) 

eslachance23:02:46

❤️ Clojure