Fork me on GitHub
#beginners
<
2018-01-18
>
Jacob00:01:03

Hey guys Im new to clojure and I have a question? I am trying to convert all of my shell and sql scripts to clojure so that I can test and whats going on with my scripts. I can select data just fine. I can insert data into the database just fine manually but I would like to be able to select from my database then manipulate the data from my select statement then insert the data into another table. Here is my select query that I am using (jdbc/query db ["select SUM(amount) from product where dt >= '2017-12-1' and dt <= '2017-12-31'"])

Jacob00:01:07

(jdbc/query db ["insert into new_products (query) values (?);" "query"]))

Jacob00:01:16

(jdbc/query db ["select id, SUM(amount) from product where dt >= '2017-12-1' and dt <= '2017-12-31'"])

seancorfield01:01:44

(answered in #sql )

justinlee01:01:50

i’m trying to get into using the repl to help me with my clojurescript development. I can’t seem to get proto repl for atom to do anything useful, so now I’m just trying to use figwheel. I just tried doing the following set of commands:

justinlee01:01:15

app:cljs.user=> (doc doc)
-------------------------
cljs.repl/doc
([name])
Macro
  Prints documentation for a var or special form given its name

nil
app:cljs.user=> (doc nothere)

nil
app:cljs.user=> (doc inc)
But figwheel hangs forever on (doc inc). Am I doing something dumb?

conan13:01:45

i don't know the answer, but the doc function is provided by cljs.repl as you can see in your output, so maybe try doing (require '[cljs.repl :as repl]) and seeing if you can then run (repl/doc inc) without it crashing? if you can, then it's probably an issue with how that library is being pulled in when you start your REPL.

seancorfield02:01:49

@lee.justin.m You might try asking in #clojurescript -- not sure how many folks here are familiar with that side of the house in terms of tooling...

justinlee03:01:23

@seancorfield thanks yea. i think i’m running out of steam… 🙂

Casey15:01:47

I'm looking for a function, similar to zipmap or map vector but that operates on lists of maps, merging the maps together like so..

Casey15:01:15

(????? [{:foo "1"} {:foo "3"}]
       [{:bar "2"} {:bar "4"}])
; => [{:foo "1", :bar "2"} {:foo "3", :bar "4"}]

reborg15:01:56

map merge ?

Casey15:01:22

oh good idea...

dpsutton15:01:30

map apply merge it looks like?

Casey15:01:48

(map #(apply merge %) (map vector ....))

reborg15:01:02

user=> (map merge [{:foo "1"} {:foo "3"}] [{:bar "2"} {:bar "4"}])
({:foo "1", :bar "2"} {:foo "3", :bar "4"})

dpsutton15:01:30

oh i missed the structure of the list of maps. yeah no need for apply

avfonarev16:01:19

Is there a canonical way to find a minimal element in a list of vectors?

edwaraco16:01:33

@avfonarev I'm not sure if you want something like:

(let [list-vec [[1 2 3] [4 5] [6 7]]]
  (apply min (mapcat identity list-vec)))

avfonarev16:01:03

@edwaraco I'm basically looking for #(first (sort %))

rauh16:01:36

@avfonarev Like this: (map #(reduce min %) [[1 2 3] [4 5] [6 7]])

edwaraco16:01:06

Do you want get the minimum element for each vector?

rauh16:01:19

Will throw an exception on empty vectors.

avfonarev16:01:06

@rauh my question was a bit ambiguous, sorry. i'm looking for a minimal element in a collection

edwaraco16:01:47

Or to avoid use mapcat identity you can use flatten

noisesmith17:01:09

flatten is really bad, you can replace mapcat identity with apply concat but more often you want to suggest one of those as a replacement for flatten

edwaraco18:01:53

I don't know flatten was bad, Thanks for your suggestion! 🙂

noisesmith18:01:23

I guess “bad” is an extreme thing to say - it’s a clumsy tool (since it is designed to disregard structure) and often a sign of poor design

rauh16:01:53

So just (reduce min xs) then?

avfonarev16:01:03

@rauh here comes my problem: vectors are not comparable through min

rauh16:01:32

So you want the vector with the least number of elements?

edwaraco16:01:42

How is your vector?

rauh16:01:50

compare can do that IIRC. It'll first check for vector length

avfonarev16:01:06

Vectors have a standard order: lexicographical (Java is a bit weird, I know, but my vectors are equilong)

avfonarev16:01:02

basically I hoped that the standard library has an equivalent of (first (sort comparator-function xs)), which is linear in time, but it seems it does not

rauh16:01:14

Yeah you can do (apply min-key count [[1 2 3] [4 5] [1]] )

rauh16:01:37

But no, for comparators there is no easy way. You'd have to hand roll it

avfonarev16:01:13

I see, thanks to everyone. Could be useful to have it in the standard library...

rauh16:01:42

Yeah there should be a quickselect that takes a compartor. That'd be useful.

Sabbatical201717:01:12

I am playing around with spec and I like it very much. It would appear helpful to turn on all instrumentation during development, including the :fn and :ret parts of fdefs. However, the spec guide explicitly says that "Note that the :ret and :fn specs are not checked with instrumentation as validating the implementation should occur at testing time." I know that I can just use https://github.com/jeaye/orchestra to turn them all on, but I was just wondering what the justification was for how spec is doing it. Spec seems to be pretty well thought out and I would like to understand why something is happening before I turn it off...

seancorfield18:01:08

instrument is about ensuring your calls are correct; check is about ensuring the behavior of a given function is correct.

seancorfield18:01:07

clojure.spec is designed around a particular "model" of testing that separates out the things that folks normally mix together in testing.

seancorfield18:01:04

And it emphasizes that behavioral testing (of functions) is often better served by property-based (generative) testing than by example-based "unit" testing. It also emphasizes that fixed properties of behavior are important -- something we really don't pay much attention to in example-based testing -- and that in turn helps shift the focus to mostly pure, mostly simple functions, which are combined to build a highly testable system with a strong level of confidence.

lilactown18:01:51

can I tell lein or cljsbuild to exclude a directory from compilation?

noisesmith18:01:46

@lilactown you can put files somewhere outside the source-paths

lilactown18:01:01

I specifically want the files next to each other

noisesmith18:01:10

there’s no such setting

Sabbatical201718:01:39

@seancorfield Thank you! What do you mean by "fixed properties" of behavior?

lilactown18:01:46

my ideal project arrangement seems to be anathema to the way clojure projects are typically set up:

project.clj
src/some/namespace/
  - feature/
     - core.cljs
     - server.cljs
     - tests/
     - client-app/
       - project.clj
       ...

seancorfield19:01:52

@sabbatical2017 as a simple example: (= (reverse (reverse s)) s) -- a property of the reverse function is that if you call it twice on any sequence, you should get back (an equivalent to) the original sequence.

seancorfield19:01:13

Here's another couple of examples:

(defexpect round-trip-ip->long
  (more-of {:keys [result num-tests]}
           true result
           100 num-tests)
  (tc/quick-check
   100
   (prop/for-all [a (gen/choose 0 255)
                  b (gen/choose 0 255)
                  c (gen/choose 0 255)
                  d (gen/choose 0 255)]
                 (= (ip->long (str a "." b "." c "." d))
                    (+ d (* 256 (+ c (* 256 (+ b (* 256 a))))))))))

;; tests to ensure ip->long is pretty bullet proof

(defexpect random-ip->long-test
  (more-of {:keys [result num-tests]}
           true result
           100 num-tests)
  (tc/quick-check
   100
   (prop/for-all [s gen/string]
                 (zero? (ip->long (str/replace s #"[0-9]" "."))))))
Does that help @sabbatical2017?

Sabbatical201720:01:12

Yes @seancorfield, this is super helpful. Thank you very much!

urbis21:01:30

Hello, could smbd tell is there any archive of #announcements channel? For the last two weeks?

seancorfield22:01:27

@janis.urbis Most channels are logged but, whilst the logging portion is still working, the display portion on http://clojureverse.org has been broken since November. Sorry.