This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-11-14
Channels
- # beginners (33)
- # boot (38)
- # clara (21)
- # cljs-dev (1)
- # cljsjs (2)
- # cljsrn (12)
- # clojure (230)
- # clojure-argentina (1)
- # clojure-brasil (3)
- # clojure-dusseldorf (4)
- # clojure-france (9)
- # clojure-italy (1)
- # clojure-russia (123)
- # clojure-spec (46)
- # clojure-turkiye (1)
- # clojure-uk (60)
- # clojurescript (83)
- # core-async (6)
- # cursive (10)
- # datascript (19)
- # datomic (28)
- # defnpodcast (1)
- # emacs (7)
- # figwheel (7)
- # fulcro (29)
- # leiningen (29)
- # lumo (9)
- # off-topic (14)
- # om (1)
- # onyx (25)
- # pedestal (1)
- # protorepl (3)
- # re-frame (10)
- # reagent (41)
- # ring-swagger (11)
- # shadow-cljs (10)
- # testing (5)
- # unrepl (3)
- # vim (3)
Can it be that clojure spews out clojure.lang.LazySeq@1f
file-directories when useing io/file
within for loop. Wondering how I can prevent this.
yes, but what explains these directories
$ ls target/
clojure.lang.LazySeq@1f clojure.lang.LazySeq@316eef clojure.lang.LazySeq@92aa04bb src
clojure.lang.LazySeq@30b541e8 clojure.lang.LazySeq@712f77fe
possibly something is getting a name for a directory by calling str
on the lazy sequence
here's the function https://pastebin.com/B3vtXJJH
hello. I’m trying to do a simple http call using using clj-http
package:
(defn -main
[]
(client/get ""
(fn [response] (println response)))
Exception in thread "main" java.lang.RuntimeException: EOF while reading, starting at line 4, compiling:(samsing/core.clj:10:1)
and one more question 😄 it doesn’t seem to decode to json, no matter what option I pass
nvm, this solved it: https://stackoverflow.com/questions/37264250/clj-http-not-returning-json
I'm looking for a debugging library I saw recently (on reddit, probably), but can't seem to find it now. I only looked at it for a moment, but I believe it involved one or more macros for writing values to an atom. Something like that. Anyone know what I'm talking about?
I put my lib in #announcements recently, and it involves some functions for putting stuff in an atom. They're not macros though, just functions. Lib: https://github.com/madstap/hugin Blog post: https://madstap.github.io/posts/dbg/
That's it! thanks. I checked #announcements, before posting this, but I guess I missed it.
I used to use spyscope (but it stopped working for me??), then I wrote sayid, but sometimes I still use def
and pprint
. Some of those times, I think I want something like hugin.
Yeah, def and pprint work great, but you have to put them in a do
. Apart from the debug atom, hugin has def<
and pp<
respectively that you can wrap around an arbitrary expression without changing how the code works.
is there any way to "transduce" this?
(frequencies
(reduce #(if (not (nil? (:samplevalue %2)))
(conj %1 (.intValue (:samplevalue %2)))) []
(qos/device-qos-range origem device qos alvo inicio fim)))
literally, more or less this:
(frequencies
(eduction
(keep :samplevalue)
(map (memfn intValue))
[{:samplevalue 3.1} {:samplevalue 2} nil]))
Has anyone ever used docjure for Excel file generation? Having an issue where even though I specify :data-format
it doesn't ever get applied to the generated file.
actually frequencies
is the reducing step here @fabrao so you can compact everything into:
(transduce
(comp
(keep :samplevalue)
(map (memfn intValue)))
(completing #(assoc! %1 %2 (inc (get %1 %2 0))) persistent!)
(transient {})
[{:samplevalue 3.1} {:samplevalue 2} nil])
@fabrao Depending how important speed is, the parallel solution is twice as fast, at the price of the increased complexity. I collected all in a gist: https://gist.github.com/reborg/37c76a61c9a60ddae8de737170e2e675
Before going parallel like this tho (with mutation, side-effects and whatnot), I'd carefully weight pro and cons. The very first solution with frequencies
and eduction
was easy to read and good 98% of the use cases.
Does anyone know of a way to get the frame-local variables printed during stacktraces?
I've done a little googling, and read some of clojure.stacktrace
, but nothing seems promising.
I don't think this is possible. https://stackoverflow.com/questions/4620430/getting-local-variables
I've never attempted to extend Throwables, a la slingshot
, but maybe that would be the way to go: create Throwables that automatically capture their local variables.
This blog seems to have some of the answer, from a frame-local-variables perspective: http://blog.element84.com/debugging-clojure-with-jdi.html
#?(:clj [b.acompat.macro :refer [read-at-compile-time]]
:cljs [b.acompat.macro :refer-macros [read-at-compile-time]])
^-- can this be simplified ?@qqq narrow the reader conditional to the :refer
/ :refer-macros
(as far as I can tell, that's the only difference)
@moxaj: maybe this is asking too much, but it seems that at compile time, it should be possible to infer if a symbol if a function or a macro, and have it behave accodingly
Oh right, this reminds me that wildcards are a creature comfort provided by shells.
@qqq there's a cljs feature called 'implicit macro loading'; add #?(:cljs (:require-macros [b.acompat.macro]))
to your b.acompat.macro
ns form and you can :refer
your macros as usual
@moxaj: are you suggesting:
;; foobar/macro.cljc
(ns foobar.macro
#?(:cljs (require-macros [foobar.macro])))
(defmacro black-magic [ ...])
;; other/code.cljc
(ns other.code
(:require [foobar.macro :refer [black-magic]]))
?Question: How do people feel about memoizing a function that performs IO to get something out of a database?
Does that raise red flags for anyone in and of itself?
I’m thinking of this from a readability / obviousness perspective for the benefit of the human reading the code down the road.
Right now the application should only need to read the data once for the duration of the app.
And I’m thinking of memoizing the function to keep things cleaner, so I don’t need to add yet one more thing to my global config
But down the road, it’s possible that I would need to read from the DB more than once per application run.
And I’m wondering if 6 months, a year from now, I would remember that the function was memoized, or if I would tear my hair out wondering why it isn’t reading new data in the DB
anyway, just seeing if people have experience or strong opinions about this sort of thing
haha, true story
yeah, that’s why I was hesitant about this
“Oh, memoizing is this clever, useful thing! I’ll use it!” But I’m not confident that it won’t burn me down the road
@fabrao Depending how important speed is, the parallel solution is twice as fast, at the price of the increased complexity. I collected all in a gist: https://gist.github.com/reborg/37c76a61c9a60ddae8de737170e2e675
Before going parallel like this tho (with mutation, side-effects and whatnot), I'd carefully weight pro and cons. The very first solution with frequencies
and eduction
was easy to read and good 98% of the use cases.
I'm trying to write a function that converts an unqualified keyword to a qualified keyword. I have this so far:
(defn qkw [kw]
(keyword (str (ns-name *ns*))
(str (name kw))))
unfortunately, it doesn't work in cljc as ns is undefined in cljs
is there a way to make this work in cljs too ?I had to spin up a clojurescript repl to see it for myself and yep calling *ns*
returns nil
that almost works, except ::. gets us the namespace where qkw is defined, not where qkw is called
I pasted your macro into a clj file and then called (require-macros '[my-ns :refer [qkw]])
at the figwheel repl and it but when I call (qkw :foo)
I get :my-ns/foo
so even with the macro it is still giving you the ns where it is defined and not where it's called. I'm not the best at writing macros so maybe it just needs to be tweeked
I tried to post a message to the google group yesterday, but it hasn't appeared. It's possible that it got hung up in moderation; any moderators around?
It's also possible that pressed the wrong button at some point.
@russell.mull I am one moderator of Clojure Google group. No messages are waiting for moderation approval right now. There are other moderators, so someone else may have seen it and made a reject decision on your message without me seeing it, but if your message was about Clojure, it shouldn't have been rejected.
@andy.fingerhut ok, thanks; I'll resubmit and be very careful about which button I press this time.
Is there any good way of creating an anonymous function which doesn't accept any arguments? I need to be able to create them nested.
#(function/one :alpha (partial function/two :bravo))
Using partial with full arguments works, but it doesn't feel right.@ghopper, you can’t nest #(...)
anonymous functions, but you can nest the (fn [] ...)
form
Yeah, I know I can just nest fn calls. Was just curious if there was a core function for this type of thing.
so either (fn [] (function/one :alpha #(function/two :bravo))
or (fn [] (function/one :alpha (fn [] (function/two :bravo)))
(A core function that allowed creating nested thunks)
@qqq I'm not the best at writing macros and especially calling them from cljs namespaces, but I think you might be on the right track
hey guys, I'm dealing with this strange issue -- when i call (keys my-map)
the key :example-key
is included in the results
also to be clear, i have code that's supposed to add that key and value to the map, so it not being there is problematic
app:cljs.user=> (:search-lol @re-frame.db/app-db ::but-why?)
:cljs.user/but-why?
app:cljs.user=> (keys @re-frame.db/app-db)
(:schema
:errors
:audio-label
:audio-player
:loaded
:duration
:routes-initialized?
:search-lol
:token
:active-page
:models
:status
:path-array
:file-download
:playing
:position
:page-query
:audio
:url-data
:location
:user
:audio-type)
here's the code incase i'm doing something obviously dumbWhat is (type @re-frame.db/app-db)
?
@yedi - How about (let [db @re-frame.db/app-db] (prn 'keys (keys db)) (:search-lol db :no-dice))
(to be sure the value isn't changing out from under you)?
what does (->>(keys @re-frame.db/app-db) (map name))
show?
you can make a keyword with a space in it
it might not be it, but might explain the weird behaviour
app:cljs.user=> (let [db @re-frame.db/app-db] (prn 'keys (keys db)) (:search-lol db :no-dice))
keys (:schema :errors :audio-label :audio-player :loaded :duration :routes-initialized? :search-lol :token :active-page :models :status :path-array :file-download :playing :position :page-query :audio :url-data :location :user :audio-type)
keys (:schema :errors :audio-label :audio-player :loaded :duration :routes-initialized? :search :token :active-page :models :status :path-array :file-download :playing :position :page-query :audio :url-data :location :user :audio-type)
:no-dice
app:cljs.user=> keys (:schema :errors :audio-label :audio-player :loaded :duration :routes-initialized? :token :active-page :models :status :path-array :file-download :playing :position :page-query :audio :url-data :location :user :audio-type)
checking for something like (keyword "search-lol ")
app:cljs.user=> (->>(keys @re-frame.db/app-db) (map name))
("schema"
"errors"
"audio-label"
"audio-player"
"loaded"
"duration"
"routes-initialized?"
"search-lol"
"token"
"active-page"
"models"
"status"
"path-array"
"file-download"
"playing"
"position"
"page-query"
"audio"
"url-data"
"location"
"user"
"audio-type")
¯\(ツ)/¯
(-> db
(assoc-in [:status :search-results-fetch audio-type] :success)
(assoc-in [:search-lol :search-results] results))
My next guess was unicode weirdness, but if you typed in :search-lol
in the assoc
then that's surely not it.
and it does automatically create the nested maps:
app:cljs.user=> (assoc-in {} [:test :nested] true)
{:test {:nested true}}
@plins - Should be able to use the Java date/time API without any libs. https://docs.oracle.com/javase/tutorial/datetime/iso/index.html
@yedi - you've stumped me! ¯\(ツ)/¯
what does that print?
(let [db @re-frame.db/app-db]
(doseq [k (keys db)]
(prn k (= k :search-lol) (contains? db k) (get db k ::not-found))))
just added (= k :search-lol)
as something to print
but then it start printing everything again and an old key that should no longer be in there got printed out too
how are you printing the value?
ie. are you using a repl
or putting it as a top level form in a cljs file
seems like the app + dev tools might be in a weird state
there are something things can cause a figwheel refresh
where it will rerun all the top level forms in your code
like saving a file
maybe there’s something with autosave and not having the state setup to make figwheel happy (eg. using (def state (atom {}))
instead of (defonce state (atom {}))
I sometimes hit use-cases for this FN in threading macros. Is there a better/builtin implementation, or suggestions on the name?
(defn pred-or-nil
[pred? v]
(when (pred? v) v))
@smith.adriane idk i'm at a loss. something weird is definitely happening because it's worked fine with every other key every tried previously
the repl doesn't seem trustworthy, so i'm tryign to see if i dissoc the key somewhere in my code
@yedi did you try it from a command-line repl?
I guy I work with was having some really weird issues that cleared up when he deleted the contents of the /target dir, fwiw
Oh that’s right, I was thinking clj not cljs, nvm
but maybe the /target thing will help
it does seem like a weird situation. I would check to make sure that you’re following the tips for reloadable code, https://github.com/bhauman/lein-figwheel#writing-reloadable-code. I would also try some of these tests outside of the figwheel repl to try to isolate the issue
in the past, Ive added debug functions like
(defn ^:export debugprints []
(let [db @re-frame.db/app-db]
(doseq [k (keys db)]
(prn k (= k :search-lol) (contains? db k) (get db k ::not-found)))))
and then you can call my.namespace.debugprints()
from the dev console
Should some-> be able to have as-> contained within it?
@ghopper anecdotally, I remember having issues with it a few months ago. I usually bust out https://github.com/rplevy/swiss-arrows when I need improved threading
the -> macros are purely transformations of form, usually when people have trouble with them it is because they expect them to be aware of semantics
Alright, I'd changed a -> into an as->; I needed to nest my original -> inside the new as-> which was inside some->. My mistake.
Huh, @quest, I'll have to look into that.
Yeah, probably.
It doesn't actually look too bad in reality.
defn callback [{{{cookie-state :value} "state"} :cookies
{:strs [code hd] query-state "state"} :query-params}]
(if-let [userinfo
(and (= cookie-state query-state)
(some->
(-> @(http-client/post (env :google-token)
{:form-params {:code code
:client_id (env :google-id)
:client_secret (env :google-secret)
:redirect_uri ""
:grant_type "authorization_code"}})
:body
(cheshire/parse-string true)
:access_token)
(as-> access
(-> @(http-client/get (env :google-user-info)
{:oauth-token access})
:body
(cheshire/parse-string true)))))]
(merge
(response/redirect "/")
{:cookies {"access" (compact/sign
{:email}
(env :secret))}})
(response/redirect "/login"))
Fair enough. Do you have a specific way you'd change it?
The alternative I was running into was having more than 2 exit points. (Duplicating my redirect to /login on multiple branches)
Good call there. I definitely will. This was largely just a matter of getting it working.
Explicit cps?
often you can have control flow diverge down two paths, and then you need to join the control flow
so you just create a function that represents all actions after the join point, and pass it in to each branch
Oh, I'm following now. So you'd have a default-response defined at the beginning which calls the redirect to /login?
That makes sense. Is this a specifically defined pattern? Link?
I dunno, it is just sort of I think of code after fiddling with compilers for a while
I think this is what I was looking for: https://en.wikipedia.org/wiki/Continuation-passing_style
oh yeah, cps is a thing, and has lots of applications, a lot of which are sort of tangential to the relatively simple "hey, I have this repeated bit of code for different control flow branches, why not make it a function and reuse it on both branches"
:thumbsup: Thanks
Hi! I’m trying to receive an AWS SNS message in pedestal but I’m facing a small problem. When pedestal receives a message with content-type application/json it automatically convert the body to keywords. But aws sns sends its message with content-type text/plain and pedestal does not convert the message so I can extract the data I need. Is there a way to convert the entire message? I’m trying to create an interceptor to convert the requests but It’s not working problem:
(defn- parse-string-to-map-keys
[string-to-convert]
(walk/keywordize-keys string-to-convert))
(defn- parse-stream-to-map-keys
[stream-to-convert]
(json/read-str (slurp ( stream-to-convert)) :key-fn keyword))
(def parse-sns-message-to-json
(interceptor/interceptor
{:name ::parse-sns-message-to-json
:enter
(fn [{:keys [request] :as context}]
(let [ headers (try
(parse-string-to-map-keys (:headers request))
(catch Exception _))
body (try
(parse-stream-to-map-keys (:body request))
(catch Exception _))]
(if (and headers body)
(do
(assoc context :request (-> request
(assoc :headers headers)
(assoc :json-params body))))
(-> context
terminate))))}))
The body of the message stays like this:
:body #object[org.eclipse.jetty.server.HttpInputOverHTTP 0x9344f6 "HttpInputOverHTTP@9344f6[c=1804,q=0,[0]=null,s=EOF]"], :scheme :http, :request-method :post}
I could try this, but I don’t know the order of the functions would pedestal try to convert the message before or after my interceptor?