Re: [racket-users] Re-defined constant when dynamically evaluating module forms

2021-05-12 Thread Matthew Flatt
I think the problem here is that you're re-defining a module. The
`lifted/4` binding is probably a generated one in the module, and
redeclaring the module would require changing `lifted/4` --- and
probably other bindings that you would recognize, but `lifted/4`
happens to be hit first.

To avoid this kind of problem redeclaring a module, maybe you want a
fresh namespace every time, or maybe you want to parameterize `eval`
with `compile-enforce-module-constants` as #f so that redefinition is
allowed.

At Thu, 13 May 2021 02:40:44 +, Sage Gerard wrote:
> I have a program that takes a (trusted) module form as a list and then
> pulls out a provided value using this method. I used syntax/modread and
> generate a pseudorandom id for each value of `trusted` here.
> 
> (define-namespace-anchor anchor)
> (define module-namespace (namespace-anchor->namespace anchor))
> (define (add trusted)
>    (eval trusted module-namespace)
>    (dynamic-require `',(cadr trusted) 'data))
> 
> Full disclosure: I don't know how to do better. I figured that this
> wasn't the best way to do it to begin with because I get the following
> error under `raco test`'s runtime configuration.
> 
> define-values: assignment disallowed;
>   cannot re-define a constant
>    constant: lifted/4
>    in module:'pkgdef1620873248
> 
> I don't know what "lifted/4" is.
> 
> If I am bringing in module forms that I trust, what's the best way to
> dynamically evaluate them and pull out a provided value without running
> into that error?
> 
> --
> 
> ~slg
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email 
> to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/e0a048c8-77ac-5126-5bef-226ecbe90
> 6dc%40sagegerard.com.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/20210512204617.0%40sirmail.smtps.cs.utah.edu.


[racket-users] Re-defined constant when dynamically evaluating module forms

2021-05-12 Thread Sage Gerard
I have a program that takes a (trusted) module form as a list and then
pulls out a provided value using this method. I used syntax/modread and
generate a pseudorandom id for each value of `trusted` here.

(define-namespace-anchor anchor)
(define module-namespace (namespace-anchor->namespace anchor))
(define (add trusted)
   (eval trusted module-namespace)
   (dynamic-require `',(cadr trusted) 'data))

Full disclosure: I don't know how to do better. I figured that this
wasn't the best way to do it to begin with because I get the following
error under `raco test`'s runtime configuration.

define-values: assignment disallowed;
  cannot re-define a constant
   constant: lifted/4
   in module:'pkgdef1620873248

I don't know what "lifted/4" is.

If I am bringing in module forms that I trust, what's the best way to
dynamically evaluate them and pull out a provided value without running
into that error?

--

~slg


-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/e0a048c8-77ac-5126-5bef-226ecbe906dc%40sagegerard.com.


Re: [racket-users] Is there a good Racket DSL alternative to Image Magick?

2021-05-12 Thread kamist...@gmail.com
p.s: scale-to-fit might be interesting for you and if you don't care about 
extra options (for background/alpha etc.) you can construct the pict with 
(bitmap path) directly instead of (bitmap (make-object bitmap% ...))
p.p.s: I think there was also a way to convert/use images within picts or 
the other way around but I currently can't find it...  ...I did find it: 
pict/convert
a little bit more example code options / some might be useful for you or 
other people (I partly write this as a note to myself)

#lang racket

(require (only-in 2htdp/image circle)
 (except-in pict circle)
 pict/convert)

(module+ main
  (define c (circle 708 "solid" "blue"))
  (displayln "pict-convertible:")
  (pict-convertible? c)
  (define p (pict-convert c))
  (define p2 (frame (scale-to-fit p 1440 (pict-height p) #:mode 'inset)))
  (send (pict->bitmap p2) save-file "example.png" 'png)
  (send (pict->bitmap p) save-file "test.png" 'png)
  
  (make-directory* "./resized")
  (define resize (resizer "." "./resized" 1440))
  (resize 708 1100 "test.png"))

(define ((resizer source target max-width) width height filename)
  (define p (bitmap (build-path source filename)))
  ;; (pict-width p) (pict-height p) would give the dimmensions of the 
incoming image
  ;; maybe that could be used instead of the hardcoded arguments, if that 
is possible for the usecase
  
  ;; this is a pict based variant with scaling (maybe you don't need 
scaling)
  ;; maybe you need something a little bit different
  ;; maybe scale-to-fit with certain parameters may be enough, or you can 
keep using your 2htdp/image
  ;; version and you could convert it to a pict with pict-convert if you 
want to use pict or bitmap% functionality
  (define p2
(cc-superimpose (filled-rectangle max-width height #:color "white" 
#:draw-border? #f)
(scale-to-fit p (- max-width width) height #:mode 
'inset)))
  (send (pict->bitmap p2)
save-file (build-path target filename) 'png))

;; -
;; everything below this is completely optional and I only do it because I 
wanted to try it
;; -
;; because this contract is defined on the module boundary it won't work 
within this module
;; [define/contract could be used to define it on the function boundary]
;; we could use a ->i contract to specify that width has to be <= max-width
(require racket/contract)
(define pathish/c (or/c path-string? path-for-some-system? 'up 'same))
(provide
 (contract-out
  [resizer (->i ([source pathish/c]
 [target pathish/c]
 [max-width exact-nonnegative-integer?])
[result (max-width) (->i ([width (and/c 
exact-nonnegative-integer? (<=/c max-width))]
  [height 
exact-nonnegative-integer?]
  [filename string?])
 [result () boolean?])])]))

(module* contract-test racket
  (require (submod "..")) ;; require enclosing module

  ;; testing the contract from a submodule similar to requiring from a 
different file
  (define resize (resizer "." "./resized" 1440))
  (resize 708 1100 "test.png") ;; works
  (resize 1708 1100 "test.png")) ;; contract error

;; to execute contract-test submodule use this in drracket-repl:
;; (require (submod "." contract-test))


kamist...@gmail.com schrieb am Donnerstag, 13. Mai 2021 um 01:04:41 UTC+2:

> Personally I use pict and racket/draw instead of htdp/image, because 
> save-image is png only.
> Pict to transform or load the image, racket/draw's bitmap% to save and/or 
> load the image
> (sidenote: in general I find pict more pleasant to work with, but that may 
> be subjective):
>
> (require pict
>  racket/draw)
>
> ;; this is a rough sketch of the image functions I use
>
> ;; make-object 2nd variant: 
> https://docs.racket-lang.org/draw/bitmap_.html?q=bitmap%25#%28constructor._%28%28lib._racket%2Fdraw..rkt%29._bitmap~25%29%29
> (define b (make-object bitmap% filename-or-port image-type-see-docs #f 
> #t)) ;; background color #f, complain on failure #t
> (define p (bitmap b)) ;; use bitmap% as pict
> ;; use pict functions to transform pict
> (define p2 (scale-to-fit p ...))
> ;; other pict transforms
> (define p3 (inset p2 30 0))
> ;; saving the transformed file
> (send (pict->bitmap p3) save-file out 'png) ;; other file types possible
>
> roberthh...@gmail.com schrieb am Mittwoch, 12. Mai 2021 um 15:06:02 UTC+2:
>
>> Dan, that's awesome. Thank you.
>>
>> Martin, I would love some way to extract the image metadata from each of 
>> the files outputted by Dan's function. All my function does is take the 
>> current width and height and apply some math to return some new values, so 
>> right now I'm just manually going into finder, looking at the images width 
>> and height, and plugging those values into my function. 
>>
>> On Tuesday, May 11, 2021

Re: [racket-users] Is there a good Racket DSL alternative to Image Magick?

2021-05-12 Thread Ben Greenman
On 5/12/21, Robert Haisfield  wrote:
> Daniel, that's awesome. How would I filter down this list according to the
> regex?
>
> (define list-of-files (map path->string (directory-list starting-path)))

You can wrap it in a filter:

  (define list-of-files (filter (lambda (str) (regexp-match? "\\.png$"
str)) (map path->string (directory-list starting-path


You might also like the glob package:

  (require file/glob)
  (define list-of-files (map path->string (glob (build-path
starting-path "*.png"

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAFUu9R76maG1O1Z40UL0CUwnMHZ3vg0H9mmE_BvVgozKGN2b1w%40mail.gmail.com.


Re: [racket-users] Redirecting shared lib stdout

2021-05-12 Thread Jon Zeppieri
Can you use BinaryenModuleWriteText instead? It looks like it was
added to address your use case. -J

On Wed, May 12, 2021 at 3:49 AM Paulo Matos  wrote:
>
> Hi,
>
> I have a shared library for which I am creating some bindings:
> https://github.com/pmatos/racket-binaryen
>
> There's a function BinaryenModulePrint that prints a WebAssembly module
> to stdout.
>
> When I wrap it in racket, if I do something like:
> (define mod ...)
> (with-output-to-string (lambda () (BinaryenModulePrint mod)))
>
> The return value will be "" and it will still print the module to
> stdout. I understand! However, I don't know how to solve it in Racket.
>
> In C, things seem easier because I have access to dup2 and pipe in case
> I need to redirect things, however in Racket all my attempts have
> failed.
>
> I have created the simple example:
> ```hello.c
> #include 
>
> void hello(void) {
>   printf("hello world!\n");
> }
> ```
>
> Compile with `gcc -shared -o hello.so hello.c`.
>
> Then:
> ```hello.rkt
> #lang racket/base
>
> (require racket/port
>  ffi/unsafe
>  ffi/unsafe/define)
>
> (define libhello (ffi-lib "/home/pmatos/dev/tmp/ffi-hello-world/hello.so"))
> (define-ffi-definer define-hello libhello)
> (define-hello hello (_fun -> _void))
> (with-output-to-string hello)
> ```
>
> Here's the issue! In C, I can do something like:
> ```hello-fix.c
> #include 
> #include 
> #include 
> #include 
>
> extern void hello(void);
>
> int main(void) {
>   int filefd = open("test.txt", O_WRONLY|O_CREAT, 0666);
>   dup2(filefd, fileno(stdout));
>   hello();
>
>   return 0;
> }
> ```
> Compile with `gcc -o hello hello.c hello-fix.c`
>
> This will, send the output of hello() to a file. Now, in racket
> preferably I want to connect what is sent to raw stdout, fd 1, to
> current-output-port.
>
> My thought was that I could create a dup2 ffi binding, and use
> unsafe-port->file-descriptor to install the racket pipe file descriptor
> in stdout using the dup2 ffi binding but it doesn't work.
>
> And it doesn't work because the unsafe-port->file-descriptor returns #f,
> even though port-waiting-peer? return #f as well (which sort of hints
> that maybe the documentation of unsafe-port->file-descriptor is
> incomplete.
>
> The start of my attempt would look like this:
>
> ```hello.rkt
> #lang racket/base
>
> (require racket/port
>  ffi/unsafe
>  ffi/unsafe/define
>  ffi/unsafe/port)
>
> (define libhello (ffi-lib "/home/pmatos/dev/tmp/ffi-hello-world/hello.so"))
>
> (define-ffi-definer define-libc (ffi-lib #f))
> (define-ffi-definer define-hello libhello)
>
> (define-libc dup2 (_fun _int _int -> _int))
> (define-hello hello (_fun -> _void))
>
> (define-values (in out) (make-pipe))
>
> ;; FAILS because (unsafe-port-> ...) return #f
> (dup2 (unsafe-port->file-descriptor out) 1)
>
> ;;...
> ;; connect the other end of the port to current-output-port
> ;; and use a thread to copy-port
> ```
>
> Surely there must be a better way than this and I would be surprised if
> there isn't already something out there in a library but I haven't found
> anything yet. Any help here would be great.
>
> Thanks,
>
> --
> Paulo Matos
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/87r1icxuhi.fsf%40linki.tools.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAKfDxxzGAaw3d4-0dc0q%2BPqDyg%2Boh-Nm7bitQ3g%3DyqsUtuqLOw%40mail.gmail.com.


Re: [racket-users] Is there a good Racket DSL alternative to Image Magick?

2021-05-12 Thread Robert Haisfield
Daniel, that's awesome. How would I filter down this list according to the 
regex?

(define list-of-files (map path->string (directory-list starting-path)))

On Tuesday, May 11, 2021 at 6:57:03 PM UTC-6 daniel@gmail.com wrote:

> Hi Robert
>
>
> > Even better if I could point the function at a directory, it finds all 
> of the images in it, and creates the data structure for me. Any ideas?
>
> You can get the files in a directory using *directory-list *and filter 
> for the image files using a regex.
>
> (for ([f (directory-list "/some/path")] #:when (regexp-match? "\\.png$" f))
>  (displayln f))
>
> Dan
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/60a95d69-a124-4454-bb70-20f8ac04e6f9n%40googlegroups.com.


Re: [racket-users] Is there a good Racket DSL alternative to Image Magick?

2021-05-12 Thread kamist...@gmail.com
Personally I use pict and racket/draw instead of htdp/image, because 
save-image is png only.
Pict to transform or load the image, racket/draw's bitmap% to save and/or 
load the image
(sidenote: in general I find pict more pleasant to work with, but that may 
be subjective):

(require pict
 racket/draw)

;; this is a rough sketch of the image functions I use

;; make-object 2nd variant: 
https://docs.racket-lang.org/draw/bitmap_.html?q=bitmap%25#%28constructor._%28%28lib._racket%2Fdraw..rkt%29._bitmap~25%29%29
(define b (make-object bitmap% filename-or-port image-type-see-docs #f #t)) 
;; background color #f, complain on failure #t
(define p (bitmap b)) ;; use bitmap% as pict
;; use pict functions to transform pict
(define p2 (scale-to-fit p ...))
;; other pict transforms
(define p3 (inset p2 30 0))
;; saving the transformed file
(send (pict->bitmap p3) save-file out 'png) ;; other file types possible

roberthh...@gmail.com schrieb am Mittwoch, 12. Mai 2021 um 15:06:02 UTC+2:

> Dan, that's awesome. Thank you.
>
> Martin, I would love some way to extract the image metadata from each of 
> the files outputted by Dan's function. All my function does is take the 
> current width and height and apply some math to return some new values, so 
> right now I'm just manually going into finder, looking at the images width 
> and height, and plugging those values into my function. 
>
> On Tuesday, May 11, 2021 at 7:31:15 PM UTC-6 martin...@gmail.com wrote:
>
>> So to clarify, you want something like the following (pseudocode)?
>>
>> (define (process-image transform image-file-data)
>>(let* ([path (get-full-path image-file-data)]
>>   [image (bitmap/file path)]
>>   [new-image (transform image)])
>> (save-image new-image path))
>>
>> (define (process-file path)
>>   (let ([image-file-data (extract-image-metadata path)])
>>  (process-image resize image-file-data)))
>>
>> (map process-file (find-all-images directory))
>>
>> One question - where do the new width and height of each image come from, 
>> if you are reading the list of filenames from a directory?
>>
>> martin
>>  
>>
>> On Tue, May 11, 2021 at 3:42 PM Robert Haisfield  
>> wrote:
>>
>>> Okay, so I figured out how to do what I want on individual photos using 
>>> bitmap/file and 2htdp/image, but I'm trying to find a more programmatic way 
>>> to do it. Basically, I'd love to create a data structure that feeds into 
>>> the file path, width, and height into the image-resizer function. Even 
>>> better if I could point the function at a directory, it finds all of the 
>>> images in it, and creates the data structure for me. Any ideas?
>>>
>>> *No, I'm not trying to just resize photos (this would be much easier if 
>>> that's all I had to do), the thing I'm working with requires me to add 
>>> whitespace to the sides of the images to have them display properly.*
>>>
>>> [image: CleanShot 2021-05-11 at 16.31...@2x.png]
>>>
>>> On Tuesday, May 11, 2021 at 3:06:20 PM UTC-6 Jens Axel Søgaard wrote:
>>>
 Hi Robert,

 There are some bindings for Magick in the example folder for the FFI 
 library.


 https://github.com/racket/racket/tree/master/pkgs/racket-doc/ffi/examples

 The bindings are in magic.rkt and examples of use are in use-magick.rkt 
 (see the bottom).

 /Jens Axel


 Den tir. 11. maj 2021 kl. 22.15 skrev 'John Clements' via Racket Users <
 racket...@googlegroups.com>:

> Racket has the ability to read a variety of different image files. I 
> would go first to 2htdp/image’s “bitmap/file” to read images. 
> “save-image” 
> can write images (but only as png files). I believe there are also an 
> array 
> of lower-level image manipulation functions that are likely to have a 
> less 
> friendly interface :).
>
> Apologies in advance for any misleading information I might be 
> providing….
>
> John
>
> > On May 11, 2021, at 1:09 PM, Robert Haisfield  
> wrote:
> > 
> > Alternatively, does the normal images function for Racket work? When 
> I was looking through the documentation I saw a lot about creating shapes 
> but not about using image files.
> > 
> > On Tuesday, May 11, 2021 at 2:03:33 PM UTC-6 Robert Haisfield wrote:
> > Hmm does the video language work for image files? If so, then I 
> think it might work.
> > 
> > On Tuesday, May 11, 2021 at 9:03:35 AM UTC-6 Sage Gerard wrote:
> > I hope that has what Robert is looking for, but I don't recognize 
> that speech. In fact, I have a false memory, because I'm not finding the 
> speech I'm looking for on https://con.racket-lang.org/2018/#speakers
> > 
> > On 5/11/21 10:19 AM, Bruce O'Neel wrote:
> >> This might be it.
> >> 
> >> (seventh RacketCon): Leif Andersen -- Movies as Programs: The Story 
> of a Racket - YouTube
> >> 
> >> 
> >> 
> >

[racket-users] Error in DrRacket "Couldn't find a browser"

2021-05-12 Thread Chae Cramb
I'm new to Racket and have just started working through the "How to Code" 
course on edX. When I right-click on a primitive in DrRacket and try to 
search for it in the Help Desk I get the following error: 

send-url: Couldn't find a browser to open URL: 
"file:///C:/Users/chaec/AppData/Local/Temp/plt-sendurl-contents-file-16208383081620838308452.html"
  context...:
   C:\Program Files\Racket\share\pkgs\net-lib\net\sendurl.rkt:98:0: send-url
   [repeats 1 more time]
   C:\Program 
Files\Racket\share\pkgs\gui-lib\mred\private\mrmenu.rkt:250:14: command 
method in basic-selectable-menu-item%
   C:\Program 
Files\Racket\share\pkgs\gui-lib\mred\private\mrpopup.rkt:49:38: go
   C:\Program 
Files\Racket\share\pkgs\gui-lib\mred\private\wx\common\queue.rkt:435:6
   C:\Program 
Files\Racket\share\pkgs\gui-lib\mred\private\wx\common\queue.rkt:486:32
   C:\Program 
Files\Racket\share\pkgs\gui-lib\mred\private\wx\common\queue.rkt:634:3

I'm on Windows 10 Home 21H1.

Can anyone offer any advice?

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/adf0f62c-dca2-46e5-8d97-5b65b8155cc1n%40googlegroups.com.


Re: [racket-users] Redirecting shared lib stdout

2021-05-12 Thread Sage Gerard
I ran into this issue with rsound. I'm not sure how standard output can
be directly captured from a lower-level language in a Racket context
when that language can freely ignore the Racket printer and write
directly to STDOUT within the same operating system process.

I'd hate to just add a "me too", but the only way quick way I could
think to handle that was to add another subprocess. If I were wanting to
solve the problem in one process, I'd probably extend Racket at the C
level and integrate my output with ports.

https://docs.racket-lang.org/inside/Ports_and_the_Filesystem.html?q=c%20api

On 5/12/21 3:48 AM, Paulo Matos wrote:
> Hi,
>
> I have a shared library for which I am creating some bindings:
> https://github.com/pmatos/racket-binaryen
>
> There's a function BinaryenModulePrint that prints a WebAssembly module
> to stdout.
>
> When I wrap it in racket, if I do something like:
> (define mod ...)
> (with-output-to-string (lambda () (BinaryenModulePrint mod)))
>
> The return value will be "" and it will still print the module to
> stdout. I understand! However, I don't know how to solve it in Racket.
>
> In C, things seem easier because I have access to dup2 and pipe in case
> I need to redirect things, however in Racket all my attempts have
> failed.
>
> I have created the simple example:
> ```hello.c
> #include 
>
> void hello(void) {
>printf("hello world!\n");
> }
> ```
>
> Compile with `gcc -shared -o hello.so hello.c`.
>
> Then:
> ```hello.rkt
> #lang racket/base
>
> (require racket/port
>   ffi/unsafe
>   ffi/unsafe/define)
>
> (define libhello (ffi-lib "/home/pmatos/dev/tmp/ffi-hello-world/hello.so"))
> (define-ffi-definer define-hello libhello)
> (define-hello hello (_fun -> _void))
> (with-output-to-string hello)
> ```
>
> Here's the issue! In C, I can do something like:
> ```hello-fix.c
> #include 
> #include 
> #include 
> #include 
>
> extern void hello(void);
>
> int main(void) {
>int filefd = open("test.txt", O_WRONLY|O_CREAT, 0666);
>dup2(filefd, fileno(stdout));
>hello();
>
>return 0;
> }
> ```
> Compile with `gcc -o hello hello.c hello-fix.c`
>
> This will, send the output of hello() to a file. Now, in racket
> preferably I want to connect what is sent to raw stdout, fd 1, to
> current-output-port.
>
> My thought was that I could create a dup2 ffi binding, and use
> unsafe-port->file-descriptor to install the racket pipe file descriptor
> in stdout using the dup2 ffi binding but it doesn't work.
>
> And it doesn't work because the unsafe-port->file-descriptor returns #f,
> even though port-waiting-peer? return #f as well (which sort of hints
> that maybe the documentation of unsafe-port->file-descriptor is
> incomplete.
>
> The start of my attempt would look like this:
>
> ```hello.rkt
> #lang racket/base
>
> (require racket/port
>   ffi/unsafe
>   ffi/unsafe/define
>   ffi/unsafe/port)
>
> (define libhello (ffi-lib "/home/pmatos/dev/tmp/ffi-hello-world/hello.so"))
>
> (define-ffi-definer define-libc (ffi-lib #f))
> (define-ffi-definer define-hello libhello)
>
> (define-libc dup2 (_fun _int _int -> _int))
> (define-hello hello (_fun -> _void))
>
> (define-values (in out) (make-pipe))
>
> ;; FAILS because (unsafe-port-> ...) return #f
> (dup2 (unsafe-port->file-descriptor out) 1)
>
> ;;...
> ;; connect the other end of the port to current-output-port
> ;; and use a thread to copy-port
> ```
>
> Surely there must be a better way than this and I would be surprised if
> there isn't already something out there in a library but I haven't found
> anything yet. Any help here would be great.
>
> Thanks,
>
> --
> Paulo Matos
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/87r1icxuhi.fsf%40linki.tools.

--
~slg


-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/1abd8d17-a53c-b7fa-8983-57deec760e96%40sagegerard.com.


Re: [racket-users] Injecting local contracts for prefab constructors

2021-05-12 Thread David Storrs
If you're willing to accept a low tech solution, might I suggest this:

$ perl -i.bak -lpe 's/\(foo/\(make-foo/g' *.rkt


Also, I'll self-plug and point you towards this:

#lang racket
(require struct-plus-plus )

(struct++ foo ([a real?]) (#:omit-reflection) #:prefab)

(define checked (foo++ #:a 7)) ; contract is checked

(define not-checked (foo 7)) ; contract is not checked

(writeln checked)
(writeln not-checked)

This defines the contracts using define/contract.  It's not clear to me if
that meets your needs, but hopefully it does.

On Mon, May 10, 2021 at 10:03 AM Sage Gerard  wrote:

> I hope so! That's what I understood option B to mean.
>
> I could adjust the define-message macro according to the info in Ryan and
> Phillip's attachments, but I'm not well-educated on trapping struct
> operations. Phillip: Your example and email gives me the impression that
> when you add a chaperone, you can proxy all struct operations, including
> the gap left by the chaperone for the constructor. Is that true?
>
> The macro I'd want has to preserve *all* characteristics of *all*
> generated struct bindings, which makes things tricky since the options for
> `struct` normally operate on a strict subset. e.g.
> #:[extra-]constructor-name seems to operate on the constructor alone, and
> the #:extra- variant does not preserve the match-expander/super-id
> characteristics. #:constructor-name is closer, but the original constructor
> id is no longer directly callable by the declaring module.
> On 5/10/21 7:05 AM, Hendrik Boom wrote:
>
> On Sun, May 09, 2021 at 10:23:34PM +, Sage Gerard wrote:
>
> I have a project with 57 prefab structure types. I need to construct 
> instances using a local contract (module level contracts do not fit my needs 
> here). Since I cannot define guards, the solution is easy enough.
>
> (struct foo (num) #:prefab)
> (define/contract make-foo (-> real? foo?) foo)
>
> Problem: I already have a few hundred constructor calls without
> contracts. I could either A) rewrite them all to use contracted
> constructors, or B) attach local contracts in a sweet spot so that I
> don't have to rewrite anything else.
>
> Is there any chance you could do the "rewriting" in a macro so you
> wouldn't actually have to change the few hundred constructor calls
> when you use option A)?
>
> -- hendrik
>
>
> I prefer option B, but it doesn't look like I can attach a local contract to 
> a constructor with `struct` alone, or even with an impersonator. When I hack 
> around to rebind or hide the constructor's identifier, I break compatibility 
> with `match` and `defstruct*`.
>
> If you were in my position, what would you do?
>
> --
>
> ~slg
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/0a16cfbe-4789-a939-796e-5f6f9da21626%40sagegerard.com.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/20210510110509.cpg6jnil6th7xbsi%40topoi.pooq.com.
>
> --
> ~slg
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/94e81fc9-e8a1-e677-bf0e-b49e597313b9%40sagegerard.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAE8gKofQDbSrOS1n-i7B-T0h78x%2Bz0NRdUkZx6-1T9FyxBspBQ%40mail.gmail.com.


Re: [racket-users] Is there a good Racket DSL alternative to Image Magick?

2021-05-12 Thread Robert Haisfield
Dan, that's awesome. Thank you.

Martin, I would love some way to extract the image metadata from each of 
the files outputted by Dan's function. All my function does is take the 
current width and height and apply some math to return some new values, so 
right now I'm just manually going into finder, looking at the images width 
and height, and plugging those values into my function. 

On Tuesday, May 11, 2021 at 7:31:15 PM UTC-6 martin...@gmail.com wrote:

> So to clarify, you want something like the following (pseudocode)?
>
> (define (process-image transform image-file-data)
>(let* ([path (get-full-path image-file-data)]
>   [image (bitmap/file path)]
>   [new-image (transform image)])
> (save-image new-image path))
>
> (define (process-file path)
>   (let ([image-file-data (extract-image-metadata path)])
>  (process-image resize image-file-data)))
>
> (map process-file (find-all-images directory))
>
> One question - where do the new width and height of each image come from, 
> if you are reading the list of filenames from a directory?
>
> martin
>  
>
> On Tue, May 11, 2021 at 3:42 PM Robert Haisfield  
> wrote:
>
>> Okay, so I figured out how to do what I want on individual photos using 
>> bitmap/file and 2htdp/image, but I'm trying to find a more programmatic way 
>> to do it. Basically, I'd love to create a data structure that feeds into 
>> the file path, width, and height into the image-resizer function. Even 
>> better if I could point the function at a directory, it finds all of the 
>> images in it, and creates the data structure for me. Any ideas?
>>
>> *No, I'm not trying to just resize photos (this would be much easier if 
>> that's all I had to do), the thing I'm working with requires me to add 
>> whitespace to the sides of the images to have them display properly.*
>>
>> [image: CleanShot 2021-05-11 at 16.31...@2x.png]
>>
>> On Tuesday, May 11, 2021 at 3:06:20 PM UTC-6 Jens Axel Søgaard wrote:
>>
>>> Hi Robert,
>>>
>>> There are some bindings for Magick in the example folder for the FFI 
>>> library.
>>>
>>> https://github.com/racket/racket/tree/master/pkgs/racket-doc/ffi/examples
>>>
>>> The bindings are in magic.rkt and examples of use are in use-magick.rkt 
>>> (see the bottom).
>>>
>>> /Jens Axel
>>>
>>>
>>> Den tir. 11. maj 2021 kl. 22.15 skrev 'John Clements' via Racket Users <
>>> racket...@googlegroups.com>:
>>>
 Racket has the ability to read a variety of different image files. I 
 would go first to 2htdp/image’s “bitmap/file” to read images. “save-image” 
 can write images (but only as png files). I believe there are also an 
 array 
 of lower-level image manipulation functions that are likely to have a less 
 friendly interface :).

 Apologies in advance for any misleading information I might be 
 providing….

 John

 > On May 11, 2021, at 1:09 PM, Robert Haisfield  
 wrote:
 > 
 > Alternatively, does the normal images function for Racket work? When 
 I was looking through the documentation I saw a lot about creating shapes 
 but not about using image files.
 > 
 > On Tuesday, May 11, 2021 at 2:03:33 PM UTC-6 Robert Haisfield wrote:
 > Hmm does the video language work for image files? If so, then I think 
 it might work.
 > 
 > On Tuesday, May 11, 2021 at 9:03:35 AM UTC-6 Sage Gerard wrote:
 > I hope that has what Robert is looking for, but I don't recognize 
 that speech. In fact, I have a false memory, because I'm not finding the 
 speech I'm looking for on https://con.racket-lang.org/2018/#speakers
 > 
 > On 5/11/21 10:19 AM, Bruce O'Neel wrote:
 >> This might be it.
 >> 
 >> (seventh RacketCon): Leif Andersen -- Movies as Programs: The Story 
 of a Racket - YouTube
 >> 
 >> 
 >> 
 >> 
 >> 
 >> 11 May 2021 15:30 Sage Gerard  wrote:
 >> I don't know of one off hand, but I believe RacketCon 2018 (?) 
 included a presenter that showed a PostScript-like DSL for designers and 
 artists.  If pict not cover your needs, maybe dig into the presentations?
 >> 
 >> Failing that, can you show what you'd hope the syntax would look 
 like? That would probably help point you in the right direction,
 >> 
 >> On 5/11/21 9:26 AM, Robert Haisfield wrote:
 >>> I have to do a bunch of .jpg and .png image resizings and was 
 looking for a programmatic way to do it. Is their a Racket DSL for this? --
 >>> You received this message because you are subscribed to the Google 
 Groups "Racket Users" group.
 >>> To unsubscribe from this group and stop receiving emails from it, 
 send an email to racket-users...@googlegroups.com.
 >>> To view this discussion on the web visit 
 https://groups.google.com/d/msgid/racket-users/0d576c32-7d4d-4944-9cbc-c12f04406fccn%40googlegroups.com
 .
 >> -- ~slg --
 >> You received this message because y

[racket-users] Redirecting shared lib stdout

2021-05-12 Thread Paulo Matos
Hi,

I have a shared library for which I am creating some bindings:
https://github.com/pmatos/racket-binaryen

There's a function BinaryenModulePrint that prints a WebAssembly module
to stdout.

When I wrap it in racket, if I do something like:
(define mod ...)
(with-output-to-string (lambda () (BinaryenModulePrint mod)))

The return value will be "" and it will still print the module to
stdout. I understand! However, I don't know how to solve it in Racket.

In C, things seem easier because I have access to dup2 and pipe in case
I need to redirect things, however in Racket all my attempts have
failed.

I have created the simple example:
```hello.c
#include 

void hello(void) {
  printf("hello world!\n");
}
```

Compile with `gcc -shared -o hello.so hello.c`.

Then:
```hello.rkt
#lang racket/base

(require racket/port
 ffi/unsafe
 ffi/unsafe/define)

(define libhello (ffi-lib "/home/pmatos/dev/tmp/ffi-hello-world/hello.so"))
(define-ffi-definer define-hello libhello)
(define-hello hello (_fun -> _void))
(with-output-to-string hello)
```

Here's the issue! In C, I can do something like:
```hello-fix.c
#include 
#include 
#include 
#include 

extern void hello(void);

int main(void) {
  int filefd = open("test.txt", O_WRONLY|O_CREAT, 0666);
  dup2(filefd, fileno(stdout));
  hello();
  
  return 0;
}
```
Compile with `gcc -o hello hello.c hello-fix.c`

This will, send the output of hello() to a file. Now, in racket
preferably I want to connect what is sent to raw stdout, fd 1, to
current-output-port.

My thought was that I could create a dup2 ffi binding, and use
unsafe-port->file-descriptor to install the racket pipe file descriptor
in stdout using the dup2 ffi binding but it doesn't work.

And it doesn't work because the unsafe-port->file-descriptor returns #f,
even though port-waiting-peer? return #f as well (which sort of hints
that maybe the documentation of unsafe-port->file-descriptor is
incomplete.

The start of my attempt would look like this:

```hello.rkt
#lang racket/base

(require racket/port
 ffi/unsafe
 ffi/unsafe/define
 ffi/unsafe/port)

(define libhello (ffi-lib "/home/pmatos/dev/tmp/ffi-hello-world/hello.so"))

(define-ffi-definer define-libc (ffi-lib #f))
(define-ffi-definer define-hello libhello)

(define-libc dup2 (_fun _int _int -> _int))
(define-hello hello (_fun -> _void))

(define-values (in out) (make-pipe))

;; FAILS because (unsafe-port-> ...) return #f
(dup2 (unsafe-port->file-descriptor out) 1)

;;...
;; connect the other end of the port to current-output-port
;; and use a thread to copy-port 
```

Surely there must be a better way than this and I would be surprised if
there isn't already something out there in a library but I haven't found
anything yet. Any help here would be great.

Thanks,

-- 
Paulo Matos

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/87r1icxuhi.fsf%40linki.tools.