Fork me on GitHub
#clojure
<
2019-04-24
>
p-himik07:04:37

Hello. I'm trying to use org.apache.commons.io.input.ObservableInputStream to make sure that I remove the file that I just streamed. For that I need to extend the abstract class org.apache.commons.io.input.ObservableInputStream$Observer and override some of its methods. I tried using proxy but the overridden methods are not called. Is this because they're not public and have the default visibility instead? My next step is to use gen-class. Is there anything simpler for this task?

p-himik08:04:41

Ah, my Java's a bit rusty - package private methods cannot be overridden from another package at all. And the corresponding bug in Common's JIRA says: "Fixed, will be in 2.7, whenever that comes." Oh well.

p-himik09:04:27

New question - is there any way to use Java files in a Clojure project while keeping the working REPL? I don't want to reload Java files, I just want them to work. Right now I just keep getting ClassNotFoundException.

Ivan Koz09:04:06

do you mean (import)?

p-himik09:04:40

Well yeah. Only the java file is within my project source files.

p-himik09:04:21

It seems that Leiningen allows you to specify :java-source-paths. The thing is, I don't use Leiningen and Boot - I just use clj.

Ivan Koz09:04:59

@p-himik from https://clojuredocs.org/clojure.core/import

;; For cases when 'import' does not do it for you, i.e. "live-coding"
;; You can use the DynamicClassLoader, ClassReader and the Resolver.
;; 

(def dcl (clojure.lang.DynamicClassLoader.))

(defn dynamically-load-class! 
  [class-loader class-name]
  (let [class-reader (clojure.asm.ClassReader. class-name)]
    (when class-reader
      (let [bytes (.-b class-reader)]
        (.defineClass class-loader 
                      class-name 
                      bytes
                      "")))))

(dynamically-load-class! dcl "java.lang.Long")
(dynamically-load-class! dcl 'org.joda.time.DateTime)

;; From that point the dynamically loaded class can be
;; used by the Reflector to invoke constructors, methods and to get fields. 

p-himik09:04:05

But it requires a corresponding .class file, doesn't' it? I'm getting `Execution error (IOException) at clojure.asm.ClassReader/readStream (ClassReader.java:300). Class not found` with it.

Ivan Koz09:04:18

yeah seems like

p-himik09:04:38

Yep, I could use https://github.com/tailrecursion/javastar that it mentions to just slurp that java file. 😄

Ivan Koz09:04:10

thats all i found, tbh i'm interested myself

p-himik09:04:57

Found this: https://www.mail-archive.com/[email protected]/msg103900.html More than half a year old but is probably still true.

clj is not a build tool, only a pure Clojure runner, so without something 
additional to do Java compilation, you can’t use it in a mixed source project.

aspra09:04:23

Any suggestions/reads/libs on how to stub an external RESTAPI service in my tests? So far I am thinking of https://github.com/myfreeweb/clj-http-fake

jeroenvandijk09:04:33

I'm curious too. I think Clojure is missing a library like this https://github.com/lostisland/faraday

aspra11:04:49

the one I mentioned above is the Clojure adaptation of https://github.com/chrisk/fakeweb. Have you heard of that?

jeroenvandijk11:04:46

No I haven't heard of it (but doesn't say much actually)

gklijs21:04:20

Is the rest API has swagger, you could maybe use one of the java generated servers https://github.com/swagger-api/swagger-codegen/wiki/Server-stub-generator-HOWTO to bad clojure is no option.

aspra13:04:45

very good tip @U26FJ5FDM thank you

gklijs14:04:17

I used it to 'mock' a notification server (actually the mock worked better than the .not real version :))

aspra15:04:36

🙂 great, will give it a try. It is a swagger API so should do!

mloughlin12:04:13

what's the best way to modify a macro that expects a literal Seq to evaluate functions/symbols passed to it?

(defmacro old-printy
  [print-this]
  (if (seq print-this)
    `(print ~print-this)))
Passing '[a b c] works, but (def print-these '[a b c])/`(old-printy print-these)` throws exception. So far I have this
(defmacro new-printy 
  [print-this]
  (let [evaluated-print-this (cond 
                               (fn? print-this) `(~print-this) 
                               (seqable? print-this) `(seq ~print-this)
                               (symbol? print-this) `(into [] ~print-this)
                               :else print-this)]
    `(print ~evaluated-print-this)))

mloughlin12:04:17

I forgot the (if (seq evaluated-print-this)) in the second one :face_with_rolling_eyes:

mars0i21:04:12

(defmacro old-printy
  [print-this]
  `(if (seq ~print-this)
     (print ~print-this)))

mars0i21:04:01

Not sure if this handles all of the intended cases, @UHK8B8STX:

(defmacro new-printy
  [print-this]
  `(let [evaluated-print-this#  (cond 
                                  (fn? ~print-this) (~print-this)
                                  (seqable? ~print-this) (seq ~print-this)
                                  (symbol? ~print-this) (into [] ~print-this)
                                  :else ~print-this)]
   (print evaluated-print-this#)))

mars0i21:04:32

I guess Slack's thread function doesn't like wide code.

mloughlin22:04:32

I missed out keyword and probably a few more, but it seems overly laborious to have to cond based on type. I wonder if there's a better method?

Ivan Koz12:04:30

i'm just starting with core async, if i want some transformations on a channel data is that a proper way to do it?

(defn filter
  ([c] (filter (chan) c))
  ([c' c]
   (go (while true 
         (>! c' (<! c)))) ;; logic here
   c'))

Alex Miller (Clojure team)12:04:08

no, channels can take a transducer

Ivan Koz12:04:36

alright ill look into it, is above pattern useful for something?

Alex Miller (Clojure team)12:04:59

(chan 10 (filter odd?))

Alex Miller (Clojure team)12:04:22

that's a channel, buffer size 10, that you can put numbers into and only odds will come out

Alex Miller (Clojure team)12:04:58

also, fyi there's #core-async for more in-depth questions

Ivan Koz12:04:17

yeah, noted its just seems completely silent

Alex Miller (Clojure team)12:04:39

just waiting for you :)

😄 4
pablore13:04:30

How do I organize a deps.edn project with java source files?

pablore13:04:35

I have something like this:

src/
   -  clj/
        - my-namespace/...
   -  java/ 
        - my-namespace/...

pablore13:04:04

But when I import my java classes, clojure is unable to find them

rickmoynihan13:04:34

@pablore: tools.deps is not a build tool, so you can’t do that without compiling those java classes — and for that you need to look into build tools

rickmoynihan13:04:41

or just use leiningen

rickmoynihan13:04:59

which will do that

pablore14:04:04

Ok, migrated to leiningen, but having the same error

jlmr16:04:14

Hi, is there something like https://github.com/metosin/ring-swagger but which does the reverse? So it transforms a swagger spec (or open api spec) into a working mock API endpoint?

kszabo16:04:45

It’s not Clojure, but this is close: https://github.com/yayoc/swagger-to-mock

jlmr06:04:15

Thanks, I’ll take a look at it!

souenzzo16:04:00

There is some "style guide" for project's layout?

CyberSapiens9720:04:50

those of you that use Schema for data spec, there is any way to validate some data against a schema without returning an exception, only true or false ?

CyberSapiens9720:04:33

@jlmr if you find something like that, let me know, i'm using swagger too

noisesmith20:04:57

you can replace s/check with s/valid? oh, schema not spec, - plumatic/schema also has a function that you can use s/check it appears schema and spec us valid vs. check in opposite ways(?)

CyberSapiens9720:04:38

check returns nil if data matches the schema

CyberSapiens9720:04:26

s/validate throws an exception, so i have to handle it in order to not break my entire app. and i don't want to handle it

noisesmith20:04:42

because there's often only one way to be correct, and many ways to be wrong

CyberSapiens9720:04:14

guess i'll have to make a wrapper fn to handle it

CyberSapiens9720:04:36

and make it return true or false

CyberSapiens9720:04:37

or i can use s/check with nil? which i think it's very weird to read

noisesmith20:04:34

@cybersapiens97 (complement s/check) - returns a function which returns true or false in the way you want

👍 4
CyberSapiens9720:04:32

oohh, how smart

Radosław Mejer21:04:15

Hello everyone 👋. I just created an account here so it would be nice to introduce myself 🙂 My name is Radek, I'm from Poland. Not yet a clojure dev yet aiming to become one in some time. Currently working as a PHP/JS dev (w/ +10 years of experience). Proud owner of ErgoDox, VIM maniac. Currently watching season 2 of One Punch Man 😛

48
👋 20
💯 8
8
seancorfield21:04:49

Be sure to check out the wide range of channels here -- you'll find #beginners very helpful for early questions as you're learning the language (folks in there have opted in to helping new folks!). Pretty much every library in common use has a channel here somewhere -- and if you have questions about Slack itself (or this Slack in particular), there's #slack-help

Radosław Mejer21:04:34

awesome, thanks for quick introduction 🙂

jussi09:04:05

Season 2 of One Punch Man is great!

Radosław Mejer10:04:26

@U3BUZ2BPZ IKR ? Awesome stress reliever 🙂

Ivan Koz21:04:47

Poland Strong! :flag-pl: Greetings from Russia! :flag-ru:

👋 8