This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-06-14
Channels
- # beginners (183)
- # boot (6)
- # cider (106)
- # cljs-dev (17)
- # cljsjs (2)
- # cljsrn (2)
- # clojure (56)
- # clojure-italy (14)
- # clojure-nl (39)
- # clojure-spec (49)
- # clojure-uk (138)
- # clojurescript (197)
- # core-logic (37)
- # cursive (22)
- # datascript (5)
- # datomic (29)
- # devcards (18)
- # emacs (1)
- # events (8)
- # figwheel (1)
- # fulcro (59)
- # lein-figwheel (1)
- # leiningen (1)
- # off-topic (54)
- # onyx (3)
- # pedestal (1)
- # portkey (4)
- # re-frame (18)
- # reagent (5)
- # reitit (43)
- # ring (6)
- # ring-swagger (26)
- # shadow-cljs (42)
- # spacemacs (8)
- # specter (12)
- # sql (3)
- # tools-deps (21)
- # vim (18)
What is the best method to wrap a reagent/render to make sure the element is loaded and not get the "Target container is not a DOM element" exception?
@dylanrdrake what are you trying to do? i’m not sure i follow
trying to render a component right when the page loads. I'm getting a "target container is not a DOM element" exception. I think the render is being evaled before the page is done loading @lee.justin.m
so you typically do something like
(defn mount-root []
(reagent/render [#'react-dnd-component]
(.getElementById js/document "app")))
and then you stick a script tag below the <div id="app"></div>
aaahhhhh got it. thanks @lee.justin.m
when receiving an edn string in the body of the response from the server, what is the recommended way to read that edn into a reagent/atom? cljs.reader/read-string?
Is there way to watch inner key of the atom? I mean I know that I can watch primitives, but what if I want to watch the :name key in { :name ‘Karol’ } map.
I want to simulate the store.
can you (declare)
a name that you will later define via (defn-)
in the same namespace (will it still result in a non-public definition)?
No need
Actually, that's a lie. I did Scheme back in college. But that was about 16 years ago and I didn't remember any of it when I started doing Clojure. Also I am no convinced such outdated knowledge would have helped anyway 😉
@kwcharllie379 I don't think there's such a function in core.
(defn add-key-watch [reference watch-key map-key on-map-key-change]
(add-watch reference watch-key
(fn [_ _ old-map new-map]
(let [old-value (old-map map-key)
new-value (new-map map-key)]
(when (not= old-value new-value)
(on-map-key-change old-value new-value))))))
;; Usage
(def a (atom {:foo 5
:bar 10}))
(add-key-watch a :my-watch-id :foo (fn [old-value new-value]
(prn "old: " old-value
"new: " new-value)))
;; Repl example
(swap! a update :foo inc)
"old: " 5 "new: " 6
=> {:wat 10, :foo 6}
(swap! a identity)
=> {:wat 10, :foo 6} ;; No print since the value for :foo didn't change
@U0J30HBRS Thank you so much 🙂
I'm having trouble with post endpoint in compojure:
(defapi mgmt-routes
{:swagger {:ui "/swagger-mgmt-ui"
:spec "/swagger-mgmt.json"
:data {:info {:version "1.0.0"
:title "Sample API"
:description "Sample Services"}}}
;;
(context "/api/mgmt" []
:middleware [wrap-session-auth]
:auth-rules admin?
(POST "/create_user" req
:summary "Creates a user with the given username and password."
(ok
(println (slurp (:body req)))))))
When sent a request from swagger ui like this:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/octet-stream' -d '{ \
"username": "test_2", \
"password": "secret" \
}' ''
There's no body received.
Been stuck here for about two hours now. Can you point me in the right direction?(POST "/create_user" []
:summary "Creates a user with the given username and password."
:body [body {:username s/Str, :passwod s/Str]
(ok body))
the request :body
is parsed automatically and the parsed body is found under :body-params
.
so, this would work also:
(POST "/create_user" req
:summary "Creates a user with the given username and password."
(ok
(println (:body-params req))
hope this helps, ping @U83GGL8DA
@U055NJ5CC: Thanks, however, I came to this after trying the body schema. For example,
(POST "/create_user" req
:summary "Creates a user with the given username and password."
:body [body {:username schema/Str :password schema/Str}]
(ok
(println body)))))
Will result in an error:
2018-06-14 17:51:03,516 [XNIO-2 task-23] INFO compojure.api.exception - Request validation failed: schema.utils.ValidationError@5539130
clojure.lang.ExceptionInfo: Request validation failed: schema.utils.ValidationError@5539130
at clojure.core$ex_info.invokeStatic(core.clj:4739) ~[clojure-1.9.0.jar:na]
at clojure.core$ex_info.invoke(core.clj:4739) ~[clojure-1.9.0.jar:na]
at compojure.api.coerce$coerce_BANG_.invokeStatic(coerce.clj:61) ~[na:na]
at compojure.api.coerce$coerce_BANG_.invoke(coerce.clj:53) ~[na:na]
at water.routes.services$fn__61132$fn__61133.invoke(services.clj:270) ~[na:na]
at compojure.core$wrap_response$fn__30542.invoke(core.clj:158) ~[na:na]
at compojure.core$wrap_route_middleware$fn__30526.invoke(core.clj:128) ~[na:na]
at compojure.core$wrap_route_info$fn__30531.invoke(core.clj:137) ~[na:na]
at compojure.core$wrap_route_matches$fn__30535.invoke(core.clj:146) ~[na:na]
at compojure.api.routes.Route.invoke(routes.clj:74) [na:na]
at compojure.core$routing$fn__30550.invoke(core.clj:185) [na:na]
at clojure.core$some.invokeStatic(core.clj:2693) [clojure-1.9.0.jar:na]
at clojure.core$some.invoke(core.clj:2684) [clojure-1.9.0.jar:na]
at compojure.core$routing.invokeStatic(core.clj:185) [na:na]
at compojure.core$routing.doInvoke(core.clj:182) [na:na]
My handler:
(mount/defstate app
:start
(do
(start-router!)
(middleware/wrap-base
(routes
(-> #'home-routes
(wrap-routes middleware/wrap-csrf)
(wrap-routes middleware/wrap-formats)
(wrap-authentication auth-backend))
(-> #'service-routes
(wrap-authentication auth-backend))
(-> #'mgmt-routes
(wrap-authentication auth-backend)
wrap-params)
#'websock-routes
#'auth-routes
(route/not-found
(:body
(error-page {:status 404
:title "page not found"})))))))
clojure.lang.ExceptionInfo: Request validation failed: schema.utils.ValidationError@5539130
is changed in some later version to include the human readable error instead of object pointer
The response body is quite long, here's the beginning:
<!DOCTYPE html>
<html><head><meta charset="utf-8"></meta><title></title><link rel="stylesheet" href="/prone/1645105518/prone.css"></link></head><body><div id="ui-root"></div><input type="hidden" id="script-replacement-string" value="DQ59MP"></input><script type="text/json" id="prone-data">{:title nil, :location "/api/mgmt/create_user", :error {:message nil, :type "java.lang.NullPointerException", :class-name "NullPointerException", :frames [{:file-name "exception.clj", :line-number 58, :class-path-url "compojure/api/exception.clj", :loaded-from "compojure-api-1.1.12", :method-name "request-parsing-handler", :package "compojure.api.exception", :lang :clj, :id 0, :source {:code "(ns compojure.api.exception\n (:require [ring.util.http-response :as response]\n [clojure.walk :as walk]\n [compojure.api.impl.logging :as logging]\n [schema.utils :as su])\n (:import [schema.utils ValidationError NamedError]\n [com.fasterxml.jackson.core JsonParseException]\n [org.yaml.snakeyaml.parser ParserException]))\n\n;;\n;; Default exception handlers\n;;\n\n(defn safe-handler\n \"Writes :error to log with the exception message & stacktrace.\n\n Error response only contains class of the Exception so that it won't accidentally\n expose secret details.\"\n [^Exception e data req]\n (logging/log! :error e (.getMessage e))\n (response/internal-server-error {:type \"unknown-exception\"\n...
something strange in the overall configuration, as by default, c-api gives you readable schema errors by default.
if you can push you code somewhee, I can try to figure out what is happening there. Have you used the swagger-ui to test the endpoints?
@U055NJ5CC : Thanks for the help. I’m away from desk now, but when I get time, I will try to create a minimal example with the problem. (I can’t post my code as-is because it’s work-related). Perhaps in trying to create a minimal example, I will figure out the problem myself. Thanks for the guidance again.
Probably not important, but you are returning nil
(the result of println
) as a the response body.
Hi everyone, I'm very new to Clojure and ClojureScript. I was wondering if there was a good tutorial for learning javascript interop with clojurescript. Especially using external libraries with interop would be appreciated. Thanks in advance. :)
@UAENCMDB3 not sure if this is what you're looking for but it may help: https://medium.freecodecamp.org/here-is-a-quick-overview-of-the-similarities-and-differences-between-clojurescript-and-javascript-c5bd51c5c007
Hi, thanks for the link. It wasn't really what I was looking for. I have a javascript code using a javascript library but I haven't been able to make it work in clojurescript. There's an interop for javascript which means that I can call javascript library functions in a clojurescript code and mix them up. I haven't been able to find anything very useful on the subject. I was wondering if anyone here might be able to help. :)
In other words I'd like to use the javascript library in clojurescript. Just like I'm able to call Java in Clojure. :)
@UAENCMDB3 there probably should be a tutorial, but I don’t know of one. the good news is that once you actually have a javascript library loaded (which is the hard part), the actual interop is pretty straightforward. if you read up on .
, .-
, #js
, clj->js
, and js->clj
, you pretty much know what’s going on.
if you are having trouble, feel free to ask questions here and i’m sure we can get you going
I'm building an SPA with Clojurescript (Reagent) for the frontend, Clojure on the backend and edn over the wire. My server is responding to ajax calls with edn strings in the body of the response. What is the recommended way to read that edn into a reagent/atom? cljs.reader/read-string?
@dylanrdrake I don’t know (I just use json), but from hanging out here, I think most people use transit
as a wire protocol when they want to be more clojurey rather than edn. I could be wrong though.
yeah I'm trying to use all Clojure throughout the application just for the sake of doing it and learning. I eventually even want to try out Datomic for the db and leverage it's unique take on time. Thanks, I'll look into transit.
Sorry If I am asking a trivial question, we are using immutant
which apparently under the hood is using undertow
as a web server. I could manage to enbale http/2
which seems like
(mount/defstate ^{:on-reload :noop}
http-server
:start
(http/start
(-> env
(assoc :handler (handler/app))
(update :http2 true)
(update :io-threads #(or % (* 2 (.availableProcessors (Runtime/getRuntime)))))
(update :port #(or (-> env :options :port) %))))
:stop
(http/stop http-server))
however, I am struggling to enable compression for my content such as Gzip or deflate. Does anybody have an experience with it and know how I can do that ?
BTW, I have searched and it seems I cannot find a good documents regarding this in Clojure. Any advice is highly appreciated.After a few months of trying to better utilize deftype to define new data structures, there is one thing I am consistently getting tripped up on. Let's say I know which Clojure core functions I would like to use to manipulate the data structure purely based on a semantic understanding of the existing Clojure functions. How do I know what prototcol/interface/objects to implement in the deftype?
@U7XR2PZFW if you know the name of the protocol, you can hop into the repl and look at the source for the protocol. e.g.:
cljs.user=> (source ILookup)
(defprotocol ILookup
"Protocol for looking up a value in a data structure."
(-lookup [o k] [o k not-found]
"Use k to look up a value in o. If not-found is supplied and k is not
a valid value that can be used for look up, not-found is returned."))
@lee.justin.m Thanks thats a handy tip. I was more asking about how to determine which of the clojure.lang interfaces are appropriate to implement for a given data structure. For example, if I know what behavior I want to see when I call (nth ds 5)
, where ds
is my new data structure, how do I know what interface to implement to achieve that.
@U7XR2PZFW oh well same trick. look for implements?
conditionals
my favorite feature of the language is that you can just dive into the source. if you have a good editor you can just do it straight from there
it would be interesting if there was a more programmatic way to see what protocols a method belongs to (i’m not even sure if that’s the proper way to phrase it) but i don’t know of one
Your suggestion of looking for implements?
sounds exactly like what I wanted, but I might be missing something. I just tried it for nth
and got this:
=> (source nth)
(defn nth
"Returns the value at the index. get returns nil if index out of
bounds, nth throws an exception unless not-found is supplied. nth
also works for strings, Java arrays, regex Matchers and Lists, and,
in O(n) time, for sequences."
{:inline (fn [c i & nf] `(. clojure.lang.RT (nth ~c ~i ~@nf)))
:inline-arities #{2 3}
:added "1.0"}
([coll index] (. clojure.lang.RT (nth coll index)))
([coll index not-found] (. clojure.lang.RT (nth coll index not-found))))
But I see no mention of implements?
. I know from trial and error that I should be looking for something like clojure.lang.Indexed
.weird the source in clojurescrip is totally different and contains this:
(implements? IIndexed coll)
(-nth ^not-native coll n)
this is probably worth asking in #clojure because I think whats going on is that some of the lower level functions are falling back to a java protocol. hence the clojure.lang.RT
. I think that the “protocol” stuff is a wrapper around java interfaces
clojure interfaces / protocols are very granular but you can see them with supers
(ins)user=> (supers (class []))
#{clojure.lang.IPersistentVector clojure.lang.IObj clojure.lang.IPersistentStack java.util.RandomAccess clojure.lang.IKVReduce clojure.lang.Associative clojure.lang.Seqable clojure.lang.IMeta clojure.lang.Indexed clojure.lang.IReduce java.lang.Iterable clojure.lang.ILookup clojure.lang.IEditableCollection clojure.lang.APersistentVector java.util.List clojure.lang.IReduceInit clojure.lang.IHashEq java.lang.Object java.util.Collection clojure.lang.Counted clojure.lang.IFn clojure.lang.AFn clojure.lang.Reversible java.util.concurrent.Callable java.lang.Comparable clojure.lang.Sequential java.lang.Runnable clojure.lang.IPersistentCollection java.io.Serializable}
(ins)user=> (supers (class ()))
#{clojure.lang.IPersistentList clojure.lang.IObj clojure.lang.IPersistentStack clojure.lang.ISeq clojure.lang.Seqable clojure.lang.IMeta java.lang.Iterable java.util.List clojure.lang.IHashEq java.lang.Object clojure.lang.Obj java.util.Collection clojure.lang.Counted clojure.lang.Sequential clojure.lang.IPersistentCollection java.io.Serializable}
to nitpick, java has no protocols, only interfaces, and most of clojure.core uses interfaces
so how do you tell what interfaces/protocols a function like nth
implements (or reifies or whatever the right word is)
@U051SS2EU Thanks! supers
get's me part of the way there, but its about the source for the PersistentVector etc.
@lee.justin.m if you follow the functions nth calls down the call stack, eventually you get to interface methods
@U7XR2PZFW that source is all in clojure.core and it follows a typical java project structure, it can be helpful to use a java editor on the clojure.core source or at least get a nice tree navigator plugin for github
@U051SS2EU So if we look at the implementation of nth
here: ([coll index] (. clojure.lang.RT (nth coll index)))
where would I look next to eventually end up on something that tells me clojure.lang.Indexed
?
src/jvm/clojure/lang/RT.java, which will define an nth method
so either Indexed or something nthFrom (defined right after that) accepts
definitely go with Indexed
interesting! thanks @U051SS2EU that was illuminating (for me at least)
there can be a mental block to going and reading the java, but it's not as bad as you might fear, and actually quite sane and simple for a java codebase for the most part
Ah gothca! That is awesome, thanks! So it looks like RT.java is going to hold the answers for this kind of thing. I checked (source %)
for conj, get, nth, and count and they all basically just call a function in RT.java
right - there's other interesting classes in that same clojure/lang directory but RT has a lot of the interesting stuff
RunTime
it's the code clojure needs to actually run (not just compilation stuff)
Does anyone have a good story for SublimeText 3? SublimeREPL is the only thing that claims it’s working but it’s lein only, and even in this case I couldn’t make it work.
Any suggestions on clojure editors? I’ve tried several and keep bumping into rough edges.
Recent survey results said half are in emacs and cider. I think cursive is the most polished and it's Rock solid. I like cider and emacs though
Maybe that was light table. I don’t recall. I’m using atom now, which is pretty decent.
I tried cursive and just didn’t like it. I need to try emacs I guess. I used it 20 years ago.
Atom and VSCode are getting some attention, yes. Spacemacs is a nice default Emacs setup that’s gotten popular with Vim people.
Are most devs using Linux / Mac? I’m on windows and that may be why I’m hitting some of these rough edges.
There's a thread on clojure verse about Windows specific stuff. Consensus was that it was really good with a few workarounds
For Java I use IntellIJ and thought about picking up Cursive at some point but never made the plunge.
i used emacs for 10 years and i’m never going back 🙂 i miss atom in many ways but cursive is just too solid not to use. plus @cfleming is a great guy and hangs around here and i like supporting him
@radekdymacz (and @scott.archer and others) If you want to try a curated Emacs package setup, take a look at Prelude -- maintained by the same guy who leads CIDER etc. It adds much better command completion, project navigation, and so on. I tried a number of curated Emacs setups but that seemed about the best (for Clojure development).
That said, after using Emacs 20+ years ago, then coming back to it for about two years while doing Clojure, I switched to Atom/ProtoREPL and have been happy with that.
Emacs takes a huge amount of work to get used to -- but most people who get anywhere close to mastery will never go back to other editors.
i like that all my environments look and act the same. editing clojure, scheme, go, random bash files, text files, log files, they all behave the way i'm used to
for some, that would also include email, calendar, organization, irc, rss, and on and on. for a particular tool it may not ever be the best. for a set of tools it's proven quite nice for me
All that matters to me re editor is that I can disable parainfer/paraedit and Atom's modular packages gave me that and I already used it for other languages.
Thanks for pointing me to this. I’ll definitely try it. So far I’m used to NeoVim (because regular Vim doesn’t support asynchronous plugins) but am still looking at Emacs since I started learning Clojure
btw emacs doesn't do multi-threading like nvim
but it has a much richer ecosystem of code, and a much nicer extension language for coding in
Everyone who’s coming from (neo)vim to emacs should check spacemacs (#spacemacs) - I switched from vim, although I had only few months experience back then
emacs just dipped its toe into multithreading with the 26 release. but it is far from pervasive
interesting
I switched to vim (then nvim) from emacs
I used emacs with evil mode but I got tired of long editor start up, freezes for long lines our repl output in a buffer, and crashes from bad elisp code
also, i was writing some elisp code the other day that used (let ((comment-start ...
and it broke emacs because that's a global variable and elisp is a dynamic lisp
oops!
yeah, when doing hardcore elisp hacking I find I need to have one emacs open for editing the final code, and a second (started with emacs -q
ideally) for testing the code
nothing worse than a bug in your code killing the editor you use to fix the bug
Yeah. And working now doesn't guarantee it'll work up when up restart. That's always a pain
the -q
flag does help there - starting without any of your other libs loaded
Oh I mean like closures that are hanging around or variables you defined and then changed and old state is laying around
Does core.async utilize all the cpu cores like golang or elixir?
@lee.justin.m you can kind of get that via the cat
transducer (into coll cat [c1 c2 ... cn])
@dfcarpenter on the jvm where multiprocessing exists, yes
@kyselyradek I came from just vim vim and setting up a ton of dotfiles myself. Emacs took a couple days to figure out but once I figured a few commands in Spacemacs, the rest came naturally for project handling and window navigation. Document editing for me is just vim so it really just expanded vims capabilities.
I’m trying to read a list of files in a directory using clojure and then get the absolute path. How do I go from what I believe is a Java array of File objects to a vector of strings containing the absolute path of each file?
Versus a real IDE, I only use spacemacs for Clojure. I use VSCode for JS, IntelliJ for Java... I'm pretty IDE agnostic but Spacemacs has been pretty easy to use and gets me into the groove of using shortcuts for navigation instead of clicking everything with my mouse.
When I try to map .getAbsolutePath across the array it doesn’t recognize .getAbsolutePath.
@scott.archer methods are not first class, so can't be passed as arguments on the jvm, you can create a lambda with a a method call in it, functions are first class as they are objects
#(.getAbsolutePath %)
same process with isDirectory
So since it can’t reference the function, you define a function that calls that method on whatever is passed in.
it can reference functions- instances of clojure.lang.IFn
it can't reference methods (behaviors of classes)
this is why there's two different words for these things
Map takes a function as a parameter, .getName isn’t a function, but you can define one on the fly that calls the .getName method on it’s input and returns that.
there are objects that reify and describe methods in reflection packages but those aren't really methods either - they describe methods
@scott.archer that's correct - I nitpick on function vs. method because the distinction is an important one to understand
I get it now, thanks for the help, I would have been stumped on that one for a while.
I have a web app that is kind of a hybrid SPA. The home, login and create-new-user pages are all separate routes until the user logs in. Then they are served the SPA. I'm trying to get figwheel configured but when i run lein figwheel
then navigate to localhost:3449
in my browser i am presented with this: Figwheel server: Resource not found
I don't have any .html files in my app. Everything is generated via hiccup. How do you config figwheel to work with my client app that is behind some other routes?
Edit: never mind. I think I figured it out. I was mounting the app component incorrectly.
Is this an idiomatic way to ensure that all entries in a list are the same?
(every? (partial = (first coll)) (rest coll))
In Clojure I can fetch the HTML contents of a URL with (slurp "https://www.whatever.com"). How would I do it in ClojureScript?
and as a follow-up question (I'm new here), how do you type code snippets in these threads?
use three backtick on each side for multi-line, and one backtick on each side for individual items
>
`var
`oh cool
I don't know how to easily quote the three-backtick, but you likely get the idea
thanks,
I figured it was something like that
racket is well optimized for new learners
but clojure is a reasonable first lisp if you can ignore the weird stuff at first
(weird stuff being the jvm integration, which is less important when first learning)
I came to Clojure from Scheme which I learned because of SICP. But I see less purpose in going the other direction. Just go right to Clojure!
and other people say: learning racket to learn clojure easier is like learning french to learn spanish easier
holy crap! I was thinking of making that same thing, guess I'm too late
yeah it is just my fear of missing out that keeps me looking if the grass is greener when learning another lisp first lol
Ok another question. I’m trying to use shell/sh to execute a command, but I want to build the argument list as a vector and pass it to sh.
that's easier to do with ProcessBuilder / Process I think, but not hard
wait - just use apply
user=> (apply clojure.java.shell/sh "ls" ["-l" ".."])
{:exit 0, :out "total 722808\ndrwxr-xr-x 3 justin.smith ..."}