
is it possible to define an alias to a match expander?


(define-syntax EQUALS (syntax-local-value #'==))

Packages as a macro: #lang racket
(require (for-syntax syntax/parse racket/syntax racket/base))
(require racket/match)
(define-syntax (define-match-alias stx)
(syntax-parse stx
[(_define-match-alias new old)
(syntax/loc stx
(define-syntax new (syntax-local-value #'old)))]))
(define-match-alias EQUALS ==)
(match 42
[(EQUALS 41) "foo"]
[(EQUALS 42) "bar"]
[_ "baz"])

Thanks!

I have only tested with ==
I expect a different solution is needed if your want, say, kons
to be an alias of cons
.

hey, I’m trying to use deflate and inflate from the standard library: 1. why doesn’t deflate print anything to stdout when I call it with (current-output-port)
? 2. are they regular inflate and deflate from zlib, or something else? they produce different results than Ruby functions

Saw your post on Discord, but I’ll answer here, because I like Slack threading a lot more…

When I try a program like the one you wrote there, I do get compressed output.

But writing compressed output to a terminal can do strange things, depending on the terminal. Have you already tried directing output to a file?

Compared to zlib, it’s the same compression algorithm. But if I remember correctly, zlib has a different header than pkzkip.

Or maybe it’s that deflate
in Racket produces “raw” output without a header.

sarna@ub:~/bin$ cat deflate
#! /usr/bin/env racket
#lang racket/base
(require file/gzip)
(define out-port (open-output-string))
(define-values (in out crc) (deflate (current-input-port) out-port))
(display (get-output-string out-port))
sarna@ub:~/bin$ echo "hello" \| ./deflate > deflated-by-racket
sarna@ub:~/bin$ echo "hello" \| ruby -r zlib -e "puts Zlib::Deflate.deflate(STDIN.read)" > deflated-by-ruby
sarna@ub:~/bin$ cat deflated-by-racket
�H����sarna@ub:~/bin$ cat deflated-by-ruby
x��H���K
sarna@ub:~/bin$ sha256sum deflated-by-racket
f3c485e8f752e758c920db0d8d350fc5db3531636744f2c56b2dab2e4a75911f deflated-by-racket
sarna@ub:~/bin$ sha256sum deflated-by-ruby
a6c93c480a9b40e707267b677e0dfe956a92178fbe7f716ea70b33a7dbd52f9d deflated-by-ruby

could it be something stupid, like newlines? the one generated by Ruby seems to have a newline

ah, puts
writes a newline. still, it’s after deflation, not before

by the way, I can’t inflate files that Ruby can: sarna@ub:~/src/git-simple-commit$ cat .git/objects/83/36a0c66d524e5db64999cd6b90a70015673b17 \| ruby -r zlib -e "puts Zlib::Inflate.inflate(STDIN.read)"
commit 178tree 88e38705fdbd3608cddbe904b67c731f3234c45b
author sarna <sarna.dev@protonmail.com> 1627137706 +0200
committer sarna <sarna.dev@protonmail.com> 1627137706 +0200
First commit.
sarna@ub:~/src/git-simple-commit$ cat .git/objects/83/36a0c66d524e5db64999cd6b90a70015673b17 \| inflate
inflate: error in compressed data
context...:
/usr/share/racket/collects/file/gunzip.rkt:217:0: inflate
body of "/home/sarna/bin/inflate"
sarna@ub:~/src/git-simple-commit$ which inflate
/home/sarna/bin/inflate
sarna@ub:~/src/git-simple-commit$ cat ~/bin/inflate
#! /usr/bin/env racket
#lang racket/base
(require file/gunzip
racket/trace)
(define out-port (open-output-string))
(inflate (current-input-port) out-port)
(displayln (get-output-string out-port))

gunzip-through-ports
gives gnu-unzip: bad header
.. :pensive:

I’m probably getting the details wrong, but I think git uses decompress which uses an adler checksum. There is an implementation in Racket but it is private. I extracted the code for a different project here: https://github.com/samdphillips/racket-minecraft/blob/main/nbt/decompress.rkt

@samdphillips this one works! thank you :pray:

any ideas why the Ruby one works out of the box though?

They might be checking a magic number? No idea

they use the window size from the header - https://rubyapi.org/3.0/o/zlib/inflate#method-c-new

oh, or maybe it’s not related? I’m so lost right now..

They may have an API over the zlib api that handles it transparently. AIUI the Racket compression routines are written in Racket.

yep, Racket ones are in Racket, Ruby is here though https://github.com/ruby/zlib/blob/master/ext/zlib/zlib.c

what about stuff not getting printed to stdout? I have to do the (define out-port (open-output-string))
(do-something out-port)
(displayln (get-output-string out-port))
dance

Works for me? (still some binary stuff in the stream there) racket
Welcome to Racket v8.1 [cs].
> ,req nbt/decompress.rkt
> (define fname ".git/objects/53/828c2536260e14dc45b2431172edc8d592a192")
> (zlib-inflate (open-input-file fname) (current-output-port))
tree 70100644 main.rkt{ZyrQȹ9\|V740000 privateo*\|(P;.
kP!
>

let me check again!

I tried with an commit object and it’s much nicer

it cuts the output for me

with current-input-port
: tree 74100644 hello.txt
with writing to open-input-string
and printing it afterwards: tree 74100644 hello.txt�6%
�۩�V�����FJ100644 world.txt�b��t+��$Y$ߙ+\�q
(sorry for the garbage bytes, I don’t have any other file big enough)

For binary data you should be using open-input-bytes

ah! it seemed to work fine, thanks for the suggestion though - will use it instead

For the record, at least for Posix I’d prefer piping output through hexdump
instead of printing the raw bytes to the terminal. Hexdump example: $ hexdump -C .git/objects/0e/55ea9762a3f1ee6f00bc84286902c7321dba6d
00000000 78 01 2b 29 4a 4d 55 30 31 36 63 30 34 30 30 33 \|x.+)JMU016c04003\|
00000010 31 51 d0 4b cf 2c 49 2c 29 29 ca 4c 2a 2d 49 2d \|1Q.K.,I,)).L*-I-\|
00000020 66 d8 9f e3 b8 6b e6 3c ab 25 d9 eb 67 e4 2d 6a \|f....k.<.%..g.-j\|
00000030 5c c5 57 ea fc af c4 c4 00 08 c0 0a 33 4a 93 18 \|\.W.........3J..\|
00000040 64 9a 72 35 df 07 a6 e4 79 72 15 af 91 9b b3 ec \|d.r5....yr......\|
...
If you want something “diffable”, see https://stackoverflow.com/questions/21713725/ .


beware that box-cas!
uses eq?
instead of equal?
to check if the current value is the same as the expected current value

I haven’t hit this issue in the wild yet but I suspect that can make retry loops run forever in some situations, like if the box has a chaperone attached and the value you give as the expected current value doesn’t have one

I would think that a rename transformer would work here, without needing any of these tricks, but maybe it doesn’t for some reason

It does but I never thought of it.

Rename transformer wouldn’t work for cons
though, right?

Cause match
expects cons
by symbol name, not by binding