Fork me on GitHub
#beginners
<
2019-08-07
>
GC00:08:45

Hi, I am not very clear, why liberator.core/defresoure is used for??

GC00:08:11

Plus, do I need to them too alongside functions?

GC00:08:09

my second question is, can we call functions inside liberator?

noisesmith00:08:37

defresource is for serving content out of a jar that started as a file in your repo before building

noisesmith00:08:15

so if you want a static file that isn't represented by code or a string in a source file, and want to be able to serve it directly from the jar without unzipping or whatever

noisesmith00:08:22

commonly used for index.html

noisesmith00:08:58

liberator is a collection of http libs, you typically call whatever functions you need in order to serve a request from inside your handler function for an individual route / method

noisesmith00:08:36

correcting the above: that liberator calls a "resource" isn't what we'd usually call a resource - it's a way ot describe an endpoint (and can include arbitrary function calls)

GC00:08:16

so, they are just defined for end points ?

noisesmith00:08:21

I'm sad they decided to use the word "resource" for this, as it already has a meaning in the clojure http handling world :/

noisesmith00:08:39

yeah, they define endpoints that can be called if a certian route is hit

noisesmith00:08:41

@gagan.chohan this intro shows using an arbitrary function for :handle-ok (the non-error case) http://clojure-liberator.github.io/liberator/tutorial/getting-started.html

noisesmith00:08:57

(defroutes app
  (ANY "/foo" [] (resource :available-media-types ["text/html"]
                           :handle-ok (fn [ctx]
                                        (format "<html>It's %d milliseconds since the beginning of the epoch."
                                                (System/currentTimeMillis))))))

noisesmith00:08:06

and of course that function can call other functions

GC00:08:01

cool, Thanks for the detailed explanation.

GC01:08:51

is there a way to check in testing that what functions did function being tested called?

noisesmith01:08:37

not that I know of (it may well exist in a third party lib I haven't used), clojure (and fp generally) tends to lead you toward using example based and property based tests that are focused on input values and return values, and not the control flow of the code executed

GC01:08:02

There’s a function that calls S3 or cache to retrieve data, I want to test both scenarios, in case of S3 and cache, they both call different functions

noisesmith01:08:04

come to think of it, I have seen this done with with-redefs

GC01:08:08

output will be same in both cases

GC01:08:53

so, what do you recommend to test such a function

noisesmith01:08:51

I take back what I said before: the pattern is (let [executed (atom [])] (with-redefs [some-ns/call-s3 (fn [& args] (swap! executed conj {:f :some-ns/call-s3 :args [args]}))] (some-other-fn))

noisesmith01:08:28

after you execute some-other-fn you check the contents of executed (it could contain zero or more entries of course)

noisesmith01:08:42

and you would "redef" every function you want to track / stub

noisesmith01:08:15

I'm personally not a fan of this sort of test, and with-redefs shouldn't be used in a multi-threaded context, but it does work

GC01:08:17

so , is there any preferred approach to test what is being called, S3 or Cache, considering both of them returns same data

noisesmith01:08:47

that's what the above will do (add another redef for other-ns/hit-cache)

noisesmith01:08:16

it replaces the function you want to track with something that records its invocation - which is then data you can make an assertion about

GC01:08:44

okay, I will first look at with-redefs docs

noisesmith01:08:51

@gagan.chohan

user=> (def calls (atom []))
#'user/calls
user=> (with-redefs [clojure.core/println (fn [& args] (swap! calls conj {:f 'println :args args}))] (println :OK))
[{:f println, :args (:OK)}]
user=> @calls
[{:f println, :args (:OK)}]

noisesmith01:08:47

it shouldn't be used with multiple threads in use, because two overlapping with-redefs calls can lead to losing the original function definition

noisesmith01:08:07

so you're committing to single-threaded execution during tests if using this

noisesmith01:08:30

(or very careful usage of threads and redefs)

dpsutton01:08:37

Difficulty in testing is (to me) a sign that you need to refactor. If you can change this to have two (or arbitrarily many) strategies to do whatever you might get more adaptable code and more easily tested code at the same time

Godwin Ko01:08:02

don’t know why this doesn’t work (map #(.autoSizeColumn sheet %) (range 2)) while call it one-by-one (.autoSizeColumn sheet 0) & (.autoSizeColumn sheet 1) works?

beders02:08:31

what error are you getting?

dpsutton02:08:47

try doall or mapv? perhaps laziness is involved?

👍 1
1
Godwin Ko02:08:17

you save my day, thx a lot!!

👍 1
dpsutton03:08:06

be wary of side effects from a map

Crispin04:08:25

does .autoSizeColumn return a new value? If its all just for side effects, use doseq instead.

Crispin04:08:53

or, as you are looping over indexes, have a look at dotimes

byrongibby06:08:54

Hi. After setting JAVA_CMD to C:\Program Files\Java\jdk1.8.0_211\bin\javac.exe running lein install produces

javac: invalid flag: -Dclojure.compile.path=C:\git\xx\mranderson/target/classes
Usage: javac <options> <source files>
use -help for a list of possible options
Does anyone know what I'm doing wrong/how to fix?

byrongibby06:08:27

JAVA_CMD should have been set to C:\Program Files\Java\jdk1.8.0_211\bin\java. 🙂

1
Godwin Ko08:08:37

I try to export a large excel file using docjure, and the jvm keep crashing due to OutOfMemoryError, GC overhead limit exceed. Which jvm options should I alter? Thx in advance 🙇:skin-tone-2:

schmee09:08:55

try -Xmx2G -Xms2G and replace 2G with however much memory you want!

1
Casey08:08:07

often i'm faced with making a serious of changes to a map (as part of some data munging), I generally use the -> thread macro to change update/assoc ops, however it gets awkward when I want to , for example, change/add a key based on the value of another key. is there no function in cloure core for doing this?

Casey08:08:14

i can workaround it by using as-> but .. 😕 i don't see that macro used much in code in the wild vs ->, which makes me think i'm missing something

Crispin10:08:29

I just use destructuring in let's in combination with the threaded assoc/updates or bind to one var (usually it's %) and use get-in's.

Crispin10:08:06

like

(->> collection
     (map #(let [{:keys [one two]} %]
             (-> %
                 (assoc-in [:key :path 1] (get-in % [:other :key :path]))
                 (assoc :two one)
                 (assoc :one two)
                 (update-in [:yet :another] inc)))))

Crispin10:08:42

you can also destructure in the (fn ...) arg list instead of using anonymous inline func

Crispin10:08:15

(->> collection
     (map (fn [{:keys [one two] :as arg}]
            (-> arg
                (assoc-in [:key :path 1] (get-in arg [:other :key :path]))
                (assoc :two one)
                (assoc :one two)
                (update-in [:yet :another] inc)))))

Casey10:08:25

thanks for sharing. using destructuring to bind the values before threading is nice

schmee09:08:20

@ramblurr Clojure core is intentionally limited when it comes to stuff like this, so I recommend either writing your own helper functions or to check out something like https://github.com/nathanmarz/specter/ and see if that might be of use

magthe10:08:20

I have a dev scenario / lein question. Say that I have two Clojure projects, libfoo and svcbar. svcbar has libfoo as one of its dependencies. Now I want to make a change in libfoo and try it out in svcbar. I bump the version i libfoo from 1.1 to 1.2-SNAPSHOT, I test, I build a JAR -- I'm happy. Then I change over to the svcbar dir and change its dependency on libfoo to 1.2-SNAPSHOT, but of course that build fails because that version isn't published. How do I make a locally built version of a library available to lein when building something that depends on it?

Crispin13:08:50

You can lein install it to install it to your local maven repo

Crispin13:08:03

or you can use "checkouts" so you don't even need to install it

Crispin13:08:33

oh. just saw this was answered below... heh.

magthe14:08:49

What is a Maven repo? Is the cache in ~/.m2 actually a full-blown Maven repo?

NoahTheDuke14:08:49

is reduce lazy? It doesn't look like it, but my experiments are proving inconclusive

dpsutton14:08:21

what data points to it being possibly lazy?

NoahTheDuke15:08:50

on second thought, i (naively) assume every iteration tool in clojure is lazy. the language does a good job with being consistent on that front (all of the seq-related tools are lazy), and the reduce docs don't say either way. the impl isn't wrapped in a lazy-seq, which points to it not being lazy

NoahTheDuke14:08:37

my bad programming, i think

alexmiller14:08:43

it's not lazy

NoahTheDuke14:08:21

i had a doseq, and wanted to save the results, so i switched to a for loop wrapped in a doall, but on closer inspection i was seeing some weird behavior, so i thought i'd try using reduce to make sure, and then weird behavior persisted, so the problem looks to be an error in my logic somewhere

NoahTheDuke15:08:45

thanks for the quick answer!

gerred16:08:02

is there a reason at this time not to just openjdk 12?

gerred16:08:07

and prefer openjdk 11

seancorfield16:08:54

8, 11, 14 are the LTS (Long Term Support) versions which is why folks tend to prefer those versions, even in the openjdk camp.

gerred16:08:21

ahh 👍 yeah, ok. for some reason in my head I was thinking 12 was LTS

gerred16:08:48

time to add in the adoptopenjdk cask then. 😄

seancorfield16:08:23

We're stuck on openjdk8 because some pieces of our infrastructure won't run on jdk9+ but I'm testing a lot of stuff on both jdk8 and jdk11 at this point.

alexmiller17:08:46

for Clojure, we're trying to emphasize the LTS versions so we recommend 8 and 11 as primary right now (but the matrix tests with 12 too)

alexmiller17:08:28

we are making no effort to test with 9 or 10 right now (although we did prior to 11 and there are no particular issues with them)