okay neat
Hmmm. Seems like chrome doesn't care xD
Hmmm.
So I tried "nativefier" on a login endpoint https://github.com/nativefier/nativefier
the CSRF token is changing, but it's apparently not getting the right one via this embedded electron app
not mission-critical, but still a perplexing issue.
Huh. I dunno.
I thought maybe I was passing in an outdated token or something... the web version works fine, the nativefier version does not
When I type: ((apply comp '((+ 1) (+ 1))) 1) I get: Execution error (ClassCastException) at clj-event-loop.core/eval2305 (form-init8093888511408098339.clj:1). clojure.lang.PersistentList cannot be cast to clojure.lang.IFn
sure
You have a quoted list in there so it's just data, not functions.
((apply comp [inc inc]) 1)
will work.
Also (+ 1)
is not a function, it's a value: (+)
is 0, (+ 1)
is 1, (+ 1 2)
is 3.
inc
is "add 1" or your could use (partial + 1)
and get a function that adds one.
Hi, can anyone help me with setting up Intellij?
I've set up a barebones cljs project as per https://andrearichiardi.com/blog/posts/clojurescript-cursive-shadow-setup.html, but IDEA won't stop complaining, and fetching maven repositories fails, like so-
This is what my deps.edn looks like
@kifah FYI there's a #cursive channel too
Thank you Simon, crossposted there 🙏:skin-tone-3:
Every time I restart doing something in clojure, it get confused again. Can someone help me understand why this doesn't equal 0 when I
is an integer?
(reduce + 0 (concat (range (- i) -1)
(range 1 i))))
I see, it is because range DOES NOT include its upper bound ....
if I add the numbers (range -99 -1)
to the numbers in (range 1 99)
, I don't get 0.
I have a question about loop
/`recur`. If I have a function which DOES work recursively, is it still better to write it as loop
if possible? i.e., is there some speed advantage?
Singletons are 1 instance per JVM instance, right? (As opposed to 1/cpu, 1/motherboard, etc.) I wouldn't think multiple JVM's on the same machine would share runtime, but I wanted to check.
Is there a way that I can write a clojure fn to operate on a Java instance without having to pass the Java instance into every method call?
I was trying to rewrite a small but precise Scala function in Clojure, and I am unable to make it as concise. in Clojure it is roughly twice the size. The function is purely functional, doesn't depend on OO paradigms, so my instinct is it ought to be very concise in Clojure. Here is the Scala code.
def treeFold[A](seq: Seq[A])(z: A)(f: (A, A) => A): A = {
def dwindleTree(stack: List[(Int, A)]): List[(Int, A)] = {
stack match {
case (i, b1) :: (j, b2) :: tail if i == j =>
dwindleTree((i + 1, f(b2, b1)) :: tail) case _ => stack
}}
val stack = seq.foldLeft((1, z) :: Nil) {
(stack, ob) => dwindleTree((1, ob) :: stack)
}
stack.map(_._2).reduce { (a1, a2) => f(a2, a1) } }
One thing that makes this concise is that the first case in the pattern match case (i,b1)::(j,b2)::tail if i== j
simultaneously checks two different conditions. In the Clojure code I need two concentric if
forms.
(defn tree-fold
"omitting doc string...."
[f z coll]
(letfn [(dwindle-tree [stack]
(loop [stack stack]
(if (empty? (rest (rest stack)))
stack
(let [[[i b1] [j b2] & tail] stack]
(if (= i j)
(recur (cons [(inc i) (f b2 b1)] tail))
stack)))))]
(let [stack (reduce (fn [stack ob]
(dwindle-tree (cons [1 ob] stack)))
(list [1 z])
coll)]
;; stack is guaranteed to have at least one element
(reduce (fn [a1 a2] (f a2 a1))
(map second stack)))))
Isn't there a better way?depends what you mean by "singleton" and "runtime", but generally yes
there are several ways to work with "ambient" objects: • Clojure dynamic vars (available and overridable on a per thread basis) • Java threadlocals (threads only see "their" version) • global state (all Clojure vars are global state)
@jimka.issy Perhaps this can be made concise with core.match. I will give it a try
that said, all of these mechanisms result in functions that depend on external "stuff" not visible to the caller. in general those kinds of functions are harder to test, harder to understand, and harder to maintain, and should be considered an exceptional case to use only when you've thought about all the tradeoffs
loop/recur will not grow and shrink the stack, whereas a recursive function will. The default stack depth is small enough that you will often cause an exception to be thrown due to stack exhaustion in a few thousand depth recursive calls. You can configure it higher, but loop/recur will avoid that completely
I came up with this, but it's pretty ugly, so not really an improvement:
(require '[clojure.core.match :refer [match]])
(defn tree-fold
"omitting doc string...."
[f z coll]
(letfn [(dwindle-tree [stack]
(loop [stack stack]
(match (nnext stack)
([[i b1] [_ b2] & tail] :guard (fn [[[i _] [j _]]] (= i j)))
(recur (cons [(inc i) (f b2 b1)] tail))
:else stack)))]
(let [stack (reduce (fn [stack ob]
(dwindle-tree (cons [1 ob] stack)))
(list [1 z])
coll)]
;; stack is guaranteed to have at least one element
(reduce (fn [a1 a2] (f a2 a1))
(map second stack)))))
(tree-fold + 0 [1 2 3])
I couldn't find out how you can re-use the bindings in the :guard
@alexmiller thank you! Yeah, after I wrote that I realized that that's kind of like global variables. Good to know it's possible but not preferable
🙂
reusing the bindings with :guard
is the important part of guard. right?
Thanks. BTW what is nnext
?
True. I guess you could do it like this. But this isn't buying you much over let
. Maybe just accept that Clojure is longer than Scala here ;)
(dwindle-tree
[stack]
(loop [stack stack]
(match (nnext stack)
([[i b1] [j b2] & tail])
(if (= i j)
(recur (cons [(inc i) (f b2 b1)] tail))
stack)
:else stack)))
same as (next (next ..))
is it necessary, the Scala code matches stack rather than matching (next (next ...))
(empty? (rest (rest stack)))
will return the same truthyness as (nnext stack)
Or I could take it as a challenge to implement a pattern matching which sane guards ? Grins. Don't complain unless you're willing to submit a fix.
Yes. core.match is extensible btw, but I'm not sure if it is possible to "forward" bindings into a predicate... I think it should be, but not sure.
of course I can reduce one line by using tail recursion rather than loop/reduce
I know (for my algorithm) that the maximum recursion is log(length(coll))
I can put the operands of reduce
on a single line if I make reduce-1
a local function. that trades two lines for two lines.
(defn tree-fold
"Like the 3-arg version of reduce except that does not compute from left-to-right
but rather effectingly by grouping in concentric groups of two. e.g.
(+ (+ (+ 1 2) (+ 3 4)) (+ (+ 5 6) (+ 7 8))) ...
The remaining (right-most) elements are partitioned into 2's as much as possible.
Intermediate values become unreferenced as quickly as possible, allowing them to
be GCed"
[f z coll]
(letfn [(dwindle-tree [stack]
(if (empty? (rest (rest stack)))
stack
(let [[[i b1] [j b2] & tail] stack]
(if (= i j)
(dwindle-tree (cons [(inc i) (f b2 b1)] tail))
stack))))
(reduce-1 [stack ob]
(dwindle-tree (cons [1 ob] stack)))]
(let [stack (reduce reduce-1 (list [1 z]) coll)]
;; stack is guaranteed to have at least one element
;; and have length <= log_2(coll)+1
(reduce (fn [a1 a2] (f a2 a1))
(map second stack)))))
I haven't looked at the specifics of this code, but people have often made their custom variants of cond
macros that do combinations of conditional tests and bindings that are specific to each branch. Not sure if that helps with code size / expressivity here, but it is a reasonably common desire that isn't part of Clojure core library (except if-let
and when-let
, and also somewhat cond->
and cond->>)
yes. in my case small-code-size relates to putting a code snippet on a beamer (powerpoint) slide to display to non-closure experts.
I'm giving a talk in a few weeks to a group of what I believe to be data scientists, about use cases for non-standard fold/reduce patterns.
i.e., why a person might be dissatisfied with the default fold implementation in your language of choice, and what you can do about it.
the original paper is written for a scala crowd, but I'd like to give the same talk to a non-scale crowd.
super-optimized-scala can look like line-noise.
as per my original post https://clojurians.slack.com/archives/C053AK3F9/p1615129053470300
I'm trying to import a local Clj namespace from the same project, different file.
I type: (:use 'my-package-name.my-namespace) and get nil. Then I try to call something from that namespace: (some-func 0.) and get: Unable to resolve symbol in this context What am I doing wrong?
@jimka.issy Can you give more context? Where are you typing this expression? In a REPL outside of an ns
form?
Then you should use (use ....)
, not the keyword
(:use 'my-package-name.my-namespace)
returns nil
because it tries to look up the keyword in the symbol
In a repl outside of a ns form - yes
(use 'my-package-name.my-namespace)
@borkdude when you say it returns nil because it tries to look up the keyword, does that mean it succeeded or failed
it doesn't do anything namespace related. the expression evaluates like (get 'my-namespace :foo)
e.g. (:foo {:foo 1})
returns 1
, because the keyword looks itself up in its argument
but (:foo "dude")
returns nil
because there is no :foo
key in a string
you are just hitting a confusing case of this while trying to use use
My problem is that when I type my-package-name, the repl fills in .core. I can type in .my-namespace manually, but then the repl doesn't let me call anything from the namespace I just imported. Even after I type my-namespace manually, the repl won't let me call anything from that namespace
You have to load code in order to use it, if you haven't loaded the file that defines a namespace, then the namespace doesn't exist and you can't use it
@hiredman Oh. Okay!
It isn't like java where you can use a long fully qualified named anywhere regardless of if you import a class or not
require and use both also load the code
@hiredman is there a way to load a file that implies local project? for instance, tells repl to look in src/my-project/.../someFile.clj without typing src/my-project
Namespace names implicitly map to resource names relative to classpath roots
So when you require the namespace foo.bar, clojure looks for a foo/bar.clj resource
Where is you aren't familiar with java resources, you can for now just think of resources as a file
So generally you will use some tool to launch your repl, which puts your src directory on the classpath, so when you require foo.bar, src/foo/bar.clj will be loaded
On the topic of namespaces. I just worked through a problem and I’d like to confirm my understanding.
1. I’m connecting to a remote REPL
2. my main (ns …)
declaration requires another namespace in a deeper directory
3. that file doesn’t exist on the remote REPL because I’m developing and organizing on my local machine
4. I get a FileNotFoundException because my other namespace files aren’t on the remote host (they’re local)
5. as long as I go to the buffers of the namespaces I need and load the files, everything works
My understanding is that if the namespace is already loaded into the remote REPL, it won’t look for the file on the remote host.
Am I understanding this correctly?
sorry if I’m relying on information in my own head for this to make sense. Here’s an example:
I have the following files on my local host where I’m editing the code:
• myapp/src/core.clj
• myapp/src/otherstuff/thing.clj
Those files aren’t on the remote host where the REPL is running.
In myapp/src/core.clj
, I have the following line:
(ns core (:require [otherstuff.thing] :as thing])
When I eval that last line I get the exception as I would expect since the file isn’t there on the remote host. If open the local file myapp/src/otherstuff/thing.clj
and load the buffer into the remote REPL, the problem goes away.
Whenever you do a require
or use
on a namespace in Clojure, it remembers this in a *loaded-libs*
internal variable (which you can examine if you are curious to see what it contains). Future require
/`use` (or their equivalents inside of an ns
form) will avoid reloading a namespace that *loaded-libs*
says has been loaded already.
Even if you are not trying to do any dynamic development at the REPL, this is a critical time-saver, because you don't want a namespace required from 20 other namespaces to be loaded 20 times.
ok, just confirming that I got it. If it’s loaded, it goes in *loaded-libs*
and won’t be looked for to load anymore when you require it in a (ns …)
command. So it doesn’t have to exist remotely if you loaded it locally.