Re: What is this notation? "-a"

2021-12-26 Thread William la Forge
https://stackoverflow.com/questions/10846423/is-there-a-clojure-convention-for-naming-private-functions/10853372

On Saturday, December 25, 2021 at 6:53:56 PM UTC-5 hank@gmail.com wrote:

> 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/dd81c045-f6a3-4c31-94ea-1f252e0fa86an%40googlegroups.com.


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

2021-12-26 Thread LaurentJ
Hank,

Just a message to give you the solution [spoiler alert]
Don't read it, if you still want to search :)






SPOILER












SPOILER





  ;; ugly version using the fact that java objects are mutable in place
  (defn ugly-read-chars-one-by-one
[reader]
(let [sb (StringBuilder.)]
  (loop []
(let [v (.read reader)]
  (if (neg? v)
(.toString sb)
(do (.append sb (char v))
  (recur)))


  ;; "better" version using loop bindings
  (defn read-chars-one-by-one
[reader]
(loop [sb (StringBuilder.)
  v (.read reader)]
  (if (neg? v)
(.toString sb)
(recur (.append sb (char v))
  (.read reader)


  ;; usage
  (require '[clojure.java.io :as io])
  (with-open [rdr (io/reader "some-file.txt")]
(read-chars-one-by-one rdr))



Regards,
Laurent
Le dimanche 26 décembre 2021 à 18:12:56 UTC+1, hank@gmail.com a écrit :

> Hi --
> Thanks for taking the time to help me.
> As far as I understand the examples, loop has this template:
>
> loop [binding]
>   (condition
>  (statement)
>  (recur (binding)))
> And in 'recur' the loop is re-executed with new bindings.
>
> There was indeed an issue with the 'recur' outside 'when'. Thanks for 
> pointing that out.
> I corrected that in the version below.
>
> I also changed to a smaller file (UTF-8 encoded in Linux), called 
> 'ribs.txt', with the following content:
>
> source/txt on  master [!?] 
> ❯ cat ribs.txt
> Try my delicious pork-chop ribs!
>
> source/txt on  master [!?] 
> ❯ cat ribs.hexdump 
> : 54 72 79 20 6d 79 20 64 65 6c 69 63 69 6f 75 73  Try my delicious
> 0010: 20 70 6f 72 6b 2d 63 68 6f 70 20 72 69 62 73 21   pork-chop ribs!
> 0020: 0a   .
>
>
> (defn pt8 [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)
>  ; put recur INSIDE THE WHEN
>  (loop [val (.read bfr)]
>(when (not (= val -1))
>  (.append ct (Character/toChars (.read bfr)))
>(recur [val (.read bfr)])))
> ; when finished...
> (.toString ct)))
>
> I think this fixed the 'recur', because it does rebinding to a new call to 
> "read()".
> However, the errors remains.
> 
> user> (pt8 ribs)
> Execution error (IllegalArgumentException) at java.lang.Character/toChars 
> (Character.java:8572).
> Not a valid Unicode code point: 0x
>
> The file used is sufficiently small so that we can walk the bytes using 
> jshell:
>
> jshell> FileReader afr = new FileReader("/home/hank/source/txt/ribs.txt/")
> afr ==> java.io.FileReader@1698c449
>
> jshell> BufferedReader bfr = new BufferedReader(afr)
> bfr ==> java.io.BufferedReader@5ef04b5
>
> jshell> StringBuilder ct = new StringBuilder()
> ct ==> 
>
> FileReader reads 2 bytes per character so, to get to the end, of the first 
> hexdump line, let's walk 32 bytes:
>
> jshell> for (int i=0; i < 31; i++) {
>...> if ((value = bfr.read()) != -1) { ct.append((char) value); }
>...> i++;
>...> }
>
> jshell> ct
> ct ==> Try my delicious
>
> : 54 72 79 20 6d 79 20 64 65 6c 69 63 69 6f 75 73  Try my delicious
>   T  r  y  SP m  y  SP d  e  l  i  c  i  o  u  s  (< YOU ARE 
> HERE)
>
> : 54 72 79 20 6d 79 20 64 65 6c 69 63 69 6f 75 73  Try my delicious
> 0010: 20 70 6f 72 6b 2d 63 68 6f 70 20 72 69 62 73 21   pork-chop ribs!
> 0020: 0a   .
>
> Now we iterate 31 more bytes, stopping short of the last character:
> jshell> for (int i=0; i < 30; i++) {
>...> if ((value = bfr.read()) != -1) { ct.append((char) value); }
>...> i++;
>...> }
>
> jshell> ct
> ct ==> Try my delicious pork-chop ribs
>
> : 54 72 79 20 6d 79 20 64 65 6c 69 63 69 6f 75 73  Try my delicious
> 0010: 20 70 6f 72 6b 2d 63 68 6f 70 20 72 69 62 73 21   pork-chop ribs!
> ^ we stopped here  
>
> We just advance one more:
>
> jshell> if ((value = bfr.read()) != -1) { ct.append((char) value); }
>...> 
>
> jshell> ct
> ct ==> Try my delicious pork-chop ribs!
>
> And one more time, to see if it borks:
> jshell> if ((value = bfr.read()) != -1) { ct.append((char) value); }
>
> jshell> ct
> ct ==> Try my delicious pork-chop ribs!
>
> Nope, everything looks fine. Now where does that '0xFFF" come from?!
> -- 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 

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

2021-12-26 Thread LaurentJ
Hi Hank,

That loop/recur is still wrong because `loop` set bindings to define names 
and gives initial values but `recur` does *not set bindings*, it just 
provides new values.
So `recur` does not need a vector of bindings like `loop`

The pattern is as follow:
  (loop [a-local-var "initial-value"]
(if (should-stop-looping? a-local-var)
  (execute-last-expression a-local-var)
  (recur a-new-local-var)))

In your pt8 function, you are reading your stream to many times in a loop, 
if I write your pt8 function in procedural pseudo-code, this is what I get: 

bfr = input-stream()
ct = string-buffer()

set loop-start   // set loop point
with val,   // set loop args
val = read(bfr) // set args initial value, first read

if !(val == -1) {
ct.append(char(read(bfr)))  // read again! and lose previous byte!!
goto loop-start
 with val = vector(val, read(bfr))  // read again! this byte will 
be lost soon :(
}
ct.toString()


Regards,
Laurent


Le dimanche 26 décembre 2021 à 18:12:56 UTC+1, hank@gmail.com a écrit :

> Hi --
> Thanks for taking the time to help me.
> As far as I understand the examples, loop has this template:
>
> loop [binding]
>   (condition
>  (statement)
>  (recur (binding)))
> And in 'recur' the loop is re-executed with new bindings.
>
> There was indeed an issue with the 'recur' outside 'when'. Thanks for 
> pointing that out.
> I corrected that in the version below.
>
> I also changed to a smaller file (UTF-8 encoded in Linux), called 
> 'ribs.txt', with the following content:
>
> source/txt on  master [!?] 
> ❯ cat ribs.txt
> Try my delicious pork-chop ribs!
>
> source/txt on  master [!?] 
> ❯ cat ribs.hexdump 
> : 54 72 79 20 6d 79 20 64 65 6c 69 63 69 6f 75 73  Try my delicious
> 0010: 20 70 6f 72 6b 2d 63 68 6f 70 20 72 69 62 73 21   pork-chop ribs!
> 0020: 0a   .
>
>
> (defn pt8 [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)
>  ; put recur INSIDE THE WHEN
>  (loop [val (.read bfr)]
>(when (not (= val -1))
>  (.append ct (Character/toChars (.read bfr)))
>(recur [val (.read bfr)])))
> ; when finished...
> (.toString ct)))
>
> I think this fixed the 'recur', because it does rebinding to a new call to 
> "read()".
> However, the errors remains.
> 
> user> (pt8 ribs)
> Execution error (IllegalArgumentException) at java.lang.Character/toChars 
> (Character.java:8572).
> Not a valid Unicode code point: 0x
>
> The file used is sufficiently small so that we can walk the bytes using 
> jshell:
>
> jshell> FileReader afr = new FileReader("/home/hank/source/txt/ribs.txt/")
> afr ==> java.io.FileReader@1698c449
>
> jshell> BufferedReader bfr = new BufferedReader(afr)
> bfr ==> java.io.BufferedReader@5ef04b5
>
> jshell> StringBuilder ct = new StringBuilder()
> ct ==> 
>
> FileReader reads 2 bytes per character so, to get to the end, of the first 
> hexdump line, let's walk 32 bytes:
>
> jshell> for (int i=0; i < 31; i++) {
>...> if ((value = bfr.read()) != -1) { ct.append((char) value); }
>...> i++;
>...> }
>
> jshell> ct
> ct ==> Try my delicious
>
> : 54 72 79 20 6d 79 20 64 65 6c 69 63 69 6f 75 73  Try my delicious
>   T  r  y  SP m  y  SP d  e  l  i  c  i  o  u  s  (< YOU ARE 
> HERE)
>
> : 54 72 79 20 6d 79 20 64 65 6c 69 63 69 6f 75 73  Try my delicious
> 0010: 20 70 6f 72 6b 2d 63 68 6f 70 20 72 69 62 73 21   pork-chop ribs!
> 0020: 0a   .
>
> Now we iterate 31 more bytes, stopping short of the last character:
> jshell> for (int i=0; i < 30; i++) {
>...> if ((value = bfr.read()) != -1) { ct.append((char) value); }
>...> i++;
>...> }
>
> jshell> ct
> ct ==> Try my delicious pork-chop ribs
>
> : 54 72 79 20 6d 79 20 64 65 6c 69 63 69 6f 75 73  Try my delicious
> 0010: 20 70 6f 72 6b 2d 63 68 6f 70 20 72 69 62 73 21   pork-chop ribs!
> ^ we stopped here  
>
> We just advance one more:
>
> jshell> if ((value = bfr.read()) != -1) { ct.append((char) value); }
>...> 
>
> jshell> ct
> ct ==> Try my delicious pork-chop ribs!
>
> And one more time, to see if it borks:
> jshell> if ((value = bfr.read()) != -1) { ct.append((char) value); }
>
> jshell> ct
> ct ==> Try my delicious pork-chop ribs!
>
> Nope, everything looks fine. Now where does that '0xFFF" come from?!
> -- 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 

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

2021-12-26 Thread Hank Lenzi
Hi --
Thanks for taking the time to help me.
As far as I understand the examples, loop has this template:

loop [binding]
  (condition
 (statement)
 (recur (binding)))
And in 'recur' the loop is re-executed with new bindings.

There was indeed an issue with the 'recur' outside 'when'. Thanks for 
pointing that out.
I corrected that in the version below.

I also changed to a smaller file (UTF-8 encoded in Linux), called 
'ribs.txt', with the following content:

source/txt on  master [!?] 
❯ cat ribs.txt
Try my delicious pork-chop ribs!

source/txt on  master [!?] 
❯ cat ribs.hexdump 
: 54 72 79 20 6d 79 20 64 65 6c 69 63 69 6f 75 73  Try my delicious
0010: 20 70 6f 72 6b 2d 63 68 6f 70 20 72 69 62 73 21   pork-chop ribs!
0020: 0a   .


(defn pt8 [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)
 ; put recur INSIDE THE WHEN
 (loop [val (.read bfr)]
   (when (not (= val -1))
 (.append ct (Character/toChars (.read bfr)))
   (recur [val (.read bfr)])))
; when finished...
(.toString ct)))

I think this fixed the 'recur', because it does rebinding to a new call to 
"read()".
However, the errors remains.

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

The file used is sufficiently small so that we can walk the bytes using 
jshell:

jshell> FileReader afr = new FileReader("/home/hank/source/txt/ribs.txt/")
afr ==> java.io.FileReader@1698c449

jshell> BufferedReader bfr = new BufferedReader(afr)
bfr ==> java.io.BufferedReader@5ef04b5

jshell> StringBuilder ct = new StringBuilder()
ct ==> 

FileReader reads 2 bytes per character so, to get to the end, of the first 
hexdump line, let's walk 32 bytes:

jshell> for (int i=0; i < 31; i++) {
   ...> if ((value = bfr.read()) != -1) { ct.append((char) value); }
   ...> i++;
   ...> }

jshell> ct
ct ==> Try my delicious

: 54 72 79 20 6d 79 20 64 65 6c 69 63 69 6f 75 73  Try my delicious
  T  r  y  SP m  y  SP d  e  l  i  c  i  o  u  s  (< YOU ARE 
HERE)

: 54 72 79 20 6d 79 20 64 65 6c 69 63 69 6f 75 73  Try my delicious
0010: 20 70 6f 72 6b 2d 63 68 6f 70 20 72 69 62 73 21   pork-chop ribs!
0020: 0a   .

Now we iterate 31 more bytes, stopping short of the last character:
jshell> for (int i=0; i < 30; i++) {
   ...> if ((value = bfr.read()) != -1) { ct.append((char) value); }
   ...> i++;
   ...> }

jshell> ct
ct ==> Try my delicious pork-chop ribs

: 54 72 79 20 6d 79 20 64 65 6c 69 63 69 6f 75 73  Try my delicious
0010: 20 70 6f 72 6b 2d 63 68 6f 70 20 72 69 62 73 21   pork-chop ribs!
^ we stopped here  

We just advance one more:

jshell> if ((value = bfr.read()) != -1) { ct.append((char) value); }
   ...> 

jshell> ct
ct ==> Try my delicious pork-chop ribs!

And one more time, to see if it borks:
jshell> if ((value = bfr.read()) != -1) { ct.append((char) value); }

jshell> ct
ct ==> Try my delicious pork-chop ribs!

Nope, everything looks fine. Now where does that '0xFFF" come from?!
-- 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/d3510982-bcaa-43c2-9461-aaae7be11f49n%40googlegroups.com.


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

2021-12-26 Thread LaurentJ
Hank

Your loop/recur in your pt5 function is still not good. Take the time to 
read the loop/recur documentation and to understand examples.

A Clojure loop/recur is not really a loop like in other procedural 
languages.
It is more akin to a new function call at the `loop` point with new args 
provided by the `recur` call.

If you continue with the function call metaphor
The `loop` call defines 3 things:
  - the starting point of the function
  - the argument name of the function
  - the **initial values** of those arguments

When you need to call again that function with new arguments, you use 
`recur` with **new values**.
When you don't need to recur, well... dont call `recur` :) and just 
evaluate a last expression which is the result of the `loop` expression.

regards
Laurent


Le dimanche 26 décembre 2021 à 02:35:47 UTC+1, hank@gmail.com a écrit :

> 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/66318891-e779-4a3c-818c-a235f7b32550n%40googlegroups.com.


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

2021-12-26 Thread LaurentJ
Hi,

In the quoted example the `recur` call is *inside* the `when` which is a 
huge difference because there is in this case an halting condition to get 
out of the loop ;)

regards
Laurent
Le dimanche 26 décembre 2021 à 14:41:15 UTC+1, hank@gmail.com a écrit :

> 2021-12-25, 21:11:46 UTC-3, LaurentJ wrote:
> "Hi,
>
> Your loop/recur usage is wrong, your error may be because your loop has no 
> halting condition."
>
> Hi Laurent --
> I actually took inspiration from one of the sources you posted:
> (import '(javax.sound.sampled AudioSystem AudioFormat$Encoding))
>
> (let [mp3-file (java.io.File. "tryout.mp3")
>   audio-in (AudioSystem/getAudioInputStream mp3-file)
>   audio-decoded-in (AudioSystem/getAudioInputStream 
> AudioFormat$Encoding/PCM_SIGNED audio-in)
>   buffer (make-array Byte/TYPE 1024)]
>   (loop []
> (let [size (.read audio-decoded-in buffer)]
>   (when (> size 0)
> ;do something with PCM data
> (recur)
>
>
>

-- 
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/93ed9e36-bd43-4592-b5c3-89187304d75cn%40googlegroups.com.


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

2021-12-26 Thread Hank Lenzi
2021-12-25, 21:11:46 UTC-3, LaurentJ wrote:
"Hi,

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

Hi Laurent --
I actually took inspiration from one of the sources you posted:
(import '(javax.sound.sampled AudioSystem AudioFormat$Encoding))

(let [mp3-file (java.io.File. "tryout.mp3")
  audio-in (AudioSystem/getAudioInputStream mp3-file)
  audio-decoded-in (AudioSystem/getAudioInputStream 
AudioFormat$Encoding/PCM_SIGNED audio-in)
  buffer (make-array Byte/TYPE 1024)]
  (loop []
(let [size (.read audio-decoded-in buffer)]
  (when (> size 0)
;do something with PCM data
(recur)


-- 
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/c293ceb0-d187-41f9-b0a4-68860cb55bd2n%40googlegroups.com.


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

2021-12-26 Thread Hank Lenzi
Thanks, Harold.
You see, that was an exercise in Java interop - I know about slurp, but I 
was trying to understand what was going on there.
-- 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/dc222f28-0f21-4f02-b79c-1d0f4db4f95bn%40googlegroups.com.