This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-10-03
Channels
- # announcements (4)
- # beginners (68)
- # boot (3)
- # business (20)
- # cider (39)
- # cljs-dev (7)
- # cljsjs (1)
- # cljsrn (12)
- # clojure (122)
- # clojure-brasil (2)
- # clojure-italy (7)
- # clojure-nl (5)
- # clojure-spec (60)
- # clojure-uk (41)
- # clojurescript (67)
- # cursive (7)
- # datomic (13)
- # emacs (6)
- # figwheel-main (18)
- # fulcro (40)
- # garden (3)
- # graphql (2)
- # hyperfiddle (4)
- # jobs-discuss (10)
- # lein-figwheel (5)
- # leiningen (12)
- # luminus (6)
- # mount (3)
- # off-topic (52)
- # portkey (2)
- # re-frame (1)
- # reagent (6)
- # reitit (24)
- # shadow-cljs (15)
- # sql (3)
- # tools-deps (12)
I'm writing a macro and if macro is called (my-macro a)
I want to return a
as a quoted symbol.
So far returning (list quote sym)
says that quote isn't defined
what would be code I can return from macro to achieve that?
that generates code where this spot is occupied by
p1__3111#
which isn't bound
instead of 'a
solved it
all i had to do is (list 'quote sym)
thanks
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.
@hoertlehner if the field data is just a binary blob you should use io/input-stream rather than io/reader.
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.
it takes an InputStream in its constructor
unless your field data is a string of text, then you'll have to do something else
Thank you so much @U1XTUTPMY
you might want to look into the ByteBuffer class
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
I don't know of any nice Clojure wrapping of a ByteBuffer off the top of my head though
Hey Mark! Just wanted to say thank you again! I completely managed the little endian conversions.
Hiccup :thumbsup:
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.
Templates are so great for getting nicely formatted HTML. I couldn't get that from Hiccup. Maybe I missed a setting.
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.
“Assign it to the intern” 😉
I mean, I personally prefer Hiccup or something Hiccup-flavored.
I’m probably biased due to my preference for re-frame, which is Hiccup-flavored.
@roklenarcic You probably want (list
quote sym)`
@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
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.
maybe there's a reason that a function rather than macro is needed
well, I'm gonna go ahead and admit that its rare enough that I don't even know what that means
Inline functions are macros that behave like functions when one uses map, and - i think - reduce
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
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!
@olieidel this sounds crazy but are you sure you are using TIMESTAMP WITH TIME ZONE and not TIMESTAMP WITH TIMEZONE ?
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
@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
the docs indicate you can get: > java.time.OffsetDateTime and java.time.Instant are also supported on Java 8 and later versions.
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.
@lilactown thanks! yeah I was hoping to get around that. also, the built-in clj-time parsers all don’t parse it (great)
i'd probably write a function to convert the h2 type to clj-time and use that everywhere
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)
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)
(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
@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
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”)(defn- to-instant [^TimestampWithTimeZone ts-with-timezone]
(.toInstant (DateTimeUtils/convertTimestampTimeZoneToTimestamp
(.getYMD ts-with-timezone)
(.getNanosSinceMidnight ts-with-timezone)
(.getTimeZoneOffsetMins ts-with-timezone))))
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...))
@jkr.sw awesome, it works. I don’t even need the .toInstant
as it already returns a java.sql.Timestamp
👍
@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!
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
Either natively or via clojure.java-time
.
@seancorfield yeah I noticed, thanks! 🙂 I’m living on the moon in regard to most topics.
I would recommend natively - I don't think clojure.java-time
makes things easier (also it has a confusing name)
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: )_
not nearly as bad as clojure.jdbc
(which is some NIH version of clojure.java.jdbc
)
The worst part about clojure.java-time
is that it uses a single segment namespace (`java-time`).
wrapped up the H2 stuff from above in a gist: https://gist.github.com/olieidel/a3806430622642c51160e7576ee8e394
There are always at least two engineers on a project. You, and you in a month.
@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!