capfredf
2021-7-24 14:28:44

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


soegaard2
2021-7-24 15:12:49

shu--hung
2021-7-24 15:17:45

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


soegaard2
2021-7-24 15:20:51

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"])


capfredf
2021-7-24 15:23:33

Thanks!


soegaard2
2021-7-24 15:24:46

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


sarna.dev
2021-7-24 15:31:21

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


mflatt
2021-7-24 15:45:41

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


mflatt
2021-7-24 15:46:03

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


mflatt
2021-7-24 15:46:21

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


mflatt
2021-7-24 15:49:06

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


mflatt
2021-7-24 15:50:50

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


sarna.dev
2021-7-24 16:00:11

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


sarna.dev
2021-7-24 16:00:44

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


sarna.dev
2021-7-24 16:02:27

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


sarna.dev
2021-7-24 16:04:59

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))


sarna.dev
2021-7-24 16:39:20

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


samdphillips
2021-7-24 17:02:59

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


sarna.dev
2021-7-24 17:05:55

@samdphillips this one works! thank you :pray:


sarna.dev
2021-7-24 17:06:09

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


samdphillips
2021-7-24 17:07:16

They might be checking a magic number? No idea


sarna.dev
2021-7-24 17:20:13

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


sarna.dev
2021-7-24 17:23:47

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


samdphillips
2021-7-24 17:27:50

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


sarna.dev
2021-7-24 17:30:14

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


sarna.dev
2021-7-24 17:33:20

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


samdphillips
2021-7-24 17:44:35

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! >


sarna.dev
2021-7-24 17:45:12

let me check again!


samdphillips
2021-7-24 17:46:15

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


sarna.dev
2021-7-24 17:46:42

it cuts the output for me


sarna.dev
2021-7-24 17:52:10

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)


samdphillips
2021-7-24 17:53:15

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


sarna.dev
2021-7-24 17:55:01

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


sschwarzer
2021-7-24 18:04:52

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/ .


sschwarzer
2021-7-24 19:31:54

notjack
2021-7-24 21:58:58

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


notjack
2021-7-24 22:01:09

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


samth
2021-7-24 23:46:46

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


shu--hung
2021-7-25 02:00:35

It does but I never thought of it.


sorawee
2021-7-25 02:18:26

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


sorawee
2021-7-25 02:18:44

Cause match expects cons by symbol name, not by binding