Fork me on GitHub
#clojure
<
2018-10-03
>
roklenarcic11:10:17

I'm writing a macro and if macro is called (my-macro a) I want to return a as a quoted symbol.

roklenarcic11:10:37

So far returning (list quote sym) says that quote isn't defined

roklenarcic11:10:39

what would be code I can return from macro to achieve that?

Tom11:10:07

why are you not calling (quote sym)?

roklenarcic11:10:37

that generates code where this spot is occupied by

roklenarcic11:10:45

which isn't bound

roklenarcic11:10:48

instead of 'a

Tom11:10:40

(defmacro mymacro [] `(quote sym)) ?

Tom11:10:09

sorry, I meant: (defmacro mymacro [a] `(quote ~a))

roklenarcic11:10:39

all i had to do is (list 'quote sym)

awb9912:10:11

Does someone know how I can read a double from a stream in clojure? I am trying to parse blob in a sqlite database. With this snipet I am using getting the field :fielddata from a sqlite table row, and reading an integer. But I need to read multiple doubles from the blob.

markmarkmark13:10:01

@hoertlehner if the field data is just a binary blob you should use io/input-stream rather than io/reader.

markmarkmark13:10:30

you could take that InputStream and wrap it in a DataInputStream(https://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html) which would let you call .readDouble() and other nice things.

awb9913:10:35

Do you know how I can wrap it?

markmarkmark13:10:54

(java.io.DataInputStream. (io/input-stream ...))

simple_smile 4
markmarkmark13:10:11

it takes an InputStream in its constructor

awb9913:10:15

cool! Thanks!!!

awb9913:10:13

ClassCastException java.io.BufferedReader cannot be cast to java.io.InputStream

markmarkmark13:10:51

right, use io/input-stream instead of io/reader

4
markmarkmark13:10:55

unless your field data is a string of text, then you'll have to do something else

awb9913:10:14

That works!

awb9913:10:21

Thank you so much @U1XTUTPMY

awb9914:10:12

do you know if there is big indian small indian conversion available?

awb9914:10:22

I originally was writing the binary buffer from c#

awb9914:10:34

and I now when I read the data back, I am only getting garbage back.

awb9914:10:47

I assume .net and java somehow organize the data differently.

markmarkmark14:10:16

you might want to look into the ByteBuffer class

markmarkmark14:10:06

you could read the number of bytes you need from the stream and put that into a bytebuffer with the correct endianness (it's called order in ByteBuffer) and then getDouble from that

markmarkmark14:10:34

I don't know of any nice Clojure wrapping of a ByteBuffer off the top of my head though

awb9914:10:11

Thanks Mark!

awb9920:10:12

Hey Mark! Just wanted to say thank you again! I completely managed the little endian conversions.

awb9920:10:29

I would not know what to do if you would not have given me the hint.

awb9920:10:48

Some parts of clojure documentation are really hard to find.

awb9920:10:52

Especially interop stuff.

awb9920:10:02

Seems not many people focus on such isues.

cfleming13:10:16

@bfabry Sorry, I missed your followup question - no, it won’t.

mping13:10:23

Hi all, what’s the preferred way to generate html these days? Selmer? Hiccup?

lukas.rychtecky14:10:54

Hiccup :thumbsup:

seancorfield16:10:14

We use Selmer so that we have HTML templates that our front end team can work with. We also use Hiccup in a few places where we know the front end team will never need to tread.

Eric Ervin19:10:14

Templates are so great for getting nicely formatted HTML. I couldn't get that from Hiccup. Maybe I missed a setting.

mping19:10:24

I see thanks

awb9920:10:14

I like two things about Hiccup: 1. that I can use it for server-side rendering in in simple pages and then later on convert it very easily to react templates. 2. That it is all clojure syntax, and this eliminates the need for a different templating language. Also clojure syntax is very short and precise. So the templates end up being simple to understand.

manutter5114:10:31

“Assign it to the intern” 😉

manutter5114:10:51

I mean, I personally prefer Hiccup or something Hiccup-flavored.

manutter5114:10:20

I’m probably biased due to my preference for re-frame, which is Hiccup-flavored.

emccue14:10:45

@roklenarcic You probably want (list quote sym)`

emccue14:10:55

(list `quote sym)

emccue14:10:13

if it is the case that someone can redefine quote

emccue14:10:20

which... I am totally unsure about

Tom16:10:53

@noisesmith you were actually right. what I think was missing from your description is that clojure.zip has the next function that allows you to do a depth-first traversal of the tree in a loop which is exactly what would be required

seancorfield16:10:14

We use Selmer so that we have HTML templates that our front end team can work with. We also use Hiccup in a few places where we know the front end team will never need to tread.

idiomancy17:10:00

is there a core function that does this? if not, what might you call it?

idiomancy17:10:02

(defn ??? [val [f & args]] (apply f val args))

idiomancy17:10:22

(defn thread-first ...) ?

lilactown18:10:16

what's wrong with ->?

noisesmith18:10:45

maybe there's a reason that a function rather than macro is needed

idiomancy18:10:25

yeah, I'm using it in a reduce

idiomancy18:10:58

(comp deref thread-first) is the reduce function I'm using

idiomancy18:10:58

trust me, if (comp deref ->) was a thing, I'd be happy

heefoo18:10:13

Could be redefined as an inline function

heefoo18:10:45

On the rare cases of inline function usefulness

idiomancy18:10:20

well, I'm gonna go ahead and admit that its rare enough that I don't even know what that means

heefoo18:10:36

Inline functions are macros that behave like functions when one uses map, and - i think - reduce

idiomancy18:10:44

huh, that sounds pretty much exactly like my use case 😆

heefoo18:10:44

Indeed. Never seen a use case before 😂

idiomancy18:10:17

tbh though, I asked the original question here because it was about the core functions, but I'm actually using it in cljs. I'd be surprised if that was a thing in cljs

heefoo18:10:38

I think it is

heefoo18:10:20

Hiccup could have some inline functionality

idiomancy18:10:41

huh, might look into playing around with that then. thanks!

heefoo18:10:08

My pleasure

olieidel19:10:47

I’m trying to retrieve data from a H2 db. Specifically, timestamps with time zone information (data type TIMESTAMP WITH TIME ZONE) using clojure.java.jdbc and clj-time. Inserting is easy, just convert the joda time objects to java.sql objects and insert them. Retrieving is tricky: I get org.h2.api.TimestampWithTimeZone objects which are not easily convertible to… anything. I feel like there’s an obvious solution here which I’ve been missing the past 2 hours. Help greatly appreciated!

ghadi19:10:01

@olieidel this sounds crazy but are you sure you are using TIMESTAMP WITH TIME ZONE and not TIMESTAMP WITH TIMEZONE ?

ghadi19:10:12

they seem to be different in the docs

lilactown19:10:17

I looked up TimestampWithTimeZone: https://github.com/h2database/h2database/blob/master/h2/src/main/org/h2/api/TimestampWithTimeZone.java you can get a string at the least. you could probably convert it to a java Date or something

olieidel19:10:23

@ghadi TIMESTAMP WITH TIME ZONE. I can’t find the one you mentioned with TIME ZONE. this one: http://www.h2database.com/html/datatypes.html#timestamp_with_time_zone_type

ghadi19:10:01

the docs indicate you can get: > java.time.OffsetDateTime and java.time.Instant are also supported on Java 8 and later versions.

ghadi19:10:31

joda+clj-time are antiquated and retired by java.time, btw

olieidel20:10:30

yeah good point, thanks!

olieidel20:10:52

yes I read that in the docs, this works on insertion (you can use Instant, for example) but upon retrieval it automatically gives you the h2 TimeStampWithTimeZone object without you being able to “select” what you actually wanted (a java.time.Instant, for example) - that’s probably a limitation of clojure.java.jdbc but I’m not very proficient in it.

olieidel19:10:57

@lilactown thanks! yeah I was hoping to get around that. also, the built-in clj-time parsers all don’t parse it (great)

lilactown19:10:10

yeah it's pretty specific

lilactown19:10:46

i'd probably write a function to convert the h2 type to clj-time and use that everywhere

ghadi19:10:20

org.h2.util.LocalDateTimeUtils is what you want @olieidel

ghadi20:10:34

(org.h2.util.LocalDateTimeUtils/valueToInstant d) and sibling methods

aarkerio20:10:03

Hi! is there a way to bind a Var name like:

noisesmith20:10:40

it's possible to do (intern *ns* (symbol (str "foo" bar)) 100) but that doesn't add the same metadata that def does (edit: missed an arg at first)

noisesmith20:10:34

and it's usually a sign that your design isn't idiomatic - usually there's something more straightforward than constructing the name for a definition at runtime (eg. using a def with a hash-map rather than treating a namespace as if it were a hashmap)

noisesmith20:10:00

(ins)user=> foo
CompilerException java.lang.RuntimeException: Unable to resolve symbol: foo in this context, compiling:(NO_SOURCE_PATH:0:0) 
(ins)user=> (intern *ns* (symbol (str "f" "oo")) 1)
#'user/foo
(ins)user=> foo
1

olieidel20:10:23

@ghadi great find! I appreciate the help. But check it out, it gets even better:

org.h2.api.TimestampWithTimeZone cannot be cast to
   org.h2.value.Value

olieidel20:10:42

when calling LocalDateTimeUtils/valueToInstant on it

olieidel20:10:24

calling .getDate directly on the ResultSet works but I don’t see a practical way from here:

(jdbc/db-query-with-resultset db-spec "SELECT * FROM food"
                                (fn [rs]
                                  (.next rs)
                                  (.getDate rs "timestamp")))
;; => #inst "2018-10-02T22:00:00.000-00:00"
(the column is named “timestamp”)

Jan K20:10:01

(defn- to-instant [^TimestampWithTimeZone ts-with-timezone]
  (.toInstant (DateTimeUtils/convertTimestampTimeZoneToTimestamp
               (.getYMD ts-with-timezone)
               (.getNanosSinceMidnight ts-with-timezone)
               (.getTimeZoneOffsetMins ts-with-timezone))))

Jan K20:10:20

^ this is what i use (failed formatting)

olieidel20:10:01

oh wow, thanks!

ghadi20:10:54

to get it to automatically map within clojure.java.jdbc you need to extend a protocol:

(extend-protocol jdbc/IResultSetReadColumn
  TimestampWithTimeZone
  (result-set-read-column [ts resultset-metadata idx]
     ... convert it here...))

ghadi20:10:22

(there's a similar protocol on the write side)

olieidel20:10:13

@jkr.sw awesome, it works. I don’t even need the .toInstant as it already returns a java.sql.Timestamp 👍

olieidel20:10:31

@ghadi yup that’s right, thanks! merging that and @jkr.sw’s solution 😄 will put it up in a gist when I’m done. you guys saved me some more hours, thanks a lot!

olieidel20:10:57

can’t believe this problem was so un-google-able

seancorfield20:10:15

As one of the maintainers of clj-time I'll make my usual comment here: if you're on Java 8 (or later), consider using Java Time instead @olieidel

👍 4
✔️ 4
seancorfield20:10:55

Either natively or via clojure.java-time.

olieidel20:10:01

@seancorfield yeah I noticed, thanks! 🙂 I’m living on the moon in regard to most topics.

ghadi20:10:35

I would recommend natively - I don't think clojure.java-time makes things easier (also it has a confusing name)

ghadi21:10:53

don't at me

seancorfield21:10:37

It does have some nice conveniences around auto-detecting conversion paths between date/time types. And I think it reads much nicer than bare interop. _(and I edited this to remove the @ of you as I was typing it! :slightly_smiling_face: )_

noisesmith21:10:50

not nearly as bad as clojure.jdbc (which is some NIH version of clojure.java.jdbc)

seancorfield21:10:32

The worst part about clojure.java-time is that it uses a single segment namespace (`java-time`).

olieidel21:10:09

let’s hope a future version of myself will find that via google

ghadi21:10:40

nice follow-up @olieidel

👍 4
the2bears21:10:57

There are always at least two engineers on a project. You, and you in a month.

😄 12
4
seancorfield21:10:08

@olieidel Nice. Looking at DateTimeUtils, I wonder if it might be cleaner to go through dateAndTimeFromValue() instead? (it would work with a wider range of H2 data types). And you might want to package this up and put it on Clojars as a useful H2/JDBC library!

olieidel22:10:13

Ah. Good ideas! Will note them down and add them to my backlog :)

olieidel22:10:49

Re: DateTimeUtils, would have to check whether the TimestampWithTimeZone can be cast to Value