Fork me on GitHub
#beginners
<
2016-06-07
>
dviramontes01:06:02

cross-post from lein channel --- Hello all, I have this specific use case where i need to run $ lein run with sudo (i know bad idea, but its just for testing). does anyone know what this warning is trying to tell me:

WARNING: You're currently running as root; probably by accident.
Press control-C to abort or Enter to continue as root.
Set LEIN_ROOT to disable this warning.
is LEIN_ROOT something i set on my .bashrc or my .lein config ?

dviramontes01:06:05

@ghadi does it look like: LEIN_ROOT=true ?

ghadi01:06:14

yeah any value will do

ghadi01:06:25

LEIN_ROOT=dviramontes lein repl

victorbjelkholm12:06:39

@dviramontes: you can also set it just one time if you add it before lein run LEIN_ROOT=true lein run

victorbjelkholm12:06:51

that way you don’t have to change any configs just to try something out

seancorfield15:06:20

How about (into [:ul [:li :first-item]] list-items)?

naomarik15:06:17

yeah i'm aware that works, but wondering if there's a way to do it the other way... perhaps writing a macro? I'm just being a bit stubborn about it

senya2216:06:07

(let [t []](dotimes [i 10] (conj t i))t)
=> []

senya2216:06:18

why doesn't this populate a vector?

senya2216:06:38

whereas the following does:

senya2216:06:51

(let [t (transient [])]
  (dotimes [i 10]
    (conj! t i))
  (persistent! t))
=> [0 1 2 3 4 5 6 7 8 9]

donaldball17:06:10

Blithely, because t in the former is an immutable vector

donaldball17:06:50

conj takes an immutable data structure and returns a new immutable data structure that contains the given value

donaldball17:06:59

It does not change the immutable data structure argument

senya2217:06:51

so how to display the contents of the t at the end of let?

donaldball17:06:38

You need to accumulate the intermediate t values if you want all the integers from 0 to 9 in it

donaldball17:06:12

dotimes won’t let you do that. It’s used when you want side effects.

donaldball17:06:16

(mapv (fn [i] i) (range 10)) would be one way to do it immutably

senya2217:06:32

I see, thanks

donaldball17:06:44

(into [] (range 10)) might be more idiomatic

donaldball17:06:00

Or simply (range 10) if you don’t need it to be a vector, just a seq

alexmiller17:06:14

note that into will do transients under the hood for you too

senya2217:06:00

Interesting. I just wanted to wrap both of them into a time to do a side-by-side comparison, but apparently it's not as easy as it seems 🙂

donaldball17:06:32

Performance testing in the JVM is something of a dark art

donaldball17:06:48

For quick and dirty analyses though, try the criterium library

senya2217:06:55

took a mental note of that library, thanks!

senya2218:06:24

@alexmiller funny you mentioned it, I am just reading the 2nd chapter of your Clojure Applied where you are explaining it and doing various experimentations with code snippets of my own. Serendipitous...

dmbennett19:06:28

Random question, I’m working my way through Clojure for the Brave and True

dmbennett19:06:46

and I noticed this little difference when repl-ing through the examples

dmbennett19:06:38

why would the space between (inc 199) and (/ 100 (-7 2) cause the solutions to differ by 5000?

dmbennett19:06:01

The author expects 220 as a solution

bronsa19:06:29

there's no way that can happen @dmbennett

bronsa19:06:43

could the 5 be from a previous println?

dmbennett19:06:53

no, I just replicated it again

dmbennett19:06:11

but it requires the paren spacing at the end

bronsa19:06:46

open a new repl from scratch, try putting and removing how many spaces you want, there's no way that can happen

bronsa19:06:34

well, there's your problem

bronsa19:06:41

/100 is not a valid clojure expression

bronsa19:06:49

evaluation gets aborted

bronsa19:06:58

evaluation resumes at (- 7 2) which is 5

bronsa19:06:09

you get 2 other errors because )) is invalid code

bronsa19:06:20

and then you get the result for your next expression which is 220

dmbennett19:06:25

did you miss that last line?

bronsa19:06:32

the repl you're using causes the 5 and 220 to be printed in the same line

dmbennett19:06:34

stille evals to 5220 after fixing the space issue

bronsa19:06:40

no, that's just 5 and 220

dmbennett19:06:07

interesting, it only holds the 5 after it errors from the /100 form and then when eval-ing the next runs (+ (inc 199)(/ 100 (- 7 2))) returns the 5 and 220

bronsa19:06:33

I don't know what repl you're using, but the default clojure REPL doesn't exhibit that behaviour

dmbennett19:06:44

I’m using lein repl

bronsa19:06:46

user=> (+ (inc 199) (/100 5))
RuntimeException Invalid token: /100  clojure.lang.Util.runtimeException (Util.java:221)
5
RuntimeException Unmatched delimiter: )  clojure.lang.Util.runtimeException (Util.java:221)
RuntimeException Unmatched delimiter: )  clojure.lang.Util.runtimeException (Util.java:221)

bronsa19:06:49

¯\(ツ)

dmbennett19:06:53

iDave-MBP:~ dmbennett$ which lein repl
/usr/local/bin/lein
iDave-MBP:~ dmbennett$ lein -version
Leiningen 2.6.1 on Java 1.8.0_25 Java HotSpot(TM) 64-Bit Server VM

bronsa19:06:47

yeah, dunno. might be a known bug with lein/nREPL

dmbennett19:06:59

well thanks for that

dmbennett21:06:16

Question: why would I not be able to make this function: (defn valid-args? [function-name] (:arglists (meta (var function-name)))) the result I get is clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: Unable to resolve var: function-name in this context

dmbennett21:06:21

I wanted to make a short cut for calling (:arglists (meta #’map)) for any function, that would normally respond with ([f] [f coll] [f c1 c2] [f c1 c2 c3] [f c1 c2 c3 & colls]).