Re: Pesky java interop bug with 0xFFFFFFF error in BuferredReader

2021-12-25 Thread Harold
Hank,

Welcome. Great efforts- it certainly seems like you're learning a lot, and 
quickly.

`clojure.core/slurp` is related, in case you haven't seen it 
yet: https://clojuredocs.org/clojure.core/slurp

The implementation may also be 
enlightening: 
https://github.com/clojure/clojure/blob/clojure-1.10.3/src/clj/clojure/core.clj#L6944-L6954

It's natural when coming from non-functional languages to write `loop`-y 
code like you did, and while it's neat that Clojure enables writing code in 
that style (essential, actually, to the stated goal of pragmatism), it's 
not always helpful. In this case, the implementation of `slurp` also 
interops with Java to produce strings, but does so at a more helpful level 
of abstraction.

I'll also note here that the https://ask.clojure.org/ site may be more fun 
for these types of discussions than the mailing list- though getting help 
anywhere (there are quite a few places actually) is of course fine.

Best of luck, and warm wishes,
-Harold

On Saturday, December 25, 2021 at 6:35:47 PM UTC-7 hank@gmail.com wrote:

> Thank for the answers.
> Trying to recur with '(recur (.read bfr))' resulted in a:
> Syntax error (UnsupportedOperationException) compiling recur at 
> (*cider-repl ~:localhost:41097(clj)*:237:9).
> Can only recur from tail position
> So I changed the code (see below). 
>
> And now it complains that a previous form that was working '(.append 
> etc..') doesn't and the same error remains. 
>
> user> (pt5 myfile)
>
> Execution error (IllegalArgumentException) at java.lang.Character/toChars 
> (Character.java:8572).
> Not a valid Unicode code point: 0x
>
> (defn pt5 [file]
>
>(let [afr (FileReader. file); instances of FileReader, BufferedReader, 
> StringBuffer
>  bfr (BufferedReader. afr)
>  ct (StringBuilder.)
>  this-list (list afr bfr ct)]
>  ; (apply println this-list)
>  (loop [val (.read bfr)]
>(when (not (= val -1))
>  (.append ct (Character/toChars (.read bfr
>(recur val))
> ; when finished...
> (.toString ct)))
>
> Harder then it seemed at first sight...
> -- Hank
>
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/87069aee-1320-4774-b214-bae29e0062f3n%40googlegroups.com.


Re: Pesky java interop bug with 0xFFFFFFF error in BuferredReader

2021-12-25 Thread Hank Lenzi
Thank for the answers.
Trying to recur with '(recur (.read bfr))' resulted in a:
Syntax error (UnsupportedOperationException) compiling recur at 
(*cider-repl ~:localhost:41097(clj)*:237:9).
Can only recur from tail position
So I changed the code (see below). 

And now it complains that a previous form that was working '(.append 
etc..') doesn't and the same error remains. 

user> (pt5 myfile)
Execution error (IllegalArgumentException) at java.lang.Character/toChars 
(Character.java:8572).
Not a valid Unicode code point: 0x

(defn pt5 [file]
   (let [afr (FileReader. file); instances of FileReader, BufferedReader, 
StringBuffer
 bfr (BufferedReader. afr)
 ct (StringBuilder.)
 this-list (list afr bfr ct)]
 ; (apply println this-list)
 (loop [val (.read bfr)]
   (when (not (= val -1))
 (.append ct (Character/toChars (.read bfr
   (recur val))
; when finished...
(.toString ct)))

Harder then it seemed at first sight...
-- Hank


-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/17bab821-c47d-4791-b3ad-c56df51757a1n%40googlegroups.com.


Re: Pesky java interop bug with 0xFFFFFFF error in BuferredReader

2021-12-25 Thread Mark Nutter
I think at least part of the problem is your use of val in the let
statement. Inside the loop, you're testing (not (= val -1)),  but val is an
immutable value defined above the loop as being the first character read
from the buffer, so it will always loop until it reads in the 0xFFF
that makes it crash. You probably want to modify your loop like this:

(loop [val (.read bfr)]
  (when (not (= val -1))
(.append ct (Character/toChars val)))
(recur (.read bfr))

Now this re-assigns the next character from the buffer to val each time you
go through the loop.

I'm not a guru when it comes to Java interop, but I think the above is the
main problem you're having right now, so hopefully that will get you back
on track.


On Sat, Dec 25, 2021 at 2:23 PM Hank Lenzi  wrote:

>
> Hello --
>
> I'm learning Clojure and its Java interop stuff. I am trying to emulate
> this function:
>
>  public String readAllCharsOneByOne(BufferedReader bufferedReader) throws
> IOException {
> StringBuilder content = new StringBuilder();
>
> int value;
> while ((value = bufferedReader.read()) != -1) {
> content.append((char) value);
> }
>
> return content.toString();
> }
>
>
> So far, I've been able to reason that the parts I need are:
>
>
> (def myfile "/path/to/svenska_sample.txt")
> (import java.io.BufferedReader)
> (import java.io.FileReader)
> (import java.lang.StringBuilder)
> (import java.lang.Character)
>
> (def a-FileReader (FileReader. myfile))
> (def bufferedReader (BufferedReader. a-FileReader))
> (def content (StringBuilder.))
>
> which works
>
> user> (.append content (Character/toChars (.read bufferedReader)))
> #object[java.lang.StringBuilder 0x490447d0 "\nDe"]
> user> (.append content (Character/toChars (.read bufferedReader)))
> #object[java.lang.StringBuilder 0x490447d0 "\nDen"]
> user> (.append content (Character/toChars (.read bufferedReader)))
> #object[java.lang.StringBuilder 0x490447d0 "\nDen "]
> user> (.append content (Character/toChars (.read bufferedReader)))
> #object[java.lang.StringBuilder 0x490447d0 "\nDen t"]
> user> (.append content (Character/toChars (.read bufferedReader)))
> #object[java.lang.StringBuilder 0x490447d0 "\nDen ty"]
> user> (.append content (Character/toChars (.read bufferedReader)))
> #object[java.lang.StringBuilder 0x490447d0 "\nDen typ"]
> user> (.append content (Character/toChars (.read bufferedReader)))
> #object[java.lang.StringBuilder 0x490447d0 "\nDen typi"]
>
> The file is a small text file UTF-8 encoded in Linux with the following
> content:
>
> ❯ cat svenska_sample.txt
>
> Den typiska impulsiva olycksfågeln är en ung man som kraschar flera bilar,
> och ofta skryter lite med det, i varje fall när han är tillsammans med sina
> vänner._. För dem har otur i det närmaste blivit en livsstil, och de råkar
> konstant ut för olyckor, stora som små. Olycksfåglar kallar vi dem. Hur
> många det finns kan ingen med säkerhet säga, för det finns inga konkreta
> definitioner på denna grupp, och heller ingen given avgränsning av den. Att
> de finns, råder det emellertid ingen tvekan om, varken på sjukhusens
> akutmottagningar eller i försäkringsbranschen
>
> I wrote a function, with my brand new Clojure Java interop chops, that
> looks like this:
>
> ;; COMPILES  - BUT CAN'T GET AROUND THE 0XFFF BUG
> (defn pt%% [file]
>(let [afr (FileReader. file); instances of FileReader, BufferedReader,
> StringBuffer
>  bfr (BufferedReader. afr)
>  ct (StringBuilder.)
>  val (.read bfr)
>  this-list (list afr bfr ct)]
>  ; (apply println this-list)
>  (loop []
>(when (not (= val -1))
>  (.append ct (Character/toChars (.read bfr
>(recur))
> ; when finished...
>  (.toString ct)))
>
> but it borks with the following error:
>
> user> (pt%% myfile)
> Execution error (IllegalArgumentException) at java.lang.Character/toChars
> (Character.java:8572).
> Not a valid Unicode code point: 0x
>
> What in the world could be causing this (NOTE: I am not a Java
> programmer)?
> Here is the hex dump of the file text file:
>
> ❯ cat svenska_sample.hexdump
> : 0a44 656e 2074 7970 6973 6b61 2069 6d70  .Den typiska imp
> 0010: 756c 7369 7661 206f 6c79 636b 7366 c3a5  ulsiva olycksf..
> 0020: 6765 6c6e 20c3 a472 2065 6e20 756e 6720  geln ..r en ung
> 0030: 6d61 6e20 736f 6d20 6b72 6173 6368 6172  man som kraschar
> 0040: 2066 6c65 7261 2062 696c 6172 2c20 6f63   flera bilar, oc
> 0050: 6820 6f66 7461 2073 6b72 7974 6572 206c  h ofta skryter l
> 0060: 6974 6520 6d65 6420 6465 742c 2069 2076  ite med det, i v
> 0070: 6172 6a65 2066 616c 6c20 6ec3 a472 2068  arje fall n..r h
> 0080: 616e 20c3 a472 2074 696c 6c73 616d 6d61  an ..r tillsamma
> 0090: 6e73 206d 6564 2073 696e 6120 76c3 a46e  ns med sina v..n
> 00a0: 6e65 722e 5f2e 2046 c3b6 7220 6465 6d20  ner._. F..r dem
> 00b0: 6861 7220 6f74 7572 2069 2064 

Re: Pesky java interop bug with 0xFFFFFFF error in BuferredReader

2021-12-25 Thread LaurentJ
Hi,

Your loop/recur usage is wrong, your error may be because your loop has no 
halting condition.

https://clojure.org/reference/special_forms#loop
https://clojuredocs.org/clojure.core/loop

Regards
Laurent

Le samedi 25 décembre 2021 à 20:23:37 UTC+1, hank@gmail.com a écrit :

>
> Hello --
>
> I'm learning Clojure and its Java interop stuff. I am trying to emulate 
> this function:
>
>  public String readAllCharsOneByOne(BufferedReader bufferedReader) throws 
> IOException {
> StringBuilder content = new StringBuilder();
> 
> int value;
> while ((value = bufferedReader.read()) != -1) {
> content.append((char) value);
> }
> 
> return content.toString();
> }
>
>
> So far, I've been able to reason that the parts I need are:
>
>
> (def myfile "/path/to/svenska_sample.txt")
> (import java.io.BufferedReader)
> (import java.io.FileReader) 
> (import java.lang.StringBuilder)
> (import java.lang.Character)
>  
> (def a-FileReader (FileReader. myfile))
> (def bufferedReader (BufferedReader. a-FileReader))
> (def content (StringBuilder.))
>
> which works
>
> user> (.append content (Character/toChars (.read bufferedReader)))
> #object[java.lang.StringBuilder 0x490447d0 "\nDe"]
> user> (.append content (Character/toChars (.read bufferedReader)))
> #object[java.lang.StringBuilder 0x490447d0 "\nDen"]
> user> (.append content (Character/toChars (.read bufferedReader)))
> #object[java.lang.StringBuilder 0x490447d0 "\nDen "]
> user> (.append content (Character/toChars (.read bufferedReader)))
> #object[java.lang.StringBuilder 0x490447d0 "\nDen t"]
> user> (.append content (Character/toChars (.read bufferedReader)))
> #object[java.lang.StringBuilder 0x490447d0 "\nDen ty"]
> user> (.append content (Character/toChars (.read bufferedReader)))
> #object[java.lang.StringBuilder 0x490447d0 "\nDen typ"]
> user> (.append content (Character/toChars (.read bufferedReader)))
> #object[java.lang.StringBuilder 0x490447d0 "\nDen typi"]
>
> The file is a small text file UTF-8 encoded in Linux with the following 
> content:
>
> ❯ cat svenska_sample.txt 
>
> Den typiska impulsiva olycksfågeln är en ung man som kraschar flera bilar, 
> och ofta skryter lite med det, i varje fall när han är tillsammans med sina 
> vänner._. För dem har otur i det närmaste blivit en livsstil, och de råkar 
> konstant ut för olyckor, stora som små. Olycksfåglar kallar vi dem. Hur 
> många det finns kan ingen med säkerhet säga, för det finns inga konkreta 
> definitioner på denna grupp, och heller ingen given avgränsning av den. Att 
> de finns, råder det emellertid ingen tvekan om, varken på sjukhusens 
> akutmottagningar eller i försäkringsbranschen
>
> I wrote a function, with my brand new Clojure Java interop chops, that 
> looks like this:
>
> ;; COMPILES  - BUT CAN'T GET AROUND THE 0XFFF BUG
> (defn pt%% [file]
>(let [afr (FileReader. file); instances of FileReader, BufferedReader, 
> StringBuffer
>  bfr (BufferedReader. afr)
>  ct (StringBuilder.)
>  val (.read bfr)
>  this-list (list afr bfr ct)]
>  ; (apply println this-list)
>  (loop []
>(when (not (= val -1))
>  (.append ct (Character/toChars (.read bfr
>(recur))
> ; when finished...
>  (.toString ct)))
>  
> but it borks with the following error:
>
> user> (pt%% myfile)
> Execution error (IllegalArgumentException) at java.lang.Character/toChars 
> (Character.java:8572).
> Not a valid Unicode code point: 0x
>
> What in the world could be causing this (NOTE: I am not a Java 
> programmer)? 
> Here is the hex dump of the file text file:
>
> ❯ cat svenska_sample.hexdump 
> : 0a44 656e 2074 7970 6973 6b61 2069 6d70  .Den typiska imp
> 0010: 756c 7369 7661 206f 6c79 636b 7366 c3a5  ulsiva olycksf..
> 0020: 6765 6c6e 20c3 a472 2065 6e20 756e 6720  geln ..r en ung 
> 0030: 6d61 6e20 736f 6d20 6b72 6173 6368 6172  man som kraschar
> 0040: 2066 6c65 7261 2062 696c 6172 2c20 6f63   flera bilar, oc
> 0050: 6820 6f66 7461 2073 6b72 7974 6572 206c  h ofta skryter l
> 0060: 6974 6520 6d65 6420 6465 742c 2069 2076  ite med det, i v
> 0070: 6172 6a65 2066 616c 6c20 6ec3 a472 2068  arje fall n..r h
> 0080: 616e 20c3 a472 2074 696c 6c73 616d 6d61  an ..r tillsamma
> 0090: 6e73 206d 6564 2073 696e 6120 76c3 a46e  ns med sina v..n
> 00a0: 6e65 722e 5f2e 2046 c3b6 7220 6465 6d20  ner._. F..r dem 
> 00b0: 6861 7220 6f74 7572 2069 2064 6574 206e  har otur i det n
> 00c0: c3a4 726d 6173 7465 2062 6c69 7669 7420  ..rmaste blivit 
> 00d0: 656e 206c 6976 7373 7469 6c2c 206f 6368  en livsstil, och
> 00e0: 2064 6520 72c3 a56b 6172 206b 6f6e 7374   de r..kar konst
> 00f0: 616e 7420 7574 2066 c3b6 7220 6f6c 7963  ant ut f..r olyc
> 0100: 6b6f 722c 2073 746f 7261 2073 6f6d 2073  kor, stora som s
> 0110: 6dc3 a52e 204f 6c79 636b 7366 c3a5 676c  m... Olycksf..gl
> 

What is this notation? "-a"

2021-12-25 Thread Hank Lenzi
Hello --

Sometimes I see a notation that uses a prefix "-", as in:

user> (def -a (atom []))
#'user/-a

Is there a special meaning/convention regarding this use of a hyphen prefix?
TIA
-- Hank

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/7512b9ae-2119-43d8-9595-41bcc38caae5n%40googlegroups.com.


Pesky java interop bug with 0xFFFFFFF error in BuferredReader

2021-12-25 Thread Hank Lenzi

Hello --

I'm learning Clojure and its Java interop stuff. I am trying to emulate 
this function:

 public String readAllCharsOneByOne(BufferedReader bufferedReader) throws 
IOException {
StringBuilder content = new StringBuilder();

int value;
while ((value = bufferedReader.read()) != -1) {
content.append((char) value);
}

return content.toString();
}


So far, I've been able to reason that the parts I need are:


(def myfile "/path/to/svenska_sample.txt")
(import java.io.BufferedReader)
(import java.io.FileReader) 
(import java.lang.StringBuilder)
(import java.lang.Character)
 
(def a-FileReader (FileReader. myfile))
(def bufferedReader (BufferedReader. a-FileReader))
(def content (StringBuilder.))

which works

user> (.append content (Character/toChars (.read bufferedReader)))
#object[java.lang.StringBuilder 0x490447d0 "\nDe"]
user> (.append content (Character/toChars (.read bufferedReader)))
#object[java.lang.StringBuilder 0x490447d0 "\nDen"]
user> (.append content (Character/toChars (.read bufferedReader)))
#object[java.lang.StringBuilder 0x490447d0 "\nDen "]
user> (.append content (Character/toChars (.read bufferedReader)))
#object[java.lang.StringBuilder 0x490447d0 "\nDen t"]
user> (.append content (Character/toChars (.read bufferedReader)))
#object[java.lang.StringBuilder 0x490447d0 "\nDen ty"]
user> (.append content (Character/toChars (.read bufferedReader)))
#object[java.lang.StringBuilder 0x490447d0 "\nDen typ"]
user> (.append content (Character/toChars (.read bufferedReader)))
#object[java.lang.StringBuilder 0x490447d0 "\nDen typi"]

The file is a small text file UTF-8 encoded in Linux with the following 
content:

❯ cat svenska_sample.txt 

Den typiska impulsiva olycksfågeln är en ung man som kraschar flera bilar, 
och ofta skryter lite med det, i varje fall när han är tillsammans med sina 
vänner._. För dem har otur i det närmaste blivit en livsstil, och de råkar 
konstant ut för olyckor, stora som små. Olycksfåglar kallar vi dem. Hur 
många det finns kan ingen med säkerhet säga, för det finns inga konkreta 
definitioner på denna grupp, och heller ingen given avgränsning av den. Att 
de finns, råder det emellertid ingen tvekan om, varken på sjukhusens 
akutmottagningar eller i försäkringsbranschen

I wrote a function, with my brand new Clojure Java interop chops, that 
looks like this:

;; COMPILES  - BUT CAN'T GET AROUND THE 0XFFF BUG
(defn pt%% [file]
   (let [afr (FileReader. file); instances of FileReader, BufferedReader, 
StringBuffer
 bfr (BufferedReader. afr)
 ct (StringBuilder.)
 val (.read bfr)
 this-list (list afr bfr ct)]
 ; (apply println this-list)
 (loop []
   (when (not (= val -1))
 (.append ct (Character/toChars (.read bfr
   (recur))
; when finished...
 (.toString ct)))
 
but it borks with the following error:

user> (pt%% myfile)
Execution error (IllegalArgumentException) at java.lang.Character/toChars 
(Character.java:8572).
Not a valid Unicode code point: 0x

What in the world could be causing this (NOTE: I am not a Java programmer)? 
Here is the hex dump of the file text file:

❯ cat svenska_sample.hexdump 
: 0a44 656e 2074 7970 6973 6b61 2069 6d70  .Den typiska imp
0010: 756c 7369 7661 206f 6c79 636b 7366 c3a5  ulsiva olycksf..
0020: 6765 6c6e 20c3 a472 2065 6e20 756e 6720  geln ..r en ung 
0030: 6d61 6e20 736f 6d20 6b72 6173 6368 6172  man som kraschar
0040: 2066 6c65 7261 2062 696c 6172 2c20 6f63   flera bilar, oc
0050: 6820 6f66 7461 2073 6b72 7974 6572 206c  h ofta skryter l
0060: 6974 6520 6d65 6420 6465 742c 2069 2076  ite med det, i v
0070: 6172 6a65 2066 616c 6c20 6ec3 a472 2068  arje fall n..r h
0080: 616e 20c3 a472 2074 696c 6c73 616d 6d61  an ..r tillsamma
0090: 6e73 206d 6564 2073 696e 6120 76c3 a46e  ns med sina v..n
00a0: 6e65 722e 5f2e 2046 c3b6 7220 6465 6d20  ner._. F..r dem 
00b0: 6861 7220 6f74 7572 2069 2064 6574 206e  har otur i det n
00c0: c3a4 726d 6173 7465 2062 6c69 7669 7420  ..rmaste blivit 
00d0: 656e 206c 6976 7373 7469 6c2c 206f 6368  en livsstil, och
00e0: 2064 6520 72c3 a56b 6172 206b 6f6e 7374   de r..kar konst
00f0: 616e 7420 7574 2066 c3b6 7220 6f6c 7963  ant ut f..r olyc
0100: 6b6f 722c 2073 746f 7261 2073 6f6d 2073  kor, stora som s
0110: 6dc3 a52e 204f 6c79 636b 7366 c3a5 676c  m... Olycksf..gl
0120: 6172 206b 616c 6c61 7220 7669 2064 656d  ar kallar vi dem
0130: 2e20 4875 7220 6dc3 a56e 6761 2064 6574  . Hur m..nga det
0140: 2066 696e 6e73 206b 616e 2069 6e67 656e   finns kan ingen
0150: 206d 6564 2073 c3a4 6b65 7268 6574 2073   med s..kerhet s
0160: c3a4 6761 2c20 66c3 b672 2064 6574 2066  ..ga, f..r det f
0170: 696e 6e73 2069 6e67 6120 6b6f 6e6b 7265  inns inga konkre
0180: 7461 2064 6566 696e 6974 696f 6e65 7220  ta definitioner 
0190: 70c3 a520