This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-10-12
Channels
- # beginners (58)
- # boot (4)
- # calva (1)
- # cider (13)
- # cljdoc (1)
- # cljs-dev (7)
- # cljsrn (14)
- # clojure (93)
- # clojure-canada (1)
- # clojure-conj (1)
- # clojure-germany (1)
- # clojure-italy (6)
- # clojure-losangeles (3)
- # clojure-nl (8)
- # clojure-spec (6)
- # clojure-uk (77)
- # clojurescript (3)
- # cursive (5)
- # data-science (6)
- # datomic (52)
- # emacs (1)
- # figwheel-main (2)
- # fulcro (6)
- # graphql (7)
- # jobs (9)
- # leiningen (1)
- # luminus (15)
- # mount (14)
- # off-topic (94)
- # pedestal (1)
- # re-frame (7)
- # reagent (10)
- # shadow-cljs (75)
- # spacemacs (4)
- # test-check (15)
- # tools-deps (23)
- # unrepl (1)
Hi folks
Just a quicky here
So, I have this piece of code:
((clojure.core/fn
[n]
(clojure.core/let
[__self_rec__
(clojure.core/fn
__self_rec__
[n]
(clojure.core.async/go
(if (< n 2) 1 (*' n
(clojure.core.async/<!
(__self_rec__ (dec n)))))))]
(clojure.core.async/<!! (__self_rec__ n))))
100)
And it's throwing this:
IllegalArgumentException No implementation of method: :exec of protocol: #'clojure.core.async.impl.protocols/Executor found for class: clojure.core.async.impl.exec.threadpool$thread_pool_executor$reify__13318 clojure.core/-cache-protocol-fn (core_deftype.clj:568)
Any ideas?
Awesome btw
is there any way to either do unquote splicing in a quoted (non backtick'd) list, or to specify that symbols in a backtick'd list should be opted out of auto namespace expansion?
In a backtick'ed expression, you can opt out of auto namespace expansion by preceding the symbol with ~' but that would require doing that on all symbols you want to opt out for. I don't know of a way to do it globally for most of the expression.
In a normally quoted expression, there is no way to do splicing built into Clojure. There might be some templating library somewhere that can do something similar to what you want there.
cool, well, that little opt out trick should be fine. I'm not in the habit of writing particularly large macros
A little clean-up on the code:
(require '[clojure.core.async :refer [go <! <!!]])
(def fact (fn [n]
(let [__self_rec__
(fn __self_rec__ [n]
(go
(if (< n 2)
1
(*' n (<! (__self_rec__ (dec n)))))))]
(<!! (__self_rec__ n)))))
(fact 100)
> IllegalArgumentException No implementation of method: :exec of protocol: #'clojure.core.async.impl.protocols/Executor found for class: clojure.core.async.impl.exec.threadpool$thread_pool_executor$reify__13318 clojure.core/-cache-protocol-fn (core_deftype.clj:568)
Also equivalent:
(def fact (letfn [(__self_rec__ [n]
(go
(if (< n 2)
1
(*' n (<! (__self_rec__ (dec n)))))))]
(fn [n] (<!! (__self_rec__ n)))))
(fact 100)
;; thows the same exception
it seems to have worked for me! @mrdalloca
=> (fact 100)
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000N
Is core.async expected to work with embedded (fn ...)
forms? I haven't used it, but know it has some limitations of what it can handle inside of the code in a go
form.
core.async can't cross fn
boundaries, lexically, but this is not what's going on this case
I dove into that new library for a bit yesterday, and looked at doing what you're doing here - wrapping the "rec" function in letfn, then realized by doing that you lose the ability to use the mutually recursive functions because they can't access each others "helper fn"
Yeah totally
Didn't think of the mutually recursive functions
I do think that ideally there'd be some way to avoid defining two functions in the namespace. My only idea for this was to potentially hide the recursive helper in the function metadata, but I feel like that's probably not a great solution.
I'm struggling with something I thought would be simple. I have a SocketAddress, can I figure out the host IP, even if it's been set to 0.0.0.0 in the socket address? My current thought is to handle that specific case, but I'm not sure that's a great solution.
Couple stack overflows that might be helpful: https://stackoverflow.com/q/2381316/2103383 https://stackoverflow.com/q/7348711/2103383
how do I know where clj
puts its downloaded artifacts from maven? Does it use some environment variable or maven config?
~/.m2/repository/
?
The default for maven is ~/.m2/repository/
.
It also seems it's configurable: https://github.com/clojure/tools.deps.alpha/blob/master/src/main/clojure/clojure/tools/deps/alpha/extensions/maven.clj#L74
Here's the default value: https://github.com/clojure/tools.deps.alpha/blob/master/src/main/clojure/clojure/tools/deps/alpha/util/maven.clj#L93
for leiningen: https://stackoverflow.com/questions/12579335/leiningen-how-to-customize-the-location-of-the-m2-folder
@mrdalloca that specific error can be caused by reloading the core.async code - you end up with parts of core.async running using the old versions of protocols, which breaks as soon as you try to invoke their methods
@noisesmith Updated clojure version, restarted the repl and ta da, it worked!
Here's the code for factorial:
user> (def fact (recfn
[n]
(if (< n 2)
1
(*' n (rec-self (dec n))))))
#'user/fact
user> (fact 100)
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000N
Where recfn
is the equivalent to fn
but it's made to be able to use with rec-self
Another example:
user>
(declarec EVEN?)
(def ODD? (recfn [n]
(if (zero? n)
false
(rec-self (dec n)))))
(def EVEN? (recfn [n]
(if (zero? n)
true
(rec-self (dec n)))))
#'user/__rec__:EVEN?
#'user/ODD?
#'user/EVEN?
user> (ODD? 10)
false
user> (ODD? 10001)
false
user> (ODD? 1000001)
false
@hiredman it's wrong, my bad
I'll fix this and test
@hiredman > fn already does that > (fn f [n] do-whatever-with-f)
user> (def fact (fn fact [n]
(if (< n 2)
1 (*' n (fact (dec n))))))
#'user/fact
user> (fact 10000)
Evaluation error (StackOverflowError) at (Numbers.java:3909).
null
user> (def fact (recfn [n] (if (< n 2)
1 (*' n (rec-self (dec n))))))
#'user/fact
user> (fact 10000)
;; enormous number
@hiredman yeah it is. But the point here is not blowing the stack
Have you seen the new recursion library?
It's worth checking out its rationale
Well, that's another discussion but you got a point
I mean, it is a cute toy, but I use core.async all the time, and I definitely don't want to be calling async/go for everything single function call
core.async needed to invent some form of continuation, and now this other library tries to use those continuations more generally?
but core.async doesn't expose the general form of continuation, so this library that builds on it sticks every function call in a threadpool queue
you may want to checkout https://github.com/positronic-solutions/pulley.cps
What is the correct function if I need Java's modulus (%) operator? Is it mod or rem or neither?
@boccato Ask doc
in the REPL
user=> (doc rem)
-------------------------
clojure.core/rem
([num div])
remainder of dividing numerator by denominator.
nil
user=> (doc mod)
-------------------------
clojure.core/mod
([num div])
Modulus of num and div. Truncates toward negative infinity.
There's also
user=> (doc quot)
-------------------------
clojure.core/quot
([num div])
quot[ient] of dividing numerator by denominator.
nil
@boccato It looks like Java's %
operator is technically a remainder, not a modulus? And looking at (source mod)
I see it's defined in terms of rem
but then adjusts a negative remainder to be positive. I saw a SO post that defined a mod
function in Java in a similar way on top of the %
...
Another question, what libraries / framework would you suggest to me to port a small python/django app that implements a http API. I need file upload, to return some JSON, authentication and serve a simple js/html page that access the API (with a login page).
I am more concerned with the security part. If there is something mature for the authentication / JWT part.
@lilactown It's just going to be whatever web server library you choose and a couple of -e
options...
I seem to recall doing something similar with lein-try
back in the day... let me see if I can find that...
@lilactown Because I couldn't resist...
clj -Sdeps '{:paths ["."] :deps {ring {:mvn/version "RELEASE"}}}' -e "(require '[ring.adapter.jetty :refer [run-jetty]] '[ring.middleware.resource :refer [wrap-resource]])" -e '(run-jetty (wrap-resource (constantly nil) ".") {:port 3333})'
That will serve files as static assets in the current folder.And "LATEST"
.
(I found an old blog post where I did something similar with lein-try
-- and someone commented on the post with the python
command too as I recall :rolling_on_the_floor_laughing: )
might be fun to create a "runner" that would detect if ring/jetty/http-kit is on the classpath and automatically start it
and do things like auto reload CSS. maybe even come with nREPL to dive in and muck about
I just thought if it because I had to edit actual HTML/CSS for the first time in forever and missed the comfort of having a REPL around 😛