Fork me on GitHub
#clojure
<
2016-10-03
>
dviramontes01:10:17

any idea how i can mock a call to (System/getProperty …) in a clojure test: tried:

(deftest test-api
  (testing "get-flag correct when mac os x"
    (with-redefs-fn {#'System/getProperty (fn [_] "Mac O X")}
      #(is (= (api/get-flag) "-x")))))
errors with: Error refreshing environment: java.lang.RuntimeException: Unable to resolve var: System/getProperty in this context, compiling:(…)

dviramontes01:10:18

api/get-flag looks like

(defn get-flag
  "depending on OS"
  []
  (let [os (System/getProperty "os.name")]
    (case os
      "Linux"    "-w"
      "Mac OS X" "-t"
      :default   "-w")))

bfabry01:10:39

@dviramontes with-redefs etc operate on vars, System.getProperty is a java class with a static method. afaik there's no way to runtime stub it out. I would hide System.getProperty behind another clojure function, or a component, which can be stubbed out

dviramontes01:10:25

@bfaber umm ok, that makes sense. I’ll wrap it in a function and give that a shot, thanks!!!

dviramontes02:10:19

@bfaber that totally worked!! thanks again!!

borkdude09:10:00

Is it of any benefit to use java.jdbc/insert-multi! vs insert! for performance reasons, when you need the returned keys? It doesn’t seem so, because it does multiple inserts anyway

metametadata09:10:23

@dviramontes you could also just set the flag 🙂 in pseudocode:

deftest:
try 
  set flag
  test your function
finally
  set flag to original value

pupeno10:10:37

How do I check the version of a dependency from within my Clojure program?

josesanch10:10:50

I'm not able to use a dynamic number of fields in s/keys

josesanch10:10:21

I'm receiving (in cljs) --- Don't know how to create ISeq from: clojure.lang.Symbol

kurt-yagram10:10:50

I just can't figure out how to do this one properly: transform (map)

{"a.b.c" 1
 "a.b.d" 2
 "e.f" 3}
to
{"a" {"b" {"c" 1
           "d" 2}}
 "e" {"f" 3}}

misha11:10:30

@kurt-yagram

(defn nested [m]
  (let [f (fn [res [k v]]
            (assoc-in res (clojure.string/split k \.) v))]
    (reduce f {} m)))

(nested {"a.b.c" 1 "a.b.d" 2 "e.f" 3})
=> {"e" {"f" 3}, "a" {"b" {"d" 2, "c" 1}}}

misha11:10:31

unless there is some magic clojure.core function for similar thing

kurt-yagram11:10:34

ouch, that's nice... and definitaly shorter than what I was doing

kurt-yagram11:10:02

never thought on using reduce...

misha11:10:51

I tried map assoc-in merge first, but it did overwrite nested nodes

kurt-yagram11:10:17

yeah, I was at merge-with merge and stuff, didn't like it

kurt-yagram11:10:24

thanks, by the way!

asolovyov12:10:24

I have a weird problem with transit-clj: when writing data from time to time it died with an exception Not supported: java.lang.String, but today it started to happen really often. Any ideas what the problem is? I use custom write handlers (which handle org.joda.DateTime), but it doesn't seem to have any effect.

crankyadmin12:10:03

@asolovyov post full exception.

misha12:10:31

ava.lang.Exception: Not supported: class org.joda.time.DateTime

asolovyov12:10:45

yeah, it's one of the exceptions

asolovyov12:10:51

I was just looking at the odd one

asolovyov12:10:54

most of them are about string

rvanlaar14:10:56

I have a problem with nrepl and cider, for which I can't find an answer with google. I can't jump to the source of a something, I get "No source location"/ I run leiningen and my nrepl on a vagrant virtual machine. How can I fix this?

dominicm14:10:20

@rvanlaar The problem is that the file doesn't exist for you on your local machine, in a way that makes sense to the nrepl.

dominicm14:10:32

Not sure how to fix it (without throwing away the vm)

rvanlaar14:10:06

As in, not use a VM?

rvanlaar14:10:53

Weird, so a networked repl is only sometimes networked.

dominicm14:10:50

I'm not sure I understand. nrepl is always on the network - it's just when you run it in the same host, cider-nrepl can enable additional features like find-source

dominicm14:10:32

You might be able to get round it if the files were in the same location on guest and host OS. But that's a long shot really.

rvanlaar14:10:17

yeah, but that's not what I want, and it's not documented 😞

rvanlaar14:10:07

I've got it to work, for now, by developing locally

dominicm14:10:27

@rvanlaar the way it works is by searching the nrepl classpath. If that's on another machine, it won't be able to find the relevant files. Not sure on a proper fix for this really.

rvanlaar14:10:33

And it passes the complete filepath to emacs I guess?

dominicm14:10:53

emacs has to do some post processing, but eventually, yeah.

rvanlaar14:10:17

@dominicm Thanks for your help

dominicm14:10:30

Sorry I couldn't be more helpful.

bja15:10:50

is there an off-the-shelf memoize (maybe even backed by core.cache/core.memoize) that can ignore the first N args of my function for purposes of testing whether or not the invocations are the same?

alexmiller16:10:28

not to my knowledge

dpsutton16:10:24

@rvanlaar ask around in #cider as well. its actually pretty active and the core maintainers are often responsive

novakboskov16:10:35

How to realize a lazy sequence? At the time when it is unknown what it actually is. I know that I can do (into [] (lazy-seq [1])) for every collection but the problem is that I don't know what is the real type of a sequence.

bja16:10:29

a lazy sequence doesn't have a real type except lazy sequence

bja16:10:30

if you're turning it into a collection, you have to pick a type

bja16:10:29

there's clojure.algo.generic (although not in core) can maintain the collection type while performing operations like map/filter

alexmiller16:10:32

I would recommend using transducers/`into` over that (or mapv / filterv)

bja16:10:17

separately, is clojure.algo.generic considered deprecated now that we have transducers?

Shantanu Kumar16:10:35

@pupeno On finding dependency versions — I wrote a fn to read project.clj from the classpath and then apply regex to extract the version from a pattern. No clean alternative unless build tools put in project info as an EDN file in the JAR. Might be worth pinging on #leiningen and #boot channels.

tokenshift21:10:03

Anybody using clojure.spec?

bfabry21:10:46

@tokenshift a lot of people seem to be playing with it at least, and it seems like @seancorfield is using it for reals

kidpollo21:10:50

#clojure-spec

bfabry21:10:55

also yes that

puzzler21:10:17

with-out-str does not seem to be capturing messages printed to standard out by a Java method. Is this to be expected? And if so, is there a workaround to capture output made by a Java call?

bfabry21:10:49

with-out-str redefines the dynamic var out, it doesn't redirect the java output stream, so it's expected

bfabry21:10:39

this SO seems to have reasonable instructions for setting System.out http://stackoverflow.com/questions/8708342/redirect-console-output-to-string-in-java

hlolli21:10:52

(string/replace
 "replace (me)"
 (re-pattern
  (format "(\\s|\\t|\\(|\\[)(%s)(\\s+|\\t+|\\)|\\])" "me"))
 (str "$1" "here"))
This regex returns replace (here but I looking to get replace (here) I thought $1 would ensure me to target only whats inside the second paren group?

radon22:10:55

@hlolli:

(str/replace "replace (me)" #"(\s|\t|\(|\[)(me)(\s+|\t+|\)|\])" "$1here$3”)

radon22:10:18

For pattern / string, $1, $2, etc. in the replacement string are
   substituted with the string that matched the corresponding
   parenthesized group in the pattern.

radon22:10:55

You are replacing everything that matches.

radon22:10:02

Since your regex matches the entire string, the entire string is replaced.

radon22:10:18

If you want to keep part of it, you have to substitute it back in using $1, $3.

hlolli22:10:42

ah, ok. ahh, I misunderstood substitution completly then. Thanks @radon 🙂

hlolli22:10:20

All the years of programming and not needing regexp is embarrasing for my regex knowledge.

radon22:10:43

If you want to replace me with here, but only if it appears within the context you provided in your regex, you will need positive lookahead/lookbehind—not sure if Clojure (i.e. Java) regex supports this.

radon22:10:50

Best solution may be what I suggested.

hlolli22:10:46

yes and it does the trick. they say perl is the rolls royce of regexp, wouldn't know.

bfabry22:10:46

java regex does support lookahead/lookbehind

bfabry22:10:42

that said, using those features in a regex is basically the same as writing a big comment that says "HAHA, NOW YOU HAVE TO LOOK AT A REGEX MANUAL" above the line

mathiasx23:10:13

Anyone know off-hand if there’s a way to insert a string that I want to treat as raw HTML, inside Hiccup datastructures? I need one of those gross <!--[if IE]> statements in a page.

seancorfield23:10:24

@mathiasx Yes, you can use hiccup.util/raw-string for that I believe.

mathiasx23:10:46

Thank you, I’ll give that a shot.

mathiasx23:10:12

Either I missed it in the docs or it didn’t seem very clear — if so, I may send a docs PR to make sure others find it 🙂

seancorfield23:10:20

Or maybe escape-html depending on exactly what you need...

mathiasx23:10:40

Since it is script tags and such that conditionally get added to a page, it’ll need to stay unescaped. Standard IE polyfill junk 😞

seancorfield23:10:18

Hmm, hiccup.core/h exists as an alias for hiccup.core/escape-html — TIL.

seancorfield23:10:36

Yeah, raw-string is probably what you need for that.

mathiasx23:10:06

Ah, that’s why. raw-string doesn’t appear to be in Hiccup 1.0.5 docs. Edit: I’ll go read the code, just to be sure.