Fork me on GitHub
#beginners
<
2018-05-09
>
mooreisenough01:05:52

@slack1899 I use sdkman for my various JVM needs http://sdkman.io/index.html

hawari.rahman1707:05:42

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).

soulflyer07:05:59

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

hawari.rahman1707:05:04

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

hawari.rahman1707:05:26

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

hawari.rahman1707:05:40

It works if I were to put it inside the ns

hawari.rahman1708:05:50

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.

soulflyer08:05:24

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

hawari.rahman1708:05:39

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.

soulflyer09:05:27

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.

denisgrebennicov09: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?

joelsanchez10:05:16

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

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

joelsanchez10:05:31

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

denisgrebennicov10:05:14

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

denisgrebennicov10:05:45

My little example:

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

(eval (codegen (read-line)))

joelsanchez10:05:53

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

joelsanchez10:05:28

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

denisgrebennicov10:05:34

exactly. The input is generated at runtime

joelsanchez10:05:59

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

denisgrebennicov10: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

joelsanchez10:05:41

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

joelv12:05:29

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

hello25413:05:30

Is 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]

joelsanchez13:05:43

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

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

alexmiller13:05:06

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

alexmiller13:05:04

is related ticket

hello25413:05:21

Thanks @alexmiller. Added a vote for it 👍

hello25413:05:55

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

chris56813:05:51

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

bmaddy13:05:12

@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.

dpsutton13:05:55

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

dpsutton13:05:44

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

alexmiller13: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.

dpsutton13:05:12

oh my mistake. thank you

alexmiller13: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

alexmiller13:05:08

@hello254 you can supply your own comparator regardless

alexmiller13:05:37

using sorted-set-by

hello25414:05:19

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

troglotit14:05:15

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"

joelv14:05:30

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

alexmiller14:05:42

that suggestion not make sense to me

alexmiller14: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

alexmiller14:05:33

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

wushee15:05:35

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?

wushee15:05:19

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.

dadair15:05:22

@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

denisgrebennicov16:05:50

How can I call .indexOf while passing also starting index

denisgrebennicov16:05:34

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)

a.hrytsaienko16:05:09

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

nikoszp17:05:39

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

chris56816:05:58

@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

a.hrytsaienko16:05:04

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

chris56816:05:07

of course ymmv, but i’d highly recommend it

lilactown17:05:44

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

nikoszp19:05:11

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.

manutter5119:05:30

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.

nikoszp19:05:38

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

sveri19:05:59

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