stchang / generic-bind Goto Github PK
View Code? Open in Web Editor NEWGeneric bind forms for Racket
Generic bind forms for Racket
The following example illustrates the problem:
#lang racket
(require racket/generator generic-bind)
(define (gen1 seq)
(in-generator
(let ([refs (make-hash)])
(for ([seq-item seq])
(match-let ([(cons a b) seq-item])
(hash-set! refs a b)
(yield a))))))
(displayln (sequence->list (gen1 (list (cons 'a 'b) (cons 'c 'd)))))
(define (gen2 seq)
(in-generator
(let ([refs (make-hash)])
(~for ([($: a b) seq])
(hash-set! refs a b)
(yield a)))))
(displayln (sequence->list (gen2 (list (cons 'a 'b) (cons 'c 'd)))))
Output (with errortrace enabled):
(a c)
result arity mismatch;
expected number of values not received
expected: 1
received: 0
values...:
errortrace...:
/Users/hinsen/projects/racket/gbind.rkt:18:7: (yield a)
/Users/hinsen/projects/racket/gbind.rkt:15:3: (let-values (((refs) (make-hash))) (let-values (((temp14) (let-values (....) ....))) (void temp14)))
/Users/hinsen/projects/racket/gbind.rkt:23:11: (sequence->list (gen2 (list (cons (quote a) (quote ....)) (cons (quote ....) (....)))))
/Users/hinsen/projects/racket/gbind.rkt:23:0: (displayln (sequence->list (gen2 (list (cons (....) ....) (cons ....)))))
/Users/hinsen/projects/racket/gbind.rkt:23:0: (call-with-values (lambda () (displayln (sequence->list (gen2 ....)))) print-values)
context...:
g18
generator
/Users/hinsen/Applications/Racket/collects/racket/generator.rkt:93:4: cont
My Racket installation (including generic-bind) was up to date yesterday evening.
I like ~struct
, I like struct/contract
, and it's a shame to have to choose.
Given the other ones, I was surprised that ~for/foldr
isn't implemented. I'd like that, if you have the time.
(~for/list ([i 4] #:final (= i 2) [j 2]) (list i j))
=> '((0 0) (0 1) (1 0) (1 1) (2 0) (2 1))
(for/list ([i 4] #:final (= i 2) [j 2]) (list i j))
=> '((0 0) (0 1) (1 0) (1 1) (2 0))
Currently partially applied functions can't be defined with generic binding instances. For example, the following code gives a syntax error:
(~struct pos (x y))
(~define ((pos+ ($pos x1 y1)) ($pos x2 y2))
(pos (+ x1 x2) (+ y1 y2)))
The error claims x1
and y1
are unbound. However, partially applied functions can still be defined if you only use the generic binding patterns in the outermost arguments that aren't used for partial application:
(~define ((pos+ x1 y1) ($pos x2 y2))
(pos (+ x1 x2) (+ y1 y2)))
Would it be difficult to implement full support for partially applied functions?
The problem is that #:break only aborts the current inner loop and not multiple levels.
(~for*/list ([i 4] [j 2] #:break (= i 2)) (list i j))
=> '((0 0) (0 1) (1 0) (1 1) (3 0) (3 1))
When I updated it I noticed these warnings:
raco setup: --- building documentation ---
raco setup: 1 running: /generic-bind/generic-bind/generic-bind.scrbl
raco setup: WARNING: undefined tag in /generic-bind/generic-bind/generic-bind.scrbl:
raco setup: ((lib "generic-bind/main.rkt") $)
raco setup: ((lib "generic-bind/main.rkt") $:)
raco setup: ((lib "generic-bind/main.rkt") $list)
...
raco setup: ((lib "generic-bind/main.rkt") ⋈)
And then I noticed that if I search the racket documentation and click on the docs for generic-bind, it's rendered in the new style, but the links don't work, and they're underlined in red.
Welcome to DrRacket, version 5.90.0.9 [3m].
Language: racket; memory limit: 8192 MB.
(~for ([x 1]) (define y 1) #:break #f y)
. ~for/common: expected expression in: #:break
In the following code, the ~for_/fold use I would expect to have the same behavior as the for_/fold use. It instead lets unsafe-undefined escape.
(require generic-bind)
(struct Foo (x))
;; Good
(let ([c Foo])
(for_/fold ([out (set)])
([mumble (in-set (set (Foo 'hrmm)))])
(match mumble
[(and (Foo x) (app (λ _ c) c))
(set-add out (c x))])))
;; bad
(let ([c Foo])
(~for_/fold ([out (set)])
([($ (and (Foo x) (app (λ _ c) c)))
(in-set (set (Foo 'grumble)))])
(set-add out (c x))))
Currently defining functions with generic binding instances does not support partially applied functions. For example, the following code gives a syntax error:
(~struct pos (x y))
(~define ((pos+ ($pos x1 y1)) ($pos x2 y2))
(pos (+ x1 x2)
(+ y1 y2)))
The error claims x1
and y1
are unbound. Would this be exceptionally difficult to implement?
The call-with-values
in do-body
handles the cases when #:result is necessary.
For example something that would allow something like this:
> (~define ($stx (a b c)) #'(1 2 3))
> #'a
#<syntax 1>
I was thinking of something like this, which doesn't work anyway: (but it might be nice if it did)
(require generic-bind)
(define-match-bind list-no-order)
(~define (proc-rename . ($list-no-order (? procedure? proc) (? symbol? sym)))
(procedure-rename proc sym))
See pr #17
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.