Fork me on GitHub
#beginners
<
2023-11-01
>
Fit Opene09:11:38

Hello everyone, I'm currently exploring libgdx with Clojure, and I've encountered a peculiar issue.

(let [batch (SpritBatch.)
        atlas (TextureAtlas. (-> Gdx/files
                                 (.internal "skin.atlas")))
        logo (Texture. (-> Gdx/files
                           (.internal "logo.png")))
        player (.findRegion atlas "player")]
    (doto batch
      (.setProjectionMatrix (.combined camera))
      (.begin)
      (.draw logo 0 0)
      (.end)))
It's causing a bug: No matching method draw found taking 3 args for class com.badlogic.gdx.graphics.g2d.SpriteBatch. The only correct way to call it is:
(.draw logo 0 0
       300 300
       400 600
       0.5 0.5
       90)
(.draw player 0 0
       300 600
       100 100
       0.5 0.5)
Here's the Java documentation:
void    draw​ (TextureRegion region, float x, float y)
void    draw​ (TextureRegion region, float x, float y, float width, float height)
void    draw​ (TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)
void    draw​ (TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, boolean clockwise)
void    draw​ (TextureRegion region, float width, float height, Affine2 transform)
void    draw​ (Texture texture, float [] spriteVertices, int offset, int count)
void    draw​ (Texture texture, float x, float y)
void    draw​ (Texture texture, float x, float y, float width, float height)
void    draw​ (Texture texture, float x, float y, float width, float height, float u, float v, float u2, float v2)
void    draw​ (Texture texture, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY)
void    draw​ (Texture texture, float x, float y, float width, float height, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY)
void    draw​ (Texture texture, float x, float y, int srcX, int srcY, int srcWidth, int srcHeight)
Why isn't polymorphism working, what i missed?

delaguardo09:11:18

Looks like the fix could be this:

(.draw logo (float 0) (float 0))
the method that takes two arguments expects to see floats and getting longs instead. And you see exception like "No matching method" because there is no variant taking longs.

💯 1
Fit Opene09:11:59

Thank you, bro, that totally fixed it!

👍 1
RK C17:11:44

Is there a way for users to define a float literal? say #F 0

Can11:11:41

Hello everyone, my code is working but I don't sure is it nice attempt or not. If needed can you advice me a better attempt please.

(def my-vector [[:customer/supplier] [:customer/client]])


(defn client-permisson-check [company-type-vec]
  (if (= :customer/client (some #{:customer/client} (first (filter #(= (first %) :customer/client) company-type-vec))))
    true
    false
    )
  )

(client-permisson-check my-vector)
;=> true

vanelsas11:11:23

No idea what this is for so that makes it a bit more difficult to improve this code (for example, must you have the my-vec format the way you are showing it here? A simple improvement would be to get rid of the if and the = functions and just return the result of some. If it is nil it is false, if it isn't nil it is true. This code could probably be simplified further if you would change the format of my-vec

Can11:11:25

hello, format of my vec is my datomic queries return.

Ed11:11:32

company-type-vec isn't used by client-permission-check

Can11:11:16

Yes yes sorry fixing, it is my-vector 🙂

Can11:11:16

Fixed now, sorry for confusing.

Ed11:11:42

I think that client-permission-check could be shorter

(let [my-vector              [[:customer/supplier] [:customer/client]]
        client-permisson-check (fn [copmany-type-vec]
                                 (boolean (some #{:customer/client} (mapcat identity my-vector))))]
    (client-permisson-check my-vector))

Ed11:11:32

but often people don't worry about casting things to booleans so you could just do something like

(defn client-permisson-check [copmany-type-vec]
  (some #{:customer/client} (mapcat identity my-vector)))
? (which changes the return type, but would work the same if it was used in an if for example)

Ed11:11:11

or have I misunderstood what you're actually looking for in the list?

Can11:11:53

I just want to check my user has just client right.

Can11:11:57

I thought if I check my returned query has :customer/client attribute or not.

Ed11:11:15

and what does the query return? ... is only the first thing in each vector a permission? or are all the elements of each vector in my-vector permissions?

Can11:11:18

I am not sure that I can explained clearly.

Can11:11:07

it returns empty or permission types like supplier or client etc

Ed11:11:10

so (mapcat identity my-vector) will flatten the list into [:customer/supplier :customer/client]

Can11:11:42

(mapcat identity my-vector) => (:customer/supplier :customer/client)

Ed11:11:56

and (some #{:customer/client} ...) will check that :customer/client is in that list`

Ed11:11:57

> (mapcat identity my-vector) > => (:customer/supplier :customer/client) sorry, yes ... it will be a seq rather than a vector

Can11:11:03

(some #{:customer/client} (mapcat identity my-vector)) => :customer/client

Can11:11:57

thats better if I need boolean I can transform easily too

Ed11:11:10

:customer/client is a truthy value so if you use that in a conditional context, it will evaluate as true

Ed11:11:27

but there's nothing wrong with casting it to a boolean

Can11:11:53

awesome! Thank you so much for helping.

Ed11:11:10

if :customer/client is not in the list, then some will return nil which is falsey

Can11:11:59

classic begginer code isnt it 😄 almost half of code dissappeared 🙂

λ14:11:01

I'm trying to terminate TCP connections with the following code and I'm not sure why it isn't working https://tildegit.org/michael/irc-bot/src/commit/44e6f7836fc1da85c2af86beb0e275b2259406b5/src/irc_bot/core.clj#L54-L60

dpsutton14:11:45

(def connections (doseq [s servers] (connect s)))

dpsutton14:11:50

this is how you defined connections

dpsutton14:11:08

user=> (doc doseq)
-------------------------
clojure.core/doseq
([seq-exprs & body])
Macro
  Repeatedly executes body (presumably for side-effects) with
  bindings and filtering as provided by "for".  Does not retain
  the head of the sequence. Returns nil.
nil
but look what doseq returns

λ15:11:13

Oh yes, it returns nil!

λ23:11:35

I am running into an error that I have never seen before with this code

(defn connect [server]
  (let [socket (if (true? (:tls server))
                 (SSLSocket. (:name server) (:port server))
                 (Socket. (:name server) (:port server)))
        in     (BufferedReader. (InputStreamReader. (.getInputStream socket)))
        out    (PrintWriter. (.getOutputStream socket))
        conn   (atom {:in in :out out :socket socket})]
    (doto (Thread. #(try (conn-handler conn)
                         (catch SocketException se
                           (println (str "Caught exception: " (.getMessage se))))))
      (.start))
    conn))
--------
Syntax error (IllegalArgumentException) compiling new at (src/irc_bot/core.clj:17:18).
No matching ctor found for class javax.net.ssl.SSLSocket

λ23:11:34

What is a ctor?

phronmophobic23:11:34

what are the values of (:name server) and (:port server)?

λ23:11:39

A contructor?

👍 1
phronmophobic23:11:07

My guess is that that the arguments are unexpected types

λ23:11:22

(def servers [{:name "testnet.ergo.chat" :port 6667 :tls false}
              {:name "irc.tilde.chat" :port 6697 :tls true}
              {:name "irc.libera.chat" :port 6667 :tls false}])

λ23:11:54

I call connect like this:

(def connections (doall (map connect servers)))

phronmophobic23:11:36

The constructors for SSLSockets are protected.

λ23:11:09

OH? I don’t know any Java and this is my first programming language, does that mean I should use SSLSocketFactory isntead?

phronmophobic23:11:13

I'm not super familiar with SSLSockets. Here's a stackoverflow link, https://stackoverflow.com/a/6787029

phronmophobic23:11:20

One trick I use for java interop is to search on http://grep.app to see what's out there: https://grep.app/search?q=SSLSocket&amp;filter[lang][0]=Clojure

λ23:11:13

Oh nice thanks a lot for sharing this with me

phronmophobic23:11:55

👍 java interop is definitely an area that provides a lot of value to the Clojure ecosystem, but it does add to the amount of stuff it requires to get started.

λ00:11:03

Honestly this just seems so weird

λ00:11:06

(.createSocket (SSLSocketFactory/getDefault) (new Socket "irc.tilde.chat" 6667) "irc.tilde.chat" 6697 true)

λ00:11:18

Seems quite odd to repeat the host and the port

phronmophobic00:11:48

If you look at the javadocs, it gives a reason: > This constructor can be used when tunneling SSL through a proxy or when negotiating the use of SSL over an existing socket. The host and port refer to the logical peer destination.

phronmophobic00:11:28

It seems like the host and port might differ when proxying.

phronmophobic02:11:06

you can also try #C053PTJE6