Fork me on GitHub
#clojure
<
2018-03-16
>
wei02:03:24

using clojure.java.jdbc, is there a way to execute a sql statement not in a block?

=> (clojure.java.jdbc/execute! -db "DISCARD ALL")
PSQLException ERROR: DISCARD ALL cannot run inside a transaction block  org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse (QueryExecutorImpl.java:2422)

seancorfield02:03:28

@wei Use db-do-commands instead of execute! -- then you can tell it to not use a transaction.

seancorfield02:03:44

(clojure.java.jdbc/db-do-commands -db false "DISCARD ALL")
assuming -db is the symbol that is bound to your db-spec

seancorfield02:03:59

(also, if it helps, there's a #sql channel for in-depth JDBC / SQL questions)

wei02:03:10

@seancorfield perfect-- thanks!

wei02:03:38

ok I'll ask on #sql for future questions

seancorfield02:03:12

In general, the assumption is that DDL should be run with db-do-commands and SQL run with query / execute! (and the various insert and delete functions)

wei02:03:53

I see. for context, this is a workaround to a problem I discovered after upgrading postgres. the most recent version caches prepared statements, so sometimes a migration that changes a table will trigger an error when a cached query is run against that table

seancorfield02:03:43

PostgreSQL is a strange beast.

jduhamel03:03:01

is here a replacement to gloss?

jduhamel03:03:10

or just use gloss even though it’s been archived.

dottedmag08:03:45

clojure.spec is very useful for taking apart structured data. What is the most similar option for strings? I'd like to put a string into a parser and get a labeled tree out of it.

miikka09:03:54

@jduhamel, we do use gloss in production even though it's been archived and it works fine for us (although it could maybe have more batteries included and nicer exceptions and...). I'm not aware of a replacement.

dottedmag09:03:25

@miika Thanks. I was not thrilled by a string-based parser declaration when I looked at it. I'll give it a second try.

dottedmag09:03:08

Oh, it does have PEG, so I can write ordered choices. Sweet.

miikka09:03:27

Search the README for instaparse.combinators - you can declare the parsers with Clojure data as well

dottedmag09:03:00

Yep, read up to it already. This README is long, and I didn't expect it to have two interfaces 😃

blackawa10:03:31

How can I insert record into this table with clojure.java.jdbc and postgresql?

CREATE TABLE accounts (
  id serial primary key
);
I’ve tried these functions.
;; This function call throws PSQLException. It generates invalid query for postgresql.
(clojure.java.jdbc/insert! db :accounts {})
;; This works, but cannot get generated ID as return value
(clojure.java.jdbc/execute! db ["INSERT INTO accounts (id) VALUES (DEFAULTS);"])

mbjarland10:03:35

general question about lib deployments to clojars. Is there a best practice for the naming of artifacts? I've seen quite a number of projects deployed without an artifact name prefix, and when there is a prefix, is it considered narcissistic to use your own handle vs some generic grouping or possibly the artifact name itself (`[somename/somename "1.0.0"]`)?. How about the clj- pattern for the project name part of the artifact id? For reference and as two examples of well known clojure libs with both patterns in them, [clj-http "3.8.0"] has no prefix and uses the clj- pattern, whereas [com.rpl/specter "1.1.0"] has a prefix and no clj- pattern.

sundarj11:03:54

single-segment artifacts are a bad practice that is being weeded out slowly. using your own handle is fine, but the recommended approach is to use https://en.wikipedia.org/wiki/Reverse_domain_name_notation if you own a domain

sundarj11:03:39

note that the single-segment clj-http artifact is equivalent to clj-http/clj-http i think

mbjarland11:03:42

well I own a business which owns a domain and I think this relationship with domains might be fairly common, i.e. devs often live two lives, one in which they work for some entity with a domain, and one where they do open source projects but are possibly not associated with a domain. Ok, well I'll try to refrain from the single-segment pattern then

mbjarland11:03:26

I assume that goes for the pattern where you use the project name twice as well then...repeating the same string twice doesn't exactly improve the macro behavior of the system

sundarj11:03:47

the project name twice is equivalent to the project name once i believe

mbjarland11:03:45

yeah makes sense...ok I guess I'll meditate over the prefix a bit, need to publish a few libs

mbjarland11:03:24

using your own handle feels a bit narcissistic though, that would be forcing people to essentially write my name in their project files...a tad intrusive

sundarj11:03:50

yeah i'm not a fan of it either

mbjarland11:03:14

I guess I'm buying a domain then : )

sundarj11:03:16

there are those that use their github username and the reverse domain convention like so: com.github.username

mbjarland11:03:53

I believe that is defined by clojars as a private namespace

mbjarland11:03:27

ah no that is org.clojars.username not github

mbjarland11:03:04

then again, if you ever move your project from github to some other place, then that prefix starts to look pretty weird

mbjarland11:03:09

it seems there is a .ninjadomain ext...`[i.am.ninja/libname "1.0.01"]`?

mbjarland11:03:02

in fact for this purpose, it would have been good with a domain extension specifically for library publications .lib or something

sundarj11:03:13

there is a .dev 😛

sundarj11:03:28

would be quite nice to have something like https://js.org/ for Clojure i suppose

Sallide10:03:52

Hi all, why doesn't this work (map #(% 2 3) '(+ / * -)) but this does? (map #(% 2 3) (list + / * -)) In particular, I'm confused about why the former yields (3 3 3 3)

Sallide10:03:35

Nevermind, got it, thanks

mbjarland11:03:47

Hmm...care to enlighten me?

mbjarland11:03:14

ah they are interpreted as symbols instead of functions

petterik14:03:54

Quoting the list will return a list with the symbols '+ '/ '* '- (list + / * -) will return a list with the vars that are bound to the functions clojure.core/+ etc. Using a backtick instead of a quote will return a list of namespaced symbols that clojure can resolve to be functions, that is:

;; passing `(+ * / -)
(map #(% 2 3) `(+ * / -)) ;;=> (3 3 3 3)

petterik15:03:35

(= '(+ * / -) (list '+ '* '/ '-))
(not= '(+ * / -) `(+ * / -))
(not= `(+ * / -) (list + * / -))
(= (map (comp deref resolve) `(+ * / -))
   (list + * / -))

genmeblog11:03:00

it worked for me

user> (map #(% 2 3) (list + / * -))
(5 2/3 6 -1)

mbjarland11:03:21

@tsulej yes, it's the '(+ / * -) version that returns (3 3 3 3), seems + - / * are read as symbols and not as functions

Sallide11:03:23

Yeah if you do '() they're symbols not functions

genmeblog11:03:40

ah, former, sorry

Sallide11:03:42

so ('+ 2 3) is trying to look up '+ in 2, failing, and returning default value 3

genmeblog11:03:17

you can also use juxt for that

user> ((juxt + / * -) 2 3)
[5 2/3 6 -1]

Sallide11:03:26

oh neat, thanks

mbjarland11:03:37

(map #((resolve %) 2 3) '(+ / * -))
=> (5 2/3 6 -1)
also gives the expected result

Sallide13:03:05

How would I generate a lazy seq of factorials?

Sallide13:03:32

e.g. 1 2 6 24...

dpsutton13:03:01

Map factorial over range

sundarj13:03:05

=> (take 10 (map #(apply * % (range 1 %)) (iterate inc 1)))
(1 2 6 24 120 720 5040 40320 )

Sallide13:03:53

Is there a way to do it with corecursion? something like (this doesn't work) (def fact (lazy-cat [1] (map * fact (map inc fact))))

rauh13:03:36

Yeah:

(def fac (lazy-cat [1] (map * fac (rest (range)))))

Sallide13:03:48

oo cool, thank you

mfikes14:03:19

🙂 @rauh’s solution is special to me because making that work was my first commit to the ClojureScript compiler. There is a cool Fibonacci sequence example like that in https://dev.clojure.org/jira/browse/CLJS-817

mfikes14:03:56

(def fib-seq (lazy-cat [0 1] (map + (rest fib-seq) fib-seq)))

rauh14:03:39

Nice commit message 🙂

mfikes14:03:58

Hah! I was more diligent. 🙂

JJ14:03:54

Is the exception shown by *e the complete stacktrace?

mfikes15:03:03

@devicesfor you can take the matter into your own hands and avoid whatever any REPL might be doing, by evaluating (.printStackTrace *e)

Drew Verlee15:03:24

I was thinking about starting a “Elements of Clojure” book club. I posted some thoughts here: https://clojureverse.org/t/elements-of-clojure-book-club/1769 Feedback and possible participants welcome.

attentive03:03:51

Doesn’t look like you got much response to this, but I’d be quite interested if you do go ahead …

Russ Olsen15:03:32

@devicesfor And of course some code may have caught the original exception and thrown their its own exception, which has the effect of obscuring the original stack trace. Not sure how common this still is in the Java world, but back when I was doing plain old Java it was an all to common thing to do in Java libraries.

JJ15:03:37

I think I might be hitting that case 👍

mhcat15:03:52

I need some help interpreting part of the description of agent semantics on http://clojure.org - I'd welcome comments here. Here's the quote in question: > The actions of all Agents get interleaved amongst threads in a thread pool. At any point in time, at most one action for each Agent is being executed. Actions dispatched to an agent from another single agent or thread will occur in the order they were sent, potentially interleaved with actions dispatched to the same agent from other sources.

mhcat15:03:30

my question is about the last sentence and what it means for actions dispatched from 2 separate sources to be interleaved

mhcat15:03:52

I've been assuming that a single action runs until it is finished before yielding to another, regardless of the source

mhcat15:03:38

and I'd like it if that second sentence really means that given sources A and B, actions dispatched from both may be executed thus: A1 B1 A2 B2 A3 B3 etc

mhcat15:03:13

what I wouldn't like (which would cause me to rethink some things anyway) would be for actions A1 and B1 to be non-atomically interleaved

mhcat15:03:33

like A1.1 B1.1 A1.2 B1.2 etc

mhcat15:03:26

anyone got the truth to hand? If not, I'll write some kind of experiment later today to figure it out

Alex Miller (Clojure team)16:03:00

actions from send or send-off are enqueued to be applied to an agent and can be interleaved. Each particular send or send-off function runs atomically. There will never be more than one thread applying an action on an agent at one time.

mhcat16:03:08

@alexmiller so does that mean that the A1 action (by my understanding, a function) may be partially executed, then paused, and B1 partially executed, paused, and then A1 completed?

Alex Miller (Clojure team)16:03:20

A1 will complete, then B1

Alex Miller (Clojure team)16:03:27

there is no “partial”

mhcat16:03:27

excellent, thank you for the clarification

Sallide18:03:19

is it true that defn- is considered not a good idea? wondering why if that's the case

seancorfield18:03:26

In particular, @alexmiller said "At one point all of the current metadata niceties didn't exist (used to be #^{:private true} some may recall) and defn- seemed worth doing I presume (pre-dates my involvementT). But then that was all simplified down to just ^:private and it's preferred to compose the pieces rather than copy N things."

krukow20:03:00

what are people using as a library for very basic data visualization / charting?

krukow20:03:15

was looking at Incanter, but it seems inactive

campeterson21:03:35

@krukow Incanter 1.9.2 was released last month. It is true that for a period it saw very little development, but it is usable.

campeterson21:03:34

There are a couple libraries that were recently mentioned in the #data-science channel.

krukow21:03:49

I’ll go with incanter

krukow21:03:00

just a simple hobby project

hlship21:03:26

I hit an interesting edge case with logging and macros, overcame it, and blogged about it: https://medium.com/@hlship/macros-meta-and-logging-575d5047924c

Datomic Platonic23:03:09

the 1.9 command line tools look interesting; we may be able to get rid of boot in some cases, except for the uberjar creation: can you make an uberjar with the clojure tool?

Datomic Platonic23:03:20

@sundarj i've also been pointed to http://github.com/juxt/pack.alpha which looks similar