[racket-users] Re: Some stuff about "case".

2020-06-03 Thread Jack Firth
Isn't that just match with the == pattern?

(let ([x 'foo])
  (match 'foo
[(== x) 'got-foo]
['x 'got-quote-x])) ; => 'got-foo

On Monday, June 1, 2020 at 11:54:01 AM UTC-7, cwebber wrote:
>
> As I started typing this email and looking into the definition of case, 
> I realized my assumptions are wrong. 
>
> What I needed: something like case which dispatches on symbols, except 
> not auto-quoting the arguments... I needed to evaluate them from the 
> lexical environment.  So: 
>
>   (let ([x 'foo]) 
> (caseish 'foo 
>   [(x) 'got-foo] 
>   [('x) 'got-quote-x]))  ; => 'got-foo 
>
> I figured: case is fast, and I'm pretty sure semi-magical... my 
> intuitive sense was that it did some smart things on a compiler level 
> that would probably be anything I'd hand-code (which would either use an 
> alist or an immutable hashtable).  So I started writing up an email 
> asking if such a thing worked... then I remembered, this is a ~lisp, 
> jumping straight to definition is part of the scene... so I did that. 
> 
> I... was wrong!  From the case macro: 
>
> ;; Symbol and "other" dispatch is either sequential or 
> ;; hash-table-based, depending on how many constants we 
> ;; have. Assume that `alist' does not map anything to `#f'. 
> (define (dispatch-hashable tmp-stx alist make-hashX else-exp) 
>   (if (< (length alist) *hash-threshold*) 
>   #`(case/sequential #,tmp-stx 
>  #,@(map (λ (x) 
>#`[(#,(car x)) #,(cdr x)]) 
>  alist) 
>  [else #,else-exp]) 
>   (let ([tbl (make-hashX alist)]) 
> (if (literal-expression? else-exp) 
> #`(hash-ref #,tbl #,tmp-stx (lambda () #,else-exp)) 
> #`(or (hash-ref #,tbl #,tmp-stx (lambda () #f)) 
>   #,else-exp) 
>
> Am I reading that right?  Here I was assuming writing something like 
> case was for cool kids and I'd never pull such a thing off right. 
>
> But... now it looks like, oh, it's actually just using an alist or an 
> immutable hashtable... same as I would. 
>
> So I almost trashed this email, but thought I'd send it anyway because 
> a) either maybe it's interesting to someone or b) actually maybe I'm 
> misreading and case really is smarter / more performant and I'm just 
> missing it...! 
>
>  - Chris 
>
>

-- 
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/31189b98-2711-448f-90d1-962f2f0bda0a%40googlegroups.com.


[racket-users] Re: Some stuff about "case".

2020-06-01 Thread Christopher Lemmer Webber
Except maybe for one thing: I wonder if the version of case defined here
is written in such a way that is smart in that it never has to make said
hash table / alist more than once, at compile time.  I'm guessing so?

Christopher Lemmer Webber writes:

> As I started typing this email and looking into the definition of case,
> I realized my assumptions are wrong.
>
> What I needed: something like case which dispatches on symbols, except
> not auto-quoting the arguments... I needed to evaluate them from the
> lexical environment.  So:
>
>   (let ([x 'foo])
> (caseish 'foo
>   [(x) 'got-foo]
>   [('x) 'got-quote-x]))  ; => 'got-foo
>
> I figured: case is fast, and I'm pretty sure semi-magical... my
> intuitive sense was that it did some smart things on a compiler level
> that would probably be anything I'd hand-code (which would either use an
> alist or an immutable hashtable).  So I started writing up an email
> asking if such a thing worked... then I remembered, this is a ~lisp,
> jumping straight to definition is part of the scene... so I did that.
>
> I... was wrong!  From the case macro:
>
> ;; Symbol and "other" dispatch is either sequential or
> ;; hash-table-based, depending on how many constants we
> ;; have. Assume that `alist' does not map anything to `#f'.
> (define (dispatch-hashable tmp-stx alist make-hashX else-exp)
>   (if (< (length alist) *hash-threshold*)
>   #`(case/sequential #,tmp-stx 
>  #,@(map (λ (x)
>#`[(#,(car x)) #,(cdr x)])
>  alist)
>  [else #,else-exp])
>   (let ([tbl (make-hashX alist)])
> (if (literal-expression? else-exp)
> #`(hash-ref #,tbl #,tmp-stx (lambda () #,else-exp))
> #`(or (hash-ref #,tbl #,tmp-stx (lambda () #f))
>   #,else-exp)
>
> Am I reading that right?  Here I was assuming writing something like
> case was for cool kids and I'd never pull such a thing off right.
>
> But... now it looks like, oh, it's actually just using an alist or an
> immutable hashtable... same as I would.
>
> So I almost trashed this email, but thought I'd send it anyway because
> a) either maybe it's interesting to someone or b) actually maybe I'm
> misreading and case really is smarter / more performant and I'm just
> missing it...!
>
>  - Chris

-- 
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/87h7vuvchp.fsf%40dustycloud.org.