Fork me on GitHub
#clojure
<
2020-03-22
>
Steiner02:03:08

I want to import a lib called me.raynes.fs with require,and reference it as fs,how do i do

dpsutton02:03:07

this kind of question is perfect for #beginners . can i answer it there?

Steiner02:03:06

Here when i use the lib,I found something wrong with it

Steiner02:03:32

(require '[me.raynes.fs :as fs])
(fs/chdir "/") 
Execution error (IllegalStateException) at me.raynes.fs/chdir (fs.clj:541).
Can't change/establish root binding of: *cwd* with set

Steiner02:03:57

this is source of this function

(defn chdir
  "set!s the value of *cwd* to path. Only works inside of
   with-mutable-cwd"
  [path]
  (set! *cwd* (file path)))

dpsutton02:03:56

the docstring is telling you your problem

dpsutton02:03:10

> only works inside of with-mutable-cwd

Steiner02:03:43

then what should i do

Steiner02:03:04

is that a problem about require??

dpsutton02:03:13

(doc fs/with-mutable-cwd)

dpsutton02:03:35

user=> (fs/with-mutable-cwd (fs/chdir "projects/clojure") (prn fs/*cwd*))
#object[.File 0x7e4d2287 "/home/dan/projects/clojure"]
nil
user=> (prn fs/*cwd*)
#object[.File 0x68c87fc3 "/home/dan"]

pinkfrog03:03:29

I’d like to know, how (.toString some-proxy-instance) dispatches on the proxy instance

pinkfrog03:03:30

given a proxy instance, it has a toString function stored inside its function mapping, and also an inherited toString method from class hierarchiy

pinkfrog03:03:43

So, the generated stub method will first check if a corresponding function exists in the mapping. If not, it then goes on calling its super class.

pinkfrog04:03:16

what’s the use case of proxy when compared to deftype and reify. the only case seems to avoid implementing some interface methods? kinda weird.

dpsutton04:03:07

can you explain what you mean about checking for functions existing?

(.toString (proxy [java.util.Date] []
             (toString [] "bob")))
"bob"

dpsutton04:03:27

here i proxy a java util date and override its toString

dpsutton04:03:10

I couldn't use reify for a class so can't reify a java.util.Date. If i wanted to deftype something I would have to basically reinvent all of the date functions on the new deftype

dpsutton04:03:45

https://clojure.org/reference/datatypes#_reify has some more (official) info contrasting them

pinkfrog04:03:47

why do you have to re-define everything for deftype? You just define the method that overrides some in the superclass that you want to tweak with.

dpsutton04:03:59

how could you use deftype to get a datetime that behaves exactly like a datetime except for the toString like i did above?

pinkfrog05:03:06

@U11BV7MTK aha, I wrongly thought deftype can extend a class.

Steiner06:03:30

When I read out all content of file,I want to seek the point into header of file,how do I handle it in Java??

jumpnbrownweasel14:03:51

If you're asking how to position/seek in a file, take a look at the .RandomAccessFile class.

István Karaszi12:03:29

hi, I was trying to find a way to extend a spec, but I could not find any examples

István Karaszi12:03:59

I would like to extend this spec with other keys: (s/keys :req-un [::name])

István Karaszi13:03:02

I am not sure what you mean

p-himik13:03:19

(s/or :x (s/keys ...) :y (s/keys ...))

István Karaszi13:03:53

but the main problem is that I don’t want to repeat the original spec again

vemv13:03:08

s/merge

👍 8
p-himik13:03:10

Although, the docstring says that it accepts predicates and not specs (talking about s/or).

István Karaszi13:03:50

let me try s/merge

István Karaszi13:03:12

and it is even mentioned in the spec guide

István Karaszi13:03:14

(s/def :animal/dog (s/merge :animal/common
                            (s/keys :req [:dog/tail? :dog/breed])))

Steiner17:03:53

how do i change directory with http://clojure.java.io?? is there a marco like with-open ,suck like with-mutable-cwd,which simply program proccess

Steiner18:03:51

how can i use http://clojure.java.io to read out

potetm18:03:37

I don’t understand what “read out” means

potetm18:03:20

@steiner3044 working dir is a java thing, and there is no chdir equivalent: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4045688

potetm18:03:37

I don’t understand what “read out” means

Steiner18:03:58

It means read data from a file

Steiner18:03:04

in some way

Steiner18:03:48

is there any function in http://clojure.java.io??

potetm18:03:54

there are other ways, but you’re not being incredibly specific

potetm18:03:12

io/reader?

Steiner18:03:28

how can you read and print in repl??

potetm18:03:30

you can look at the implementation of slurp for an example usage

potetm18:03:45

(defn slurp
  "Opens a reader on f and reads all its contents, returning a string.
  See  for a complete list of supported arguments."
  {:added "1.0"
   :tag String}
  ([f & opts]
     (let [opts (normalize-slurp-opts opts)
           sw (.StringWriter.)]
       (with-open [^.Reader r (apply jio/reader f opts)]
         (jio/copy r sw)
         (.toString sw)))))

potetm18:03:05

that’s exactly what you wanna do

potetm18:03:32

make a reader -> copy to a stringwriter or bytearrayinputstream -> make a string

potetm18:03:59

though, if that’s what you want, it’s unclear to me why you would not use slurp

dpsutton18:03:44

@steiner3044 can I once again ask you to ask your questions in #beginners? There are lots of folks who love to help beginners through these kinds of challenges but this isn't the best place for it

Steiner18:03:35

to bother you

dpsutton18:03:52

no bother at all! I'm happy to help and work through problems. But there are thousands of people in this channel who haven't necessarily signed up for that. So to be respectful we can go to the beginners channel and have lots of patient and attentive people help us

paul a19:03:15

in clojure, what's the best way to take [[1]] and get a java object of type byte[][]? for some context, i'm trying to make an object that i can pass as the first arg to the DataBufferByte constructor: https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/image/DataBufferByte.html

paul a19:03:45

it's kind of hard to search for byte[][] and i'm familiar enough with java to know if it has a more searchable name

potetm19:03:40

(to-array (map to-array
               [[1] [2] [3]]))

potetm19:03:48

seems reasonable enough to me

potetm19:03:30

ooh probably have type probs w/ that

seancorfield19:03:55

(into-array (map byte-array [[1]])) does seem to provide a native byte[][]...

✔️ 4
seancorfield19:03:20

user=> (into-array (map byte-array [[1]]))
#object["[[B" 0x74a9c4b0 "[[B@74a9c4b0"]
user=> (byte-array [1])
#object["[B" 0x54f5f647 "[B@54f5f647"]
user=>

paul a19:03:39

that'll do it, thanks 🙂 on that note, maybe i'll add (for anyone searching?) that https://clojars.org/clojure-interop/java.awt doesn't yet provide a definition for the constructor that takes byte[][], but it seems like it would be pretty easy to define.