Fork me on GitHub
#beginners
<
2020-02-22
>
piyer04:02:57

why does this (filter (fn [x] (Character/isUpperCase x)) (char-array "AfSDds")) and not (filter Character/isUpperCase (char-array "ADdasd"))

Ankon Chakraborty06:02:01

This will work fine until you’re trying to compose or trying to use partial

piyer04:02:02

I see, that is a java.lang.Character fn.

Jimmy Miller04:02:17

Yeah, java methods cannot directly be used as first class functions.

hindol18:02:13

Is the underlying reason Java methods do not implement AFn? Curious to know.

noisesmith18:02:01

java methods are not first class objects - they are parameterization of the object they are invoked on

👍 4
noisesmith18:02:29

so there's no mechanism to provide them as an argument to something else

noisesmith18:02:46

in other words - not only do they not implement AFn, there's no "it" to attach AFn to

hindol05:02:41

That makes sense. Java functions are not objects so it's impossible to implement AFn for them, Did I interpret that right?

noisesmith20:02:57

almost - "function" is not the java term, this becomes tricky after iirc java 1.7 where there's a Function interface! which is kind of like a first class function, specialized for data streaming

noisesmith20:02:15

I think it's safest to not use the word function in reference to java unless you are talking about those

noisesmith20:02:32

clojure functions are java objects that implement IFn (and via that AFn)

piyer05:02:13

Is there a better way to write this? (defn my-range [x y] (loop [x x y y acc '()] (if (<= x y) (recur (inc x) y (concat acc (list x))) acc)))

ScArcher06:02:49

Are you writing this as a learning exercise?

hiredman06:02:03

Yes, don't use concat like that

hiredman06:02:35

Also you don't need a loop binding for y

piyer16:02:33

Nice. Thanks for sharing that.

andy.fingerhut06:02:53

conj onto a vector that is initially the empty vector [] would be a more efficient approach there, and idiomatic for Clojure. Note that since y never changes throughout the loop, you can leave it out of the list of loop bindings, and thus also the recur call. The value of y is usable inside the loop, since it is defined in the surrounding scope of the loop expression.

piyer16:02:38

that make sense. Thank you.

piyer16:02:03

Here is what I am trying. I was trying to concat because the test case expects persistentlist https://clojurecademy.com/course#!/subjects/17592186045880

hindol18:02:05

You can do (cons y '(x)) and get '(y x).

hindol18:02:40

Note that the order is reversed. In the end, you reverse again.

andy.fingerhut07:02:55

And if this is for practice as a learning exercise, cool, but if not, then there is a range function included with Clojure that has been optimized like crazy for any range of integers.

agigao16:02:18

hey guys, HugSQL issue here - I’m trying to provide a list of values dynamically to query and I encounter SQL Type Inference issue.

Can't infer the SQL type to use for an instance of clojure.lang.LazySeq. Use setObject() with an explicit Types value to specify the type to use.
Execution error (PSQLException) at org.postgresql.jdbc.PgPreparedStatement/setObject (PgPreparedStatement.java:962).
Any pointers are thoroughly welcome.

curtis.summers16:02:13

This is the PG JDBC driver not knowing how to deal with a LazySeq parameter. Every parameter’s Clojure/Java data type should have a corresponding PG JDBC driver data type in order to be parameterized by the JDBC driver. You might be passing in a value that is not what you’ve intended. Do you have an example of the code causing this?

curtis.summers16:02:57

HugSQL can work with a list of values with the Value List parameter: https://www.hugsql.org/#param-value-list Is this what you’re trying to do?

agigao05:02:53

I’m passing a list of event_id to a query:

(def events (sql/get-events db (:events params)))
(def tickets (sql/get-tickets db {:events (map :event_id events)}))

agigao05:02:35

tried conversion to a vector, list but the issue persists.

agigao05:02:47

Thanks for response!

curtis.summers14:02:10

How are you using the list in the SQL? Are you using a built-in HugSQL parameter type like Value List, where you’ve specified the parameter as :v*:events ?

abdullahibra16:02:52

how can i add sub-selects in honeysql and cross join lateral ?

piyer17:02:20

I was trying https://clojurecademy.com/course#!/subjects/17592186045888 (= (apply str (compress "Leeeeeerrroyyy")) "Leroy") that is the test case. My brain was thinking to use set or hash-map, here is what SegFaultAX wrote. #(map first (partition-by identity %)) This is brilliant; for other beginners out there checkout https://gist.github.com/SegFaultAX/3607101

👍 4
bnstvn18:02:14

when i have a java object which .toString() throws a NPE, the repl throws an NPE as well when it tries to print it — the printed literal doesn’t seem to contain the toString value normally, so why it is called?

jumpnbrownweasel18:02:01

Can you make this more concrete by showing some code? Having trouble following it.

jumpnbrownweasel18:02:03

Is toString throwing a NPE?

didibus18:02:19

toString throwing an NPE is a bug in the java code

didibus18:02:13

The repl I think will call str on the elements

didibus18:02:23

Which calls toString under the hood

didibus18:02:52

While print doesn't always call toString, it delegates to print-method multi-method instead

bnstvn21:02:58

thanks! i managed to make it bearable with redefining print-method on a couple of places