This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-05-09
Channels
- # aws (3)
- # beginners (61)
- # boot (1)
- # cider (80)
- # clara (5)
- # cljs-dev (5)
- # cljsrn (19)
- # clojure (40)
- # clojure-dev (29)
- # clojure-dusseldorf (2)
- # clojure-greece (21)
- # clojure-italy (16)
- # clojure-russia (7)
- # clojure-spec (31)
- # clojure-uk (70)
- # clojurescript (31)
- # cursive (21)
- # data-science (5)
- # emacs (7)
- # fulcro (23)
- # graphql (4)
- # java (18)
- # jobs (1)
- # jobs-discuss (25)
- # juxt (14)
- # nrepl (2)
- # off-topic (18)
- # om-next (2)
- # onyx (3)
- # re-frame (16)
- # reagent (50)
- # ring-swagger (5)
- # shadow-cljs (70)
- # specter (5)
- # sql (24)
- # tools-deps (16)
- # vim (41)
@slack1899 I use sdkman for my various JVM needs http://sdkman.io/index.html
Can I use require
outside of ns
? I tend to test my code within the same namespace, so I want to have like:
(comment
(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 '[clojure.zip :as zip])
, worked fine. Does mock maybe need to be added as a dependancy first?
Where do you did (require '[clojure.zip :as zip])
@soulflyer? Is it in the cider repl buffer or in the clj file?
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.
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
copy. So I create a simple function, which uses quotations for codegen, then I simply call eval on it, right?
My little example:
(defn codegen
[code]
(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
yep, defn with syntax-quote and eval afterwards should do it
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-findIs there a good reason why we can compare vectors but not lists? This works:
user> (compare [1 2] [3 4 5])
-1
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 [])
true
user=> (instance? Comparable '())
false
this is intentional but I’m not sure I recall the reason why
is related ticket
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))
@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.
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
@hello254 you can supply your own comparator regardless
using sorted-set-by
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"
@joelv have a look at https://clojuredocs.org/clojure.core/proxy
that suggestion not make sense to me
in this case s is an alias and you can resolve it in the context of the current namespace which stores that mapping
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
:nrepl-middleware
[cider.nrepl/wrap-pprint]}
: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 https://clojure.org/guides/deps_and_cli
E.g.
(.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! https://pragprog.com/book/roclojure/getting-clojure
I haven't read that one but another good intermediate Clojure book I'm finding very useful is https://pragprog.com/book/vmclojeco/clojure-applied
@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
oh even brave and true?? Great! Thanks for advice!
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.