This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-11-06
Channels
- # announcements (2)
- # beginners (97)
- # boot (3)
- # cider (23)
- # clara (9)
- # cljs-dev (40)
- # cljsrn (6)
- # clojure (107)
- # clojure-finland (2)
- # clojure-india (3)
- # clojure-italy (15)
- # clojure-nl (2)
- # clojure-spec (107)
- # clojure-uk (91)
- # clojurescript (28)
- # cursive (10)
- # data-science (4)
- # datomic (26)
- # duct (1)
- # emacs (6)
- # events (9)
- # figwheel-main (4)
- # fulcro (4)
- # graphql (2)
- # jobs (3)
- # jobs-discuss (12)
- # juxt (7)
- # kaocha (6)
- # off-topic (8)
- # onyx (2)
- # parinfer (13)
- # pedestal (32)
- # portkey (1)
- # re-frame (58)
- # reagent (17)
- # reitit (21)
- # ring-swagger (3)
- # shadow-cljs (35)
- # spacemacs (1)
- # tools-deps (33)
- # yada (13)
Anyone have any experience using an ouath v1 client with clojure? I’m trying to translate an example from Ruby but running into some issues.
I find myself using something like (first (filter #(= (foo %) "some-val") xs))
a lot. Before I make it into a first-where
/`first-with` function, I bet I'm overlooking a better way to do it. Is there one?
(some pred? coll)
is that what you are looking for? Edit: Uhh, I am not sure whether I am reading that correctly
yeah no ignore me
@johb.maier There are several reasons why you probably shouldn't be doing that...
(this came up very recently in one of these channels)
In general filter
is going to give you a chunked result so first
doesn't mean that filtering stops on the first element -- up to 31 more elements might be realized.
Also, if you need to do a linear search, you might have the wrong data structure in the first place. Something to think about.
@seancorfield Thanks. It's in a testing context (I'm traversing a kind of "DOM") and a "linear" collection is what the library gives me to work with.
Then, (first (filter ...))
is likely the best you're going to get.
I think it was @andy.fingerhut who said, when this came up elsewhere, if it isn't in core, that's not because the core team haven't thought about it 🙂
I guess the best I could do is write more utility functions to hide that functionality and ease traversing the "DOM".
I usually ask because I'm often sure I just don't know some core functionality or interpret it wrong.
That's good to know, thanks! It's not a js array in my case though (pretty sure at least, I will check).
If it isn't, you're better off with (first (filter
as converting to a js array using clj->js will probably be much slower
Oftentimes you can avoid clj->js
and js->clj
.
For shallow conversion of an effectively immutable JavaScript array to ClojureScript,
vec
is much faster than js->clj
.
Going in the opposite direction,
into-array
is much faster than clj->js
.
occurs java.lang.Exception: No namespace: reply.eval-modes.nrepl found
anyone knows the reason ?
It's caused by a version conflict -- a bug in Boot 2.8.2 https://github.com/boot-clj/boot/issues/720
If you downgrade to Boot 2.7.2 it will go away.
thx, i will try it
Is there any way to decorate protocol methods when defining a record? E.g.:
(defprotocol MyP
(thing []))
(defrecord MyR
MyP
(thing (decorator (fn [_] 1)))
@UD12BERTK You can, but not exactly how you're doing it.
Say you wanted to create a decorator that would time the execution of a function:
(defn do-the-thing [this]
(dotimes [x 10000]
(+ 2 200))
"done")
(defprotocol MyP
(thing [this]))
(defrecord MyR []
MyP
(thing [this] (time (do-the-thing this))))
user=> (thing (->MyR))
"Elapsed time: 1.330381 msecs"
"done"
You can think of time as a decorator. It is implemented as a macro though. So you can write you're own macros to decorate things. You can also do it with a normal function, but the syntax will be a bit more contrived.
(defn time-decorator [func]
(let [start-time (System/currentTimeMillis)
ret (func)]
(println (- (System.currentTimeMillis) start-time)
ret))
Now you can decorate a function like so: `(time-decorator do-the-thing)`
But, since our `do-the-thing` function takes arguments, we need to wrap it: `(time-decorator #(do-the-thing this))`
(defprotocol Coffee
(get-cost [this])
(get-ingredients [this]))
(defrecord SimpleCoffee []
Coffee
(get-cost [this] 1)
(get-ingredients [this] "Coffee"))
(defrecord WithMilk [coffee]
Coffee
(get-cost [this] (+ 0.5 (get-cost coffee)))
(get-ingredients [this] (str (get-ingredients coffee) ", Milk")))
user=> (get-cost (->SimpleCoffee))
1
user=> (get-cost (->WithMilk (->SimpleCoffee)))
1.5
user=> (get-ingredients (->SimpleCoffee))
"Coffee"
user=> (get-ingredients (->> (->SimpleCoffee) (->WithMilk)))
"Coffee, Milk"
Hope this helpsno, you can use a higher order function inside the implementation of course, but that syntax isn't valid
for example i want eveything to be in one vetor.. Out of these args: [1 23] 4 56 [5 8 9]
concat is fine in a lazy context, the problems happen when you mix eager evaluation with stacked concat invocations
for that specific example #(vec (flatten %&))
would work, I can't think of anything else simple that would handle it
but flatten has some gotchas
that’s what I usual do at least, I don’t know of any clojure socket libraries and socket interop is pretty painless
(get [(t/now) (t/now)] 1)
=> #object[org.joda.time.DateTime 0x1dd01b46 "2018-11-06T22:11:10.695Z"
What is mydates
?
That should work...
(! 509)-> clj -Sdeps '{:deps {clj-time {:mvn/version "RELEASE"}}}'
Clojure 1.9.0
user=> (require '[clj-time.core :as t])
nil
user=> (def mydates [(t/now) (t/now)])
#'user/mydates
user=> (get mydates 1)
#object[org.joda.time.DateTime 0x2cd2c8fe "2018-11-06T22:16:35.465Z"]
user=>
Perhaps you have mydates
shadowed by a local binding in the context where you call get
on it?
Show code -- or your REPL session...
Does nth work?
get on anything that isn't associative simply returns nil
any chance you turned the vector into a lazy-seq with map, filter, etc??
nth
returns UnsupportedOperationException nth not supported on this type: Unbound clojure.lang.RT.nthFrom (RT.java:888)
that means mydates is a var that hasn't received a value yet
is it somehow declared but not defined yet?
com.handler/start> (def testt [(t/now) (t/now)])
#'com.handler/start/testt
com.handler/start> testt
[#object[org.joda.time.DateTime 0x21712cdb "2018-11-06T22:23:00.389Z"] #object[org.joda.time.DateTime 0x6867ddbc "2018-11-06T22:23:00.389Z"]]
com.handler/start> (get testt 0)
nil
com.handler/start>
that would be my guess, I mean, I am not sure what the mechanics going on behind the scenes that are causing that to happen are
but putting '/' in a namespace name will break a lot of things, so I am not surprised it breaks that
Just ran the app and it worked so you were right. The namespace does seem to be busted
Clojure 1.9.0
user=> (in-ns 'foo/bar)
#object[clojure.lang.Namespace 0x41709512 "foo/bar"]
foo/bar=> (clojure.core/refer-clojure)
nil
foo/bar=> (def a 1)
#'foo/bar/a
foo/bar=> a
1
foo/bar=> (identity a)
#object[clojure.lang.Var$Unbound 0x51972dc7 "Unbound: #'foo/bar/a"]
foo/bar=>
I am kind of surprised you were able to create a badly named namespace like that, it takes a fair amount of work to do (the ns macro won't let you)
I didn't create it but I dont have a file with a namespace defined as such so not sure whats happening
How did you start that REPL session? Clojure/Java or ClojureScript?
I mean, no need to answer those questions if you can start a new REPL and the issue goes away, but if it is a repeating problem you are having, and you are interested in figuring out why, maybe we can figure it out.
The value of the :main
keyword in Leiningen's project.clj file should be namespace name, not a function
Right above that :main
I have :ring { :handler com.handler/app, :init com.handler/init}
It is a little surprising to me that nothing blew up before the REPL started up and gave you a prompt, but there are enough functions in Clojure that don't throw exceptions on invalid input, that I am not completely surprised.
I don't know ring well enough to answer, but would guess that those values are expected to be function names, not namespace names.
what is :main
used for? Is that what the REPL is going to fire up as the initial namespace?
It is optional.
This heavily commented sample Leiningen project.clj file has lots of info on what various parts of a project.clj file are for, but I wouldn't recommend reading it from cover to cover unless you are bored or intensely curious. Use it when you see something unfamiliar, although since project.clj files can also contain library-specific add-ins like the one you showed for Ring, this file cannot document everything you might see: https://github.com/technomancy/leiningen/blob/master/sample.project.clj
com.handler> (require '[clj-time.core :as t])
nil
com.handler> (def mydates [(t/now) (t/now)])
#'com.handler/mydates
com.handler> mydates
[#object[org.joda.time.DateTime 0x6b9cf4ad "2018-11-06T22:41:15.525Z"] #object[org.joda.time.DateTime 0x7cce1e84 "2018-11-06T22:41:15.525Z"]]
com.handler> (get mydates 0)
#object[org.joda.time.DateTime 0x6b9cf4ad "2018-11-06T22:41:15.525Z"]
com.handler>