Macro of the day: ; SYNTAX (store id ...)
; Make hashtable storing the values to which the ids are bound.
; The key is the symbol corresponding to the identifier.
(define-syntax (store stx)
(syntax-parse stx
[(_store id ...)
(syntax/loc stx
(make-hasheq (list (cons 'id id) ...)))]))
; SYNTAX (load ht (id ...))
; Define the ids use initial values stored in the hash table.
(define-syntax (load stx)
(syntax-parse stx
[(_store the-ht (id ...))
(syntax/loc stx
(begin
(define ht the-ht)
(define id (hash-ref ht 'id))
...))]))
;; Example
(define x 42)
(define y 'a)
(define info (store x y))
info ; => '#hasheq((x . 42) (y . a))
(let ()
(load info (x y))
(list x y))
; => '(42 a)
where’d you find that?
Wrote it myself as a quick hack.
You can simplify and make clearer with define-simple-macro
: #lang racket
(require syntax/parse/define)
; SYNTAX (store id ...)
; Make hashtable storing the values to which the ids are bound.
; The key is the symbol corresponding to the identifier.
(define-simple-macro (store id:id ...)
(make-hasheq (list (cons 'id id) ...)))
; SYNTAX (load ht (id ...))
; Define the ids use initial values stored in the hash table.
(define-simple-macro (load the-ht (id:id ...))
(begin
(define ht the-ht)
(define id (hash-ref ht 'id))
...))
;; Example
(define x 42)
(define y 'a)
(define info (store x y))
info ; => '#hasheq((x . 42) (y . a))
(let ()
(load info (x y))
(list x y))
; => '(42 a)
it kind of reminds me of a similar macro from Paul Graham’s On Lisp (my memory may be hazy)
It’s a good idea to add :id
. In general I am not too fond of define-simple-macro
. I like to see the actual syntax transformer.
I’m heading over to the RacketCon Gather space
@attilasedon1 has joined the channel
Sorry to ask a simple question, but I’m struggling to follow the documentation for racket/system. I want to use an external unix command. I have the input in a racket string, and I want to be able to read the output as a bytestring. How do I do it? I feel it must be obvious, but it is the use of ports I think which is tripping me up. Do I have to setup all the ports separately, create a process and then correctly handle the ports in and out of the process?
@rp that’s right — you need to take care of the ports. Your input string can be turned into an input port; standard error and standard out could be new ports. Perhaps you could take a look at subprocess instead of system?
if you want to reuse your current processes’s stdout/stderr, you can use current-output-port
and current-error-port
@jesse Thanks. And am I right in thinking that stdin is an output port whilst stdout and stderr are input ports? It’s kind of the wrong way round, but the right way round because stdout needs to be ready to get input from the process. Is that right?
that doesn’t sound right to me; stdin should be an input port, and the other two output ports
or do you need to switch things around somehow? (sorry if I’m getting you wrong)
I know I can do (with-output-to-string (lambda () (system “cmd”))), which allows me to get the output. However, that doesn’t help me put input in, so I think I need a full subprocess rather than using system (as you suggest).
Alright, I’ll fiddle with it more and see if I can work it out. Thanks for replying :slightly_smiling_face:
you might try using with-input-from-string
or call-with-input-string
to get your stdin set up
Oh! Can I use both with-output-to-string and with-input-from-string at the same time??
That would solve it for me. I’ll try that!
nice! good luck
Haha, it works. Its ugly (two nested lambda’s!) but it does seem to work. Thank you :slightly_smiling_face:
Does anyone have, or know of, code that uses a canvas% and handles mouse-events for freehand drawing?
IIRC, you just need to derive a class from canvas% and get the on-mouse events
Yeah, I’m just trying to not reinvent it if possible.
you should, in particular if the existing wheel is square :wink:
Your wish is me wasting time :stuck_out_tongue: https://gist.github.com/Metaxal/0f928d1b429aa36b8c6046a77c994973
(and yes, that’s a square wheel)
now has a color picker!
now with an undo button!
Now with a line-width slider! @samdphillips
Too much time on your hands :slightly_smiling_face:
Nice ‘Virtual Biergarten’ ! It was really good to see so many Racketeers! see you all tomorrow
There’s someone I know who would probably love to see this code. Thanks for spending the time on it. :)
Is there a contract like cons/c
but for streams?