Fork me on GitHub

@slack1899 I use sdkman for my various JVM needs

👍 4

Can I use require outside of ns? I tend to test my code within the same namespace, so I want to have like:

  (require '[ring.mock.request :as mock])
  (let [handler (init-handler "config.json")]
    (handler (mock/request :get "/path/123"))))
But apparently this doesn't work because I tried to eval it with cider and it complains that it's unable to resolve the alias mock (no such namespace).


I just did (require '[ :as zip]), worked fine. Does mock maybe need to be added as a dependancy first?


Where do you did (require '[ :as zip]) @soulflyer? Is it in the cider repl buffer or in the clj file?


I've added ring.mock.request as a dependency, but only to dev profile


It works if I were to put it inside the ns


Well, in my case I need it to be declared in my source file, and I would prefer if I don't include it in ns.


I have it in the dependancies in my project.clj file, but I called the require from the repl


Yeah, I used to do "tryouts" in the repl buffer. But I felt that it gets quite tedious to retype it (or copy) it whenever I restarted the repl, that's why I want to write it alongside my source file.


You could maybe take advantage of cider-repl-history if you don't want to add it to your ns. But I don't see why just having the require in your clj file doesn't work. I think it should.

Denis G09:05:56

Does anybody know If I can perform some code-generation using clojure with the help of macros? Or macros are compile time only? As a little exercise I wanted to write a BrainFuck interpreter using clojure. Instead of evaluating it straight away, I was thinking if I can do some codegen first, meaning my process function creates a function which evaluates the brainfuck expression and then I can simple evaluate it. While having codegen phase, I was thinking it will be easier to handle loops. Does anybody know how should I proceed in this case? Any ideas? Are macros good in this case?


the "code generation utilities" commonly used in macros can be used outside macros

(defn stuff [v arr]
  `(hey yes ~v ~@arr))
=> #'user/stuff
(stuff 3 [4 5])
=> (user/hey user/yes 3 4 5)


the thing with macros is that their arguments are not evaluated and their result is, but that's it, they are not very special

Denis G10:05:14

copy. So I create a simple function, which uses quotations for codegen, then I simply call eval on it, right?

Denis G10:05:45

My little example:

(defn codegen
  (if (= "+" (nth code 0))
    '(println "it's a +")
    '(println "it's smth else")))

(eval (codegen (read-line)))


if you are going to eval afterwards, might as well use a macro


...sorry, yes, go with the defn, your code is coming from read-line

Denis G10:05:34

exactly. The input is generated at runtime


yep, defn with syntax-quote and eval afterwards should do it

Denis G10:05:10

But do you think it is a good approach for interpreting? I was thinking that it will make easier to loop, since I can generate a loop construct as code, without jumping somehow to the position in code, where the loop should begin at the end of the loop. It will be handled automatically by lisp/clojure-code


I don't know. it seems a little complex to do it this way. I think a simple state machine would be better for brainfuck. or doing code generation but just doing a 1-to-1 transformation, and then defining the functions


Does anyone can tell how to mock a java function in clojure for example

(defn couchdb-find [client doc-id]
  (.find client doc-id))
I would like to mock the client.find(doc-id) function so that I can have unit-test for couchdb-find


Is there a good reason why we can compare vectors but not lists? This works:

user> (compare [1 2] [3 4 5])
but this doesn’t:
user> (compare '(1 2) '(3 4 5))
ClassCastException   [trace missing]


simply put, they don't implement Comparable. the question would be why though...

user=> (instance? Comparable [])
user=> (instance? Comparable '())

Alex Miller (Clojure team)13:05:06

this is intentional but I’m not sure I recall the reason why


Thanks @alexmiller. Added a vote for it :thumbsup:


Does that mean there’s no way to use lists in a sorted-set, short of converting them to vectors?


is there a preferred testing framework for clojure? now that i’m starting to build non-trivial programs i’d like to include testing in my workflow


@chris568 Honestly, the built in clojure.test works great. I believe that's what most people use. If you're looking for generative testing, look into clojure.spec.


one problem is that (compare (range) (range)) would just be an infinite loop. but then again so is (count (range)) so maybe this is a garbage in garbage out situation


but the short-circuit in that patch first checked the count so it wouldn't short circuit (compare (range) '(1 2))

Alex Miller (Clojure team)13:05:58

@dpsutton (range) does not return a list, it returns a seq so I don’t think that’s relevant here. Lists are finite concrete data structures.


oh my mistake. thank you

Alex Miller (Clojure team)13:05:04

usually if something is “vector but not list” the reason is requiring indexing but that seems like a weak reason to me in this case

Alex Miller (Clojure team)13:05:08

@hello254 you can supply your own comparator regardless


Thanks @alexmiller. I was actually trying to use sorted-set-by when I came across the compare weirdness


is there a way to get namespace of imported namespace? e.g:

(:require [clojure.spec.alpha :as s])
(*namespace* s) <-- which returns "clojure.spec.alpha"


@schmee cool i'll give a shot thanks!!!

Alex Miller (Clojure team)14:05:42

that suggestion not make sense to me

Alex Miller (Clojure team)14:05:05

in this case s is an alias and you can resolve it in the context of the current namespace which stores that mapping

Alex Miller (Clojure team)14:05:33

(name (ns-name (get (ns-aliases *ns*) 's)))

👍 4

question regarding leiningen: I have the following setup to make the repl work in vim:

; spyscope and redl are requirements for 
{:user { :dependencies [[spyscope "0.1.6"]
                       [redl "0.2.4"]
                       [cljfmt "0.5.1"]
                       [cider/cider-nrepl "0.17.0"]]
        :repl-options {:port 4500
        :injections [(require 'spyscope.core)
                     (require '[redl complete core])]}}
it works beautifully, but i notice that i can't run lein repl in any path, only in those that are within a leiningen project -- but the leiningen docs claim that i can run lein repl anywhere. is that a consequence of my profiles.clj or do i misunderstand something?


for example, i have a scratchpad folder for various languages where i throw in code snippets, i don't really want to create a leiningen project for that, i would prefer just having a bunch of .clj files -- if i run lein repl i get back the error IllegalArgumentException Cannot open <nil> as a Reader.


@wushee can’t answer the lein issue, but for your scratchpad folder the clojure CLI tool may be better for your needs, see Running a REPL and Using Libraries here

Denis G16:05:50

How can I call .indexOf while passing also starting index

Denis G16:05:34


(.indexOf [\a \2 \a] \a) => 0
(.indexOf [\a \2 \a] \a 1) => no matching method found
fixed while using: (.indexOf (apply str [\a \2 \a]) “a” 1)


Did somebody buy this book ? Is it good? I have ~ 4 month exp with clojure/clojurescript. And dont know is it worth to buy? Thanks a lot!


I haven't read that one but another good intermediate Clojure book I'm finding very useful is


@a.hrytsaienko I love it! I’ve been using it for the past few months, it has helped me grok clojure in a way that I couldn’t with brave and true

👍 4

oh even brave and true?? Great! Thanks for advice!


of course ymmv, but i’d highly recommend it


is there a way to set a timeout for a cljs async test?


I'm trying to add ClojureScript to an existing Luminus project using cljsbuild. However, it seems that the contents of my core.cljs don't get compiled into app.js, even though it's on the right path (I think). This is the error message I'm getting in the browser Error: goog.require could not find: (str project_ns _DOUBLEQUOTE_.app_DOUBLEQUOTE_) which I'm having a hard time making sense of.


Do you have a Luminus project elsewhere that has the CLJS setup for project.clj? If not, it might be worthwhile to do a lein new luminus example +cljs in a separate directory just so you can compare the 2 project.clj files.


Thank you! I just managed to figure it out. Typo in one of the paths after all... 😅


would be interesting to add stuff like plausability checks to eastwood or any other linting tool. For instance it could check if the src paths exist, etc