Fork me on GitHub
#clojure-uk
<
2016-08-26
>
korny08:08:54

Does anyone know of an easy way to use a String as a Reader, without resorting to Java?

korny08:08:01

(and as a Writer for that matter)

korny08:08:22

I’m writing test code and it just seems a bit ugly to have to manually create StringWriter and StringReader instances.

korny08:08:26

I could use with-out-str and send output to *out* - but I’m not keen on that approach, it’s too easy for other stuff to go to out by accident

reborg09:08:40

(with-open [s-in ( (.getBytes “string”))]) would do as an InputStrem if that is not a problem korny

korny09:08:18

Just to clarify, my test looks like:

(let [in (StringReader. simple-sample)
        out (StringWriter.)]
    (subject/convert-yaml-to-csv in out)
    out)
=> … expected result …

korny09:08:42

So it’s not that complex. Just hoping for something more clojure-y than explicitly using StringReader and StringWriter

korny09:08:43

(really, my convert-yaml-to-csv should take a sequence of strings and return a sequence of strings, which would be more cleanly testable, and then wrap that in a function that uses readers and writers)

glenjamin09:08:33

wrapping that pattern up in a helper function is probably as nice as you’re likely to get

korny09:08:34

yeah - I could clone with-out-str - it’s fairly simple:

(defmacro with-out-str
  "Evaluates exprs in a context in which *out* is bound to a fresh
  StringWriter.  Returns the string created by any nested printing
  calls."
  {:added "1.0"}
  [& body]
  `(let [s# (new java.io.StringWriter)]
     (binding [*out* s#]
       ~@body
       (str s#))))

practicalli-johnny10:08:52

I love the smell of a macro in the morning....

korny10:08:24

Yo dawg … I heard you like macros

korny10:08:50

Someone should make a meme with Aphyr’s face and a “Yo dawg - I heard you like macros…” byline.

thomas14:08:58

Clojure O’Clock….

otfrom16:08:10

at the speakers or at the audience?

otfrom16:08:13

that could be fun

tcoupland16:08:53

i was looking forward to the talks, but this has just gone and trumped them

otfrom16:08:08

tcoupland: I think this is during the talks

tcoupland16:08:24

taking 🍅 throwing a bit too far perhaps

otfrom16:08:54

tcoupland: guns would be too far

otfrom16:08:04

I think axes just add a bit of frisson

korny16:08:24

What would Hans Reiser do?

xlevus16:08:37

kill his wife

xlevus16:08:43

make a filesystem

xlevus16:08:28

and news to me: Be your own lawyer

korny17:08:38

I didn’t know that. I guess it’s hard to find lawyers when you (allegedly) killed your wife and hid the body

malcolmsparks17:08:20

We're trying to do the ax throwing and yukes in different areas.