Fork me on GitHub
#clojure
<
2015-07-29
>
Kira Sotnikov09:07:16

malcolmsparks: I’m recommending json-mode for emacs. It’s really helpful

niquola11:07:18

Hi, is there simple way to convert schema.utils.ValidationError to json?

niquola11:07:05

Or more general, convert deftype to hash-map?

lazy-lambda11:07:01

is there any function for alphabetically sorting strings ?

mitchelkuijpers11:07:26

@nicola: do you use cheshire?

mitchelkuijpers11:07:18

@nicola:

(add-encoder schema.utils.ValidationError
                      (fn [validation-error jsonGenerator]
                        (encode-seq (s-utils/validation-error-explain validation-error) jsonGenerator)))

mitchelkuijpers11:07:55

[schema.utils :as s-utils]
 [cheshire.generate :refer [add-encoder encode-seq encode-str remove-encoder]]

zcaudate12:07:51

just wondering if anyone has tried integrating clojure with vaadin/zkoss

zcaudate12:07:29

if people have though about it but decided against it, I’d like to know why not.

ghadi15:07:05

zcaudate: datomic console is Vaadin

Lambda/Sierra15:07:33

@lazy-lambda: sort works on any Comparable type, including String.

voxdolo16:07:41

I'm baffled and I have to be doing something wrong here… I have a postgres database and a table with a column type of "DATE". I'm using clj-time and clojure.java.jdbc and I'm attempting the following:

(jdbc/insert! conn :table_name
                      (assoc attrs :effective_date (-> "2015-08-29" format/parse coerce/to-sql-date)))

voxdolo16:07:43

The date reflected in the inserted table is "2015-08-28"

voxdolo16:07:57

ordinarily, I would say: "timezone issue", but it's a date and this seems to happen both early in the morning and late at night (I'm in MDT)…. so it seems like a poor fit for that.

niwinz16:07:02

This is because the date is interpreted in local time, and when it is converted to database compatible format, it wil surelly converted to UTC...

niwinz16:07:50

2015-08-29 is interpreted as 2015-08-29T00:00:00 ...

voxdolo16:07:53

clj-time uses java's .getMillis on a java.sql.Date object

voxdolo16:07:04

right, that's what I'm seeing

voxdolo16:07:16

sorry, I have that backwards, it's:

(if-let [dt (to-date-time obj)]
    (java.sql.Date. (.getMillis dt)))

niwinz16:07:18

So if yo pass it to utc, it just substracts the timezone offset and stores it in a database.

voxdolo16:07:04

bother. One would hope that making it of Date type would dodge that 😕

voxdolo16:07:40

I get it though, days are affected by TZ as well…

niwinz16:07:49

If you want store a naive date (in local time and without timezone) you should interpret your input as UTC, and then store it in a database..

voxdolo16:07:21

okey-doke. Thanks niwinz.

hlship16:07:49

Here's a question about NRepl. I'm doing something unfortunately tricky where I need to know when a WeakReference is only weakly referenced.

hlship16:07:05

So (def token (start-thread-etc ...))

hlship16:07:28

The idea being that when token is no longer referenced, I can set the flag that terminates the started thread.

hlship16:07:03

Context: this is about starting a thread "on the side" for a Ring handler, and knowing when to shut down the thread because the entire Ring pipeline has been GCed.

hlship16:07:29

What I'm seeing with VisualVM is that even after I (def token nil), there's still a reference to the token.

hlship16:07:10

So is that the case? And if so, what's the purpose of it, and how can I clear it out?

noisesmith16:07:09

hlship: hmm, could it be in *1 or *2 etc?

noisesmith16:07:13

just a wild guess

hlship16:07:26

interesting idea, let me try ...

hlship16:07:54

Tried typing nil to the command line a few times

hlship16:07:01

that does not seem to affect it

voxdolo16:07:26

niwinz: clj-time's parse parses into UTC, so I don't think that's it

voxdolo16:07:10

(format/parse "2015-08-29")
-> #object[org.joda.time.DateTime 0x76471283 "2015-08-29T00:00:00.000Z"]

niwinz16:07:28

very strange!

voxdolo16:07:07

I'm guessing the coercion of clj-time.format/to-sql-date is killing that somehow?

voxdolo16:07:18

that seems like a thing that I would not be the first to see though

voxdolo16:07:07

(java.sql.Date. (.getMillis (format/parse "2015-08-29")))
-> #inst "2015-08-29T00:00:00.000-00:00"

voxdolo16:07:24

looks to have dropped the timezone

hlship16:07:51

Hm. I may just be barking down the wrong path. I've used a (let) so that token is only held for a short time, and my WeakReference is still not triggered.

hlship16:07:39

Ok, I have a solution for my issue, but it involves a finalizer. Not happy about that, as finalizers are even chancier than WeakReferences.

hlship16:07:21

Fortunately, the functionality in questions is just for development mode.

voxdolo17:07:20

so the workaround (which I don't feel great about) was to change the type of the column to a TIMESTAMP… I'm still dealing in dates in the UI, so the extra precision does me no good, but suddenly the same code works

voxdolo17:07:48

seems like a bug, though I'm not sure where it is… guess I'll file against clj-time and let them kick it back 😐

niwinz17:07:54

can you try use java.sql.timestamp

voxdolo17:07:56

niwinz: rolling back my schema change and executing the following:

(jdbc/insert! conn :table_name 
  (assoc attrs :effective_date (-> "2015-08-29" format/parse-local coerce/to-sql-time))))

voxdolo17:07:28

I still see the date in the database as "2015-08-28"

trptcolin17:07:39

half-baked theory: that fn (compiled to a class as usual in clojure) references its classloader, and has closed over your Token?

niwinz17:07:51

An other option is indicate to the database that you are using UTC, so no any conversion will be done

voxdolo17:07:08

huh, interesting. I'm using hikari… I'm curious how that affects connections in a pool though.. I don't want every query against that connection to use UTC

voxdolo17:07:33

I do care about TZ in other parts of the app

niwinz17:07:45

@voxdolo: interacting with database always in UTC is very good practice....

niwinz17:07:58

the timezone should be part of presentation only

mikepence17:07:08

ideally, yeah

voxdolo17:07:24

fair enough… that's a potentially systemic issue.

voxdolo17:07:40

hi mikepence simple_smile

mikepence17:07:54

hey voxdolo. congrats on the baby!

niwinz17:07:55

Since (I don't remember clearly) django 1.5, the default interaction with database is set to utc

voxdolo17:07:05

(small worlds and thanks)

niwinz17:07:51

(I'm also have worked with django in python, and I know that kind of issues from python)

voxdolo17:07:09

kay… my test suite may save me here, but making that change will definitely make me want to do some thorough QA. I'll take a look into it though.

voxdolo17:07:24

thanks for bouncing around ideas niwinz simple_smile it's much appreciated.

niwinz17:07:34

you are welcome!

jamesmintram17:07:48

I wondered if anyone could give me ways to improve this small piece of code? https://gist.github.com/jamesmintram/232849395526ce9ca581

voxdolo20:07:16

jamesmintram: you might try over in #C053PTJE6

jamesmintram20:07:42

@voxdolo: Thanks - didnt realise that channel existed simple_smile

voxdolo20:07:11

:thumbsup:

hlship20:07:48

Doing web stuff with Clojure? You might want to check out the latest release of Twixt 0.1.17 https://github.com/AvisoNovate/twixt/blob/master/CHANGELOG.md -- Twixt is an asset pipeline for Clojure web applications. It takes care of just-in-time compilation and transformation, letting you write CSS (as Less), HTML (as Jade), and JavaScript (as CoffeeScript) and a bunch of other useful stuff.

Pablo Fernandez20:07:35

When using honeysql, are you supposed to have all this boilerplate: (clojure.java.jdbc/query db-spec (honeysql.core/format (honeysql.core/build :select 1))) just for running “SELECT 1” or am I missing something here?

mpenet21:07:56

it's "just" a dsl to generate string queries, so yes.

mpenet21:07:21

I think it's actually a good approach, baking everything together often ends up with lots of tradeoffs, that said if your "client" library exposed a query wrapping step (ex via a protocol you could extend it to compile on demand (possibly cache etc etc)

mpenet21:07:38

maybe something worth suggesting to clojure.java.jdbc

mpenet21:07:46

https://github.com/mpenet/alia works like this but it's for another db "family", it can take string/java instances (bound, prepared, simple etc), and a clojure dsl and do the compilation internally

Pablo Fernandez21:07:10

Well, since normally what you want to do with SQL is execute it, doing connection management in the SQL library makes sense to me… you know, so that the common tasks of connecting to the database, keeping some global connection that is automatically referred to and so on is abstracted away in a library instead of having to write it from scratch.

mpenet21:07:40

well relying on some global state is kind of hairy, but again, it's easy to write a super thin wrapper to do what you want mixing whatever libs you like for each task

Pablo Fernandez21:07:50

@mpenet: it doesn’t look easy to me as I don’t know what’s the appropriate way of handling connections… you just mention, global state is hairy.

mpenet21:07:26

just pass it as an argument to your functions, could be done via a component for instance

Pablo Fernandez21:07:13

mpenet: I don’t know what you mean by component and I still don’t know when/how to connect.

mpenet21:07:30

(execute (:connection component) "select * from foo;" ...)

mpenet21:07:01

pupeno: check https://github.com/stuartsierra/component and related articles, that should help you getting started for these kind of things

akiel21:07:06

If you need core.async error handling in Clojure(Script) I have 0.1 of https://github.com/alexanderkiel/async-error

Pablo Fernandez21:07:04

I can see that Clojure is a productive language, but it feel there’s a philosophy of providing libraries in vacuum. How is a developer supposed to get from https://github.com/jkk/honeysql to https://github.com/stuartsierra/component and I still haven’t managed to connect to the database. I really want to switch my company to Clojure but it feels everything is unfinished by design. 😕

Pablo Fernandez21:07:35

Mh, Luminus seems to be wiring clj-dbcp.

akiel21:07:50

@pupeno: do you refer to the fact hat the Clojure universe likes to provide small libs rather than frameworks?

Pablo Fernandez21:07:39

@akiel: no, that is fine. Look at this library for doing SQL from Python: http://www.pythoncentral.io/introductory-tutorial-python-sqlalchemy/ it has a function/method to connect to the database and the tutorial shows you how to. The HoneySQL tutorial doesn’t even mention the word connect.

mpenet21:07:55

pupeno: clj-dbcp and clj.jdbc provide more or less the same functionality, they're not going to give you a sql dsl

Pablo Fernandez21:07:27

mpenet: I know, they are the other side of HoneySQL.

Pablo Fernandez21:07:43

akiel: HoneySQL is obviously just one library… my problem is that I’m encountering this all the time and I’m concerned about the impact it’ll have on my developer’s productivity.

akiel21:07:52

@pupeno: honysql is only about a SQL DSL not about connecting to your database - and in my opinion you should use https://github.com/krisajenkins/yesql instead

mpenet21:07:58

pupeno: there are libs like what you mention in clj, one ex is Korma, but it's not what the clj users would advise to use

mpenet21:07:24

well it demands to write a 3 line function to wire the 2 libs, I wouldn't think it makes you unproductive

mpenet21:07:53

and you have the flexibility to pick and choose the best tool for your task

Pablo Fernandez21:07:12

mpenet: I know about Korma. I spent about two weeks working with them to get them to accept a pull request so it would connect using a database URL so that it would work on Heroku.

Pablo Fernandez21:07:48

mpenet: not having to chose is faster than chosing. And the wiring that I’m seeing in Luminus is way more than 3 lines of code.

mpenet21:07:22

I don't know about what luminus is doing, but it requires very little time/effort to achieve, that's a fact

Pablo Fernandez21:07:11

mpenet: it’s not my intention to flamewar, but stuartsierra’s component examples are way more than 3 lines of code.

mpenet21:07:53

it's a nice approach, but writing a function that takes a connection and a query representation, compiles the query and runs the query against a connection is small

mpenet21:07:31

component gives you dependency injection, and a nice abstraction to go a lot further

mpenet21:07:37

but you don't have to use it

Pablo Fernandez21:07:46

mpenet: oh yes, that function is trivial… again, my problem is when/where to connect. I’m not familiar with the thread/process model of JVM and Clojure enough to make decisions about how to handle the connection.

Pablo Fernandez21:07:06

A lot of people recommended yesql… my initial worry is that you either end up with a gazillion update functions or an update functions that update all the fields. Maybe I’m worrying over nothing but ActiveRecord, Rails’ SQL DSL, only updates the fields that changed. Is this a non-issue?

akiel21:07:07

@pupeno: you need at least a connection pool - but I do not really use RDMS anymore from Clojure so it’s not obvious for me what to use

Pablo Fernandez21:07:39

@akiel: I think I’ll just use it since it’s in the new version of Luminus. I’m curious though, what do you use?

akiel21:07:01

I use Datomic

pmooser22:07:53

Storm seems to have some issues if you use clojure > 1.5.1 ... very aggravating. I'd love to be wrong about this but I don't think I am.

lazy-lambda22:07:06

I have a vector [“x” “y” “z”], now I want to find the th of the element in the vector which is equal to say “z”.

lazy-lambda22:07:15

This is very easy with a loop, but is there any function ?

bostonaholic22:07:54

@lazy-lambda: what do you mean "the th of the element"

antishok22:07:40

@lazy-lambda: if you mean index, you can use java's .indexOf e.g. (.indexOf ["x" "y" "z"] "z")

lazy-lambda22:07:59

I was meaning index

bostonaholic22:07:07

no loop required!

lazy-lambda22:07:56

@bostonaholic: @antishok Awesome ! I was looking for something like this.

bostonaholic22:07:57

@lazy-lambda: next time, you can search for methods on the vector by (javadoc clojure.lang.PersistentVector)

bostonaholic22:07:19

and if you're unsure of the class, you can (class [])

hlship23:07:49

1.8.0-alpha-3; I'm curious what the tuples & performance impacts story is.

hlship23:07:31

I'm very interested in the direct linking option, however! Seems like something I'd want disabled during REPL oriented development though ... perhaps something that should only be enabled for AOT? But then, I start having the misgivings of what I use & test not matching what I put out for deployment.

Alex Miller (Clojure team)23:07:09

yes, you would likely not use this at dev time, then enable it when you build a jar

Alex Miller (Clojure team)23:07:37

a key aspect of this is that compiled code retains both the direct (static) and indirect invocation paths - it is the choice of the caller which to use