This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-10-05
Channels
- # 100-days-of-code (13)
- # announcements (3)
- # beginners (120)
- # boot (17)
- # calva (19)
- # cider (27)
- # cljdoc (3)
- # cljs-dev (1)
- # clojure (138)
- # clojure-dev (5)
- # clojure-italy (5)
- # clojure-nl (20)
- # clojure-russia (3)
- # clojure-spec (14)
- # clojure-uk (119)
- # clojurescript (45)
- # core-async (2)
- # datomic (23)
- # editors (28)
- # emacs (35)
- # figwheel (2)
- # fulcro (26)
- # graphql (2)
- # hyperfiddle (11)
- # jobs (4)
- # luminus (5)
- # mount (2)
- # off-topic (52)
- # onyx (39)
- # reagent (86)
- # ring-swagger (2)
- # spacemacs (20)
- # tools-deps (9)
- # yada (4)
I am trying to fetch some HTML at the REPL over the network, but keep getting 504s for a reasonably sized file, which comes up fast in Chrome. I am using enlive-html/html-resource to fetch - has anyone come across this issue? Thanks in advance…
@kingcode html-resource use the default java http client which is blocked by some sites. Try using clj-http and pass the response body to enlive.
anyone know of a clojure library that can take a file and return string contents that allow me to highlight a particular line, to have a similar effect to linter squiggly underlines?
@rymndhng Do you just want to get a sequence of lines of text from a file?
not quite, I wondered if there's something a bit more, i.e given a file + {:line 25 :col-start 10 :col-end 35 :context 5}, it'd be able to extract the 5 lines before/after line, and apply some magic highlighting from column 10->35, i.e underlining or ansi escape code definitely something I could whip up, just wondering if someones aware of this existing out there!
Like the squiggly-clojure Emacs package can visually display such things, you mean?
If you don't mind the display being inside an Emacs window, that might do what you want. If you want it in ansi escape codes on a terminal window, I don't think that particular thing will help you.
yes, kind of like squiggly-clojure there's two parts to the thing I'm trying to do: 1. figuring out the best data structure to represent this (which is where I'm hoping to lean on an existing solution) 2. rendering it -- I'm planning to spit it to a terminal, it's mostly to help show errors in a json/yaml file in a console output
For the example you gave, I can't think of a better data structure than the Clojure map you showed. For rendering, I'm not familiar with the internals of the pretty library, but it might have something you could reuse: https://github.com/AvisoNovate/pretty
Google search for terms: clojure ansi terminal gives a few hits, but nothing I have used, and not necessarily anything that makes your life easier.
Hi clojurians!
How would one call a java bit shifting operator from clojure?
I'm perfectly aware of the existence of functions like bit-shift-left
and such in the core namespace, but they do some implicit type conversion, and I don't want that. Any ideas?
You could define a static method with a primitive signature in a java class and call it via interop (if frequently called you can trust the JIT to inline it, so don't worry about the method calling overhead). You will probably still need to pass boxed values across clojure functions, there's no way to avoid that. Still, I would consider using the clojure standard libraries functions: they are inlined too, so I wouldn't be surprised if you got no type conversion at all in the end - if you call them with primitive arguments, you will directly call the primitive static methods in clojure.lang.Numbers
here’s the function and the bytecode:
(defn bsl ^long [^long a ^long b]
(bit-shift-left a b))
->
public static long invokeStatic(long a, long arg1);
0 lload_0 [a]
1 lload_2 [arg1]
2 l2i
3 lshl
4 lreturn
(defn bsl ^Byte [^Byte a ^Byte b]
(bit-shift-left a b))
=> #'asn1.core/bsl
(type (bsl 127 4))
=> java.lang.Long
@U3L6TFEJF nah@U06GS6P1N that's what I ended up doing
well, you have two errors:
1. the primitive type hint is ^byte
, not ^Byte
2. the REPL can’t handle primitive values so they will be boxed before printing. if you use primitive type hint in a let
or a loop
they will not get boxed
there are so many things that can go wrong when trying to use primitive bytes without boxing in Clojure
^byte
is not supported, though:
(defn bsl ^byte [^byte a ^byte b]
(bit-shift-left a b))
CompilerException java.lang.IllegalArgumentException: Only long and double primitives are supported, compiling
I don't know whether this lib can do what you want, but worth checking: https://github.com/ztellman/primitive-math
hey guys, I am trying to create a zip file with several csv files inside. I am using clojure.data.csv
and the function correctly writes the zipfile but most files inside are empty and the ones that have content are just full of gibberish. Does anyone have any idea what could be happening? 😕
I can't use :keys destructuring inside let inside my macro, can I?
(defmacro foo [m]
`(let [{:keys [foo bar]} ~m]
,,,))
I don't see how even clojure.core/destructure
would help :(This works fine like this. You can't use gensyms (`bar#`) with the :keys
syntax (since that would require the key in the map to match that gensym), so you have to use a raw symbol (`~'bar`) in order to avoid it to be expanded into my-namespace/bar
.
@carocad Might still be an issue with how you are writing to the zip. Or perhaps some lazy sequences not being resolved. But I'm guessing - can you show some code? Or otherwise I would get in a REPL and try out some of the csv functions and see whether you are getting the right output at that level before the zipping part happens.
Or you can use the symbol :keyword
map-destructuring syntax. E.g
(defmacro foo [m]
`(let [{bar# :bar} ~m]
(inc bar#)))
@misha@lloydshark here is the code that I am using … I have already tried realizing all lazy sequences but so far no success 😕.
(defn dump!
^ZipOutputStream
[network outstream]
(with-open [zipstream (ZipOutputStream. outstream)]
(doseq [feed (:feeds reference/gtfs-spec)
:when (some :unique (:fields feed))]
(println "writing" (:filename feed))
(let [filename (:filename feed)
zip-entry (ZipEntry. ^String filename)
header (map :field-name feed)
field-id (first (filter :unique (:fields feed)))
id (reference/get-mapping filename (:field-name field-id))
attributes (eduction (map :field-name)
(map #(reference/get-mapping filename %))
(map :keyword)
(:fields feed))]
(. zipstream (putNextEntry zip-entry))
(csv/write-csv (io/writer zipstream)
(cons header
(for [d (data/datoms network :avet (:keyword id))
:let [entity (data/entity network (:e d))]]
(for [attr attributes]
(get entity attr)))))
(.flush zipstream)
(. zipstream (closeEntry))))))
(dump! @firstlast @(:networks (:router hiposfer.kamal.dev/system)))
(io/output-stream "resources/test/foo.zip"))
Hmmm... there's a bit going on there 🙂
Ha, that's cool.
the gist of it is: create a series of file, and for each file I extract the content from a datascript structure, then write it to the csv file
For me I would probably change the implementation to take a (lazy) sequence to produce the zip.
ie Then you could pass in fixed data - and see it work.
Then separately create the function that generates your list of csv's.
That way you should be able to test the 2 things separately.
And then join them together.
jummm that is a good point. I will try that. I was doing it like this since I am returning the zipstream as a response in a ring server
Sorry that's not really an immediate fix to your problem.
hey @lloydshark I followed your advice and ended up with this:
(with-open [outstream (io/output-stream "resources/test/foo.zip")
zipstream (ZipOutputStream. outstream)]
(doseq [[filename content] [["foo.txt" (repeat 5 (range 10))]
["bar.txt" (repeat 10 (repeat 5 "hello"))]]
:let [zip-entry (ZipEntry. ^String filename)]]
(println "writing" filename)
(. zipstream (putNextEntry zip-entry))
(csv/write-csv (io/writer zipstream) content)
(.flush zipstream)
(.closeEntry zipstream))
(.finish zipstream))
Just curious - do you need the (.flush zipstream) ?
I just can't remember having to do a raw flush when I wrote to a zip.
no idea 😅 I am kind of desperate now so I started trying whatever I found in internet
ok, so I just looked at some previous zipping code of mine.
And it did a setSize on the entry.
Before the putNextEntry
Not saying that's the issue - but its at least a difference.
this does the trick
(with-open [outstream (io/output-stream "resources/test/foo.zip")
zipstream (ZipOutputStream. outstream)]
(doseq [[filename content] [["foo.txt" (repeat 5 (range 10))]
["bar.txt" (repeat 10 (repeat 5 "hello"))]]
:let [zip-entry (ZipEntry. ^String filename)
writer (io/writer zipstream)]]
(println "writing" filename)
(.putNextEntry zipstream zip-entry)
(csv/write-csv writer content)
(.flush writer)))
Ah so you are getting content now.
I see that it’s common for uberjar-generating libs to generate a -standalone
flavor, that contains everything and a, well, non-standalone flavor. Presumably you have to provide some of those dependencies manually, right? But how?
I tried java -cp ~/.m2/repository/org/clojure/clojure/1.9.0/clojure-1.9.0.jar:~/.m2/repository/org/clojure/spec.alpha/0.2.176/spec.alpha-0.2.176.jar -jar _build/october-1.0.0-SNAPSHOT.jar
, but I’m getting
Exception in thread "main" java.lang.NoClassDefFoundError: clojure/lang/Var
at nosco.main.<clinit>(Unknown Source)
@tristefigure true, thanks. but all I wanted is to save me some (:foo ~m)
, and {foo# :foo} ~m
kinda the same amount of code, so I'll just go with (:foo ~m)
Have a look at https://github.com/TristeFigure/lexikon#letmap, you might find it useful.
good to know, thanks. but not gonna pull whole dep to save me 3 (:foo ~m)
in one throw away macro : )
No problem
and {:keys [~'bar]} ~m
would be too cryptical for colleagues
(and me in, like, 3 weeks from now)
wait what does this macro do?
Does anybody have any recommendations for how to open a pdf returned by a get
call in a new window? That caviot is that we need to send a *tolkien in the request header*.
Libraries being used
[cljs-http "0.1.45"]
Without the header, this is done easily with just (.open js/window "uri-to-pdf)
, integrating the header is the tricky part.
It optimizes for the potential case that you're accessing a record that has a declared field named k
when accessed (.-key record)
that's a direct field access, but you can't write generic code like that
Question: Is there any way to determine if a symbol is reserved? E.G. I want to postwalk a form, and convert all symbols which are NOT used by the language (macros, functions, special forms) to keywords.
I am aware of special-symbol?
but that only covers a small subset of what I need.
Yes I know that is vague - basically anything that is a special-form (covered by special-symbol?), a function, or a macro
@markw You can redefine almost everything in Clojure. It has very few special forms https://clojure.org/reference/special_forms
I'm saying there are almost no "reserved" symbols.
basically I want to walk a form and convert any symbol that isn't defined by the language to a keyword
"any symbol that isn't defined by the language" <-- but that is an extremely small set of symbols.
you can use resolve
to check if something has a binding, and find out where it comes from
sounds like the hard part here is deciding the scope of what you consider "built in" for your purposes
There are a lot of namespaces bundled with Clojure. And would you consider the Contrib namespaces to be "used by the language" or not?
(`clojure.core.cache` etc)
I think really what I'm after is to convert anything within an arbitrary form which does not already have a binding outside of the form, to a keyword
You'll still need to consider the special forms I linked to above: if
, def
, let
... those resolve
to nil
.
(but at least that's a short list)
user=> (resolve 'let)
#'clojure.core/let
now good luck using the value it returns - but it does return something
Hah, that'll teach me to type more items in a list than I actually tested in the REPL!
the union of special-symbol?
plus resolve
plus some namespace whitelisting magic should suffice
@seancorfield the reason I checked was the earlier convo revealed let was not considered a special-symbol?
which got me curious...
I'd tested if
and def
and just foolishly assumed let
since it was also listed on the special forms page...
I'm seeing a pattern
(ins)user=> (special-symbol? 'let*)
true
(cmd)user=> (resolve 'let*)
nil
(cmd)user=> (special-symbol? 'let*)
true
(ins)user=> (resolve 'fn*)
nil
(ins)user=> (special-symbol? 'fn*)
true
(ins)user=> (resolve 'fn)
#'clojure.core/fn
(ins)user=> (special-symbol? 'fn)
false
hopefully the combination of special-symbol?
and resolve
as @noisesmith suggests should get it done
@markw this may or may not also be useful
(ins)user=> (defmacro local-binds [] (vec (map (comp keyword name) (keys &env))))
#'user/local-binds
(cmd)user=> (let [x 0] ((fn [v] (local-binds)) 42))
[:x :v]
there's variations of that macro that can also give you the values of the locals but it didn't sound like you need it
I'm currently working on a version of postwalk that keeps track of the local variables within a form. That might help you: https://github.com/TristeFigure/dance/blob/master/src/dance/choreography.clj#L277 doc: https://github.com/TristeFigure/dance/blob/master/src/dance/core.clj#L211
Depending on your purposes, you may end up at tools.analyzer 🙂
aha true. I tried using tools.analyzer but it looked daunting. If I recall correctly, there is a plan to provide a simpler interface to tools.analyzer, maybe I could share my experience ?
I need suggestions of libraries (or others solutions) to implement a soap server in a clojure project. The project is long-term and i saw some libraries that are no longer maintained. So anyone uses one of them? Which one is recommended?
@renata.santos Having gone down that path several times over the years, I recommend just using the Apache Axis Java libraries directly via interop.
(I don't think Clojurians use SOAP enough to have produced any effective stable solutions)
we are using defrecord
and implementing nth
on this record to destructure the arguments. But count and nth won't necessarily agree. Ie, count could return an arbitrarily large number but nth is only established for a subset of the record fields. Will clojure.lang.RT assume things based on count or are these largely independent?
are you implementing Indexed?
if so, then I think it will work
(but in general this is weird and is a case where deftype is probably the ideal choice, although it’s a lot more effort)
yes we are in prototype mode right now. So don't want to do a full deftype before we know which way to go. But worried count and nth might blow up somewhere in the runtime
I can’t think of any place it would be an issue
Can someone give me/point me to a code snippet example for connecting to a Redshift cluster with JDBC? What deps do I need in project.clj and what connection parameters to I need in and db connection map?
there's a jdbc driver, hopefully clojure.java.jdbc is able to use it (maybe @seancorfield knows?) https://docs.aws.amazon.com/redshift/latest/mgmt/configure-jdbc-connection.html
there is an example in the README.md https://github.com/clojure/java.jdbc
;; if the dbtype is not known to clojure.java.jdbc, or you want to override the
;; default choice of JDBC driver class name, you can provide :classname and the
;; name of the class to use:
(def redshift42 {:dbtype "redshift"
:dbname "myredstore"
:classname "com.amazon.redshift.jdbc42.Driver"
...})
you would need to add the redshift jdbc connector to your project.clj as a dep
Thanks noisesmith...sorry should have mentioned that I used these references as well, in various permutations of configs...but the connections just hang, finally ended in "ConnectException Connection timed out sun.nio.ch.Net.connect0 (Net.java:-2)". I was hoping for a rather full code example specific to Redshift that someone actually uses (with private db credentials redacted of course), because all the ones online I've found - including these docs - are only partial code examples for Redshift.
have you verified that your creds work from the host you are trying to use redshift from?
Yeah, all my other sql client tools connect just fine, so I know the creds are good.
In project.clj :dependencies: [org.clojure/java.jdbc "0.7.8"] [com.amazon.redshift/redshift-jdbc42-no-awssdk "1.2.12.1017"] In my core.clj" (ns foo (:require [clojure.java.jdbc :as jdbc])) (def redshift {:dbtype "redshift" :host "..." :port 5432 :dbname "..." :user "..." :password "..." :ssl true :sslfactory "org.postgresql.ssl.NonValidatingFactory" :classname "com.amazon.redshift.jdbc42.Driver" }) (jdbc/query redshift "select 42") ;; => ConnectException Connection timed out sun.nio.ch.Net.connect0 (Net.java:-2)