How to associate .hl files to Clojure syntax highlighting in Light Table? - clojure

I'd like to get Hoplon (http://hoplon.io/) code highlighted using Light Table.

All you should need to do is this:
:files [(:lt.objs.files/file-types [{:exts [:hl],
:mime "text/x-clojure",
:name "hl",
:tags [:editor.clj :editor.clojure]}])]
in your user.behavior file.
Source:
https://groups.google.com/forum/#!topic/light-table-discussion/LjlYu6K1sWk
EDIT 5/1/15: The format of Light Table behavior files changed at some point. The following variation on the above worked for me:
[:files :lt.objs.files/file-types [{:exts [:hl],
:mime "text/x-clojure"
:name "hl"
:tags [:editor.clj :editor.clojure]}]]

Related

Create a keyword from a number

tl;dr
How can I derive a keyword from a number in ClojureScript:
(keyword 22)
;;=> :22 but in fact returns nil.
Background
In my ClojureScript/Hoplon application I make HTTP requests via cljs-http. Parts of the response I receive look like this:
{:companies
{:22 {:description ... } ; A company.
:64 {:description ... }
... }
{:offers
[{:description ... } ; An offer.
{:description ... }
... ]
Each offer within the vector behind :offers has a :companyId which represents a key in :companies. As soon as I receive the response, I reset! a cell (similar to an atom) query.
Now, I'd like to iterate over each offer and call a function offer-tpl that creates the corresponding HTML. In order to do so, offer-tpl needs the offer itself as well as the related company:
(for [offer (:offers #query)]
(offer-tpl offer (get-in #query [:companies (keyword (:companyId offer))]))))))
Despite the fact that this surely can be done more elegant (suggestions very appreciated), the get-in doesn't work. (:companyId offer) returns a number (e.g. 22) but (keyword (:companyId offer)) returns nil. Calling (keyword (str (:companyId offer))) does the trick, but aren't there any other ways to do this?
(keyword "22") or (keyword (str 22)) returns :22
The reason you are getting :22 is likely because of the keywordize-keys option of a JSON translation. For example:
cljs-http defaults to keywordize-keys for jsonp:
https://github.com/r0man/cljs-http/blob/1fb899d3f9c5728521786432b5f6c36d1d7a1452/src/cljs_http/core.cljs#L115
But you can (and should) in this case pass in a flag to disable keywordization.
Not all keys in JSON are appropriate for Clojure keywordization. For example spaces in a JSON key are valid, but not in Clojure.
Please be aware that numeric keywords are probably incorrect.
https://clojuredocs.org/clojure.core/keyword#example-542692cec026201cdc326d70
It seems like that caveat has been removed from the current Clojure website, so perhaps that means something but I'm not sure what.
http://clojure.org/reference/reader Currently states that
Keywords - Keywords are like symbols, except: They can and must begin
with a colon, e.g. :fred. They cannot contain '.' or name classes.
Like symbols, they can contain a namespace, :person/name A keyword
that begins with two colons is resolved in the current namespace: In
the user namespace, ::rect is read as :user/rect
and that
Symbols begin with a non-numeric character and can contain
alphanumeric.
This definition of a keyword excludes :22 and :with spaces
The keyword function returns a result for invalid input, but this is not an endorsement, it is simply because checking for incorrect input would be a performance overhead in a core part of Clojure.
In short, not all JSON keys translate to keywords, so you should avoid keywordize-keys unless you know the keyspace and/or doing so provides some conveniences.

Fully qualified names with Cassaforte

Is there a way to do a fully qualified table name when using Cassaforte's CQL DSL?
I've currently got this code:
(defn get-data-by-user-id [client-name client-id user-id]
(cql/use-keyspace (get-session) client-name)
(cql/select (get-session) "some_data"
(where [[= :client_id client-id] [= :user_id user-id]])
(limit 1)))
I'm curious if it's possible to do something similar to Korma, eg:
(defn get-data-by-user-id [client-name client-id user-id]
(cql/select (get-session) "some_data"
(where [[= :client_id client-id] [= :user_id user-id]])
(limit 1)
(keyspace client-name)))
cassaforte uses/aliases hayt internaly for it's DSL (I authored this lib), so yes probably see:
https://github.com/mpenet/hayt/blob/master/test/qbits/hayt/core_test.clj#L26-L27
or just use a keyword if you don't need the escaping/quoting:
:foo.bar etc
clojure keywords used as cql identifiers are never escaped.

clojure - add item to nested set

Say I have a set like this:
#{#{"a"} #{"b"} #{"c"}}
Say I wanted to updated the middle set to make s become:
#{#{"a"} #{"be"} #{"c"}}
How would I achieve this?
(-> #{#{"a"} #{"b"} #{"c"}} (disj #{"b"}) (conj #{"be"}))
=> #{#{"a"} #{"be"} #{"c"}}
(of course there's no ordering in sets, it could well be shown in any order).

Using Clojure do-template to set prepared-statement columns

I have defined the following code to allow me to set column values in a java.sql.PreparedStatement. Is this code reasonable/idiomatic? How could it be improved?
(use '(clojure.template :only [do-template]))
; (import all java types not in java.lang)
(defprotocol SetPreparedStatement
(set-prepared-statement [this prepared-statement index]))
(do-template [type-name set-name]
(extend-type type-name
SetPreparedStatement
(set-prepared-statement [this prepared-statement index]
(set-name prepared-statement index this)))
BigDecimal .setBigDecimal
Boolean .setBoolean
Byte .setByte
Date .setDate
Double .setDouble
Float .setFloat
Integer .setInt
Long .setLong
Object .setObject
Short .setShort
Time .setTime
Timestamp .setTimestamp)
; Sample use
(set-prepared-statement 42 some-prepared-statement 1)
Your example looks as close to idiomatic Clojure as I can tell :)
It could perhaps benefit from abstracting the type mapping out if you have situations where you will be creating more than one template though if your creating just this one then this looks like excellent clojure to me.

What are the usages for ^ and how can I get more information on it?

found this on a blog : (def x ^{:type ::my-class} {})
apparently it adds meta data to a map
user=> (meta x)
{:type :user/my-class}
what else does ^ do ? does it have any other uses ? can it be used as a getter for meta data (not just to set meta data) ?
how can i find out information about some shortcuts in clojure ? like ^, ', `, ~. is it possible to get that from the repl ?
Look at the documentation for the Clojure reader, specifically the section on macro characters:
The Reader
Edit: Metadata has documentation too :-)