This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-05-12
Channels
- # announcements (1)
- # babashka (42)
- # beginners (114)
- # bristol-clojurians (2)
- # calva (7)
- # cider (4)
- # clj-kondo (7)
- # cljs-dev (37)
- # cljsrn (13)
- # clojure (114)
- # clojure-austin (3)
- # clojure-europe (5)
- # clojure-nl (10)
- # clojure-spec (77)
- # clojure-sweden (4)
- # clojure-uk (16)
- # clojurescript (52)
- # conjure (155)
- # core-async (18)
- # cursive (23)
- # datomic (20)
- # duct (2)
- # emacs (13)
- # figwheel (3)
- # figwheel-main (9)
- # fulcro (31)
- # gis (8)
- # helix (33)
- # jobs (12)
- # jobs-discuss (66)
- # kaocha (4)
- # lein-figwheel (1)
- # meander (16)
- # off-topic (5)
- # pathom (13)
- # pedestal (6)
- # quil (6)
- # rdf (17)
- # re-frame (32)
- # reagent (34)
- # reitit (30)
- # remote-jobs (1)
- # ring (2)
- # shadow-cljs (149)
- # spacemacs (1)
- # sql (8)
- # tools-deps (90)
- # xtdb (19)
Hi, is it not recommended to use core async channels in stateful transducers?
probably not. what are you trying to do?
since channels can be supplied transducers, it seems unlikely that there would be a use case where you would use a channel within a transducer
i'm trying to write a transducer does a partition-all
but with a timeout. For example if I have (def foo (chan n (partition-all-with-timeout 3))
and I push from 1 to 10 into the channel, I'd like to get 10
out from the channel after x
ms.
there’s a way to tell a transducer to “complete” which* will cause a transducer like partition-all
to flush its remaining contents
I would still have the timeout on the “outside” of the transducer and once the timeout triggers, get the transducer to complete and flush any remaining contents
Oh cool! I'll dabble with that. Thanks alot! I'm new to transducers 😅
(def foo (chan 5 (partition-all 3)))
(doseq [i (range 10)]
(async/put! foo i))
(async/poll! foo)
;; [0 1 2]
(async/poll! foo)
;; [3 4 5]
(async/poll! foo)
;; [6 7 8]
(async/poll! foo)
;; nil
(async/close! foo)
(async/poll! foo)
;; [9]
this definitely isn’t the best example, but hopefully it helps
in this example, closing the channel will case the transducer to “complete”
so if closing the channel is appropriate, then you just do:
(go (<! (timeout 5000)) (close! foo))
to close the channel after 5 seconds
also, async/poll!
is usually not what you want to use. I’m just using it for this example so I don’t accidentally block my repl 😬
hehe cool! Thanks alot :thumbsup:
Is there any practical way to implement apropos
for any class that could be loaded?
(assuming that we are only concerned with classes that are loaded from the filesystem)
I find it really annoying when I read java documentation that says "use class X" but doesn't mention where the class should be imported from
For any class that could be loaded, where you believe you have already installed a JAR defining that class somehow before? Or one that you may never have copied to your computer before?
If the latter, then Google searches for things like "java someClassX" can often give information about the class, methods in its API, etc.
including a full hierarchical package name like java.lang.foo.bar.ClassX
If you have an example class "X" that you are having difficulty with, you can mention it here and someone may be able to demonstrate the process.
Assume I have the jar in my deps.edn or project.clj and it's been setup
I want a Java equivalent to (apropos "cat")
Of course I can google, but that just adds friction to exploring a new java library
JAR files are typically copied into your $HOME/.m2 directory the first time you run a program like clj
or lein
that has the project as a dependency.
Java classes don't have the built-in documentation that Clojure namespaces and functions do.
Sure, but it should be possible to do something like "find all the classes that have 'Client' in their name"
It's because of that built-in documentation -- docstrings -- that stuff like apropos
can work as it does.
For an individual JAR file, you can run a command like jar tvf some-file.jar
to see a list of all classes defined within it
You could write a program (and probably someone has, but not difficult to write a bash script to do this) that looks for all JAR files in your $HOME/.m2 recursively and does jar tvf ...
on each one
@U0CMVHBL2 that interrupts my REPL flow
If you haven't loaded the class, it won't be in memory. You could write something that scanned the classpath and looked inside every JAR file it found as well as any "loose" class files found on the classpath for a class name match.
Don't do it in the REPL, but in a terminal window
Or implement it in Clojure instead of as a bash script
@U2YUELSSF If you wanted to write such a thing, you'd be replicating part of what something like depstar
does -- it reads the classpath and reads any JARs it finds there.
Interesting, maybe I can use that as a library
I figured this out, which basically works:
(defn classpropos [pattern]
(run! (comp println
(memfn getName))
(into []
(filter #(re-matches pattern (.getName %)))
(.getAllClasses
(ClassPath/from
(.getContextClassLoader
(Thread/currentThread)))))))
using: https://guava.dev/releases/17.0/api/docs/com/google/common/reflect/ClassPath.html
Thanks, I think this'll work for now
I often use clj to follow along with documentation for Java libraries, but it's really annoying having to figure out where the classes come from because the docs assume your IDE will find the class for you
@U2YUELSSF Just checking: did you already try Clojure's javadoc
function to locate docs for a given class?
@seancorfield doesn't that require knowing the fully qualified name?
Just to be clear, the point of apropos
is to find symbols that contain a string: e.g.:
user=> (apropos "cat")
(clojure.core/cat clojure.core/concat clojure.core/lazy-cat clojure.core/mapcat clojure.core/replicate clojure.spec.alpha/cat clojure.spec.alpha/cat-impl clojure.spec.gen.alpha/cat)
I use it all the time in CL when I have a general idea what I want to do, but don't know the specific function that implements it.
Ah, OK. Just wanted to check your use case.
I hope I’m not being too pedantic, I tend to overexplain when I suspect someone is misunderstanding me.
The name apropos exists in Clojure, but it does not scan for Java class names, only Clojure Var names.
A lot of Java documentation I've seen not only doesn't tell you which packages things are in (and doesn't show the import
statements) but it also doesn't tell you the Maven group/artifact IDs! 🙂 That means you can't even find the right thing to add to your project in the first place...
Is there a (reasonably idiomatic) way to spec a function that takes no arguments? I have a function like this, but am not sure what to put for :args.
(defn get-free-port
"Returns an available port."
[]
(let [socket (.ServerSocket. 0)]
(.close ^.ServerSocket socket)
(.getLocalPort ^.ServerSocket socket)))
(s/fdef get-free-port
:args >>>Not sure what to put here.<<<
:ret (s/& int? #(< 1023 % 49152)))
@noah (s/cat)
means a sequence of zero things in Spec.
(I think that question cropped up on StackOverflow recently -- was that you asking that?)
@seancorfield Wasn't me, and didn't find it as I was googling, for some reason. Must not have thought of the right search terms. Thanks!
There's a bug in the code I posted above btw. It should be (s/and int? ...)
not (s/& int? ...)
~/p/c/acl ❯❯❯ tree scratch
scratch
└── remove.clj
0 directories, 1 file
~/p/c/acl ❯❯❯ clj -Sdeps '{:paths ["scratch"]}' -Spath
scratch:/Users/dan/.m2/repository/...
remove.clj in scratch, its on the classpath but
~/p/c/acl ❯❯❯ clj -Sdeps '{:paths ["scratch"]}' -m scratch.remove
Execution error (FileNotFoundException) at clojure.main/main (main.java:40).
Could not locate scratch/remove__init.class, scratch/remove.clj or scratch/remove.cljc on classpath.
You want it to look for remove
on the classpath (not scratch.remove
).
scratch
is like src
-- the ns path is relative to src
but does not include src
oh of course! thanks @seancorfield
My work is here done... bed time! 🙂
heya, i'm looking for a replacement for https://github.com/ztellman/aleph
https://github.com/http-kit/http-kit you can try this one
http-kit iirc had issues with not supporting streaming properly, all body was buffered into memory
which is quite nasty
using :as :stream
simply gives you https://github.com/http-kit/http-kit/blob/master/src/java/org/httpkit/BytesInputStream.java, which as you can see is basically byte array wrapper 😕
If you are doing web dev you can try this lib https://github.com/sunng87/ring-jetty9-adapter
thanks, but i'd like to have netty based solution
can anyone suggest me anything good? i've seen ring-jetty stuff but
jetty is bit heavy for me
especially if other libraries which i use also utilize netty
interesting, will definitely look into it
not being updated, has few nasty bugs
could you share those bugs you ran into? i was thinking of forking it and keeping it up to date
Is there a standard way of checking if a dependency is available? I'm working on a library, and I'd like to provide a component namespace. It's usage is optional, and requiring the my-lib.component
namespace shouldn't blow up if the application doesn't use Component as a dependency (in deps.edn/project.clj). At the moment, I'm wrapping require in a try-catch in the my-lib.component
namespace, but it feels like there should be a better way to do this.
@lukaszkorecki why shouldn't it blow up. That namespace isn't usable without component.
Yes, but accidentally requiring it, reloading etc shouldn't cause any issues - it's optional after all
you can attempt to resolve a var in an ns, and fallback if it resolves to nil
scratch=> (resolve 'clojure.core/+)
#'clojure.core/+
scratch=> (resolve 'does.not.exist/foo)
nil
this means that part of your API is now either requiring some ns or not requiring it I guess
because resolve will fail even if the ns is available, if nobody has loaded it yet
@lukaszkorecki it won't be reloaded under normal circumstances & if its required by accident then blowing up seems like a good thing to do
Good point: it's just that I want to make dependency on Component truly optional, including gracefully handling requiring the my-lib.component
namespace. Maybe I'm too protective of potential users ;-)
There are also ways to construct Component-compatible things without actually requiring Component these days.
See https://github.com/seancorfield/next-jdbc/blob/master/src/next/jdbc/connection.clj#L249 for an example of a Component-compatible entity that is a safe no-op if Component isn't on your class path.
Oh that's neat - I forgot about the ability to implement protocols via metadata. I didn't think about using it that way - thanks!
bash-3.2$ clj -Sdeps $(cat scratch/remove.deps.edn) -m remove
Error while parsing option "--config-data {:deps": java.lang.RuntimeException: EOF while reading
how can i use a file as a dep?As a rule in bash, if you see a $ it should have a " around it (somewhere). At this point, paranoia wins out for me, and I always do it.
it's like every $ is a @, and you need "" in order to get
or something like that
http://clojure.java.io/input-stream on an http://clojure.java.io/file should give you a FileInputStream which has a skip
method https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html#skip(long)
scratch=> (-> "../../../.profile" (io/file) (io/input-stream) (doto (.skip 20)) (slurp) (subs 0 10))
"ON\" ]; the"
using core.match
, its possible to have the same clause for two matches? like its possible in C-like
languages with a switch?
is there a way to automatically change the file's namespace when I change its directory?
so if I change the directory from foo to foo1 of a bunch of files bar%d, how to automatically change all of them from foo.bar1 -> foo1.bar1, foo.bar2 -> foo1.bar2 etc?
like I have a Leiningen + spacemacs + cider project if it's easier through any of those tools
this might help if you haven't looked at it yet https://github.com/clojure-emacs/clj-refactor.el