Re: [racket-users] can `impersonate-hash` really filter out hash keys?

2017-06-30 Thread Matthew Butterick
That's a good suggestion. Yes, that will be smoother.


> On Jun 30, 2017, at 10:31 AM, Philip McGrath  wrote:
> 
> If it doesn't absolutely have to be a hash, you can definitely make dict-ref 
> and dict-iterate-key etc. work differently.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] can `impersonate-hash` really filter out hash keys?

2017-06-30 Thread Philip McGrath
If it doesn't absolutely have to be a hash, you can definitely
make dict-ref and dict-iterate-key etc. work differently.

-Philip

On Fri, Jun 30, 2017 at 12:15 PM, Matthew Butterick  wrote:

>
> > On Jun 30, 2017, at 10:07 AM, Robby Findler 
> wrote:
> >
> > Maybe you could make the impersonator (it would be a chaperone, really
> > in what I'm suggesting) signal an error if it gets one of the private
> > keys and then hand out only the hashes with the impersonator around
> > it, keeping the "raw" one around for code that is allowed to access
> > the private keys?
>
> I still want the private keys to be directly accessible. Maybe this is the
> best bet, where I include a #f/#f entry in the table, and then I can use
> #:when to do the filtering easily:
>
> (define ih (impersonate-hash (make-hash (list (cons #f #f)))
>  (λ (h k) (values k (λ (h k v) v)))
>  (λ (h k v) (values k v))
>  (λ (h k) k)
>  (λ (h k) (and (eq? k 'foo) k
> (hash-set! ih 'foo 42)
> (hash-set! ih 'bar 21)
> (hash-set! ih 'zam 7)
> (for/list ([(k v) (in-hash ih)]
>  #:when k)
>  (cons k v)) ; '((foo . 42))
> (hash-ref ih 'foo) ; 42
> (hash-ref ih 'bar) ; 21
> (hash-ref ih 'zam) ; 7
>
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] can `impersonate-hash` really filter out hash keys?

2017-06-30 Thread Matthew Butterick

> On Jun 30, 2017, at 10:07 AM, Robby Findler  
> wrote:
> 
> Maybe you could make the impersonator (it would be a chaperone, really
> in what I'm suggesting) signal an error if it gets one of the private
> keys and then hand out only the hashes with the impersonator around
> it, keeping the "raw" one around for code that is allowed to access
> the private keys?

I still want the private keys to be directly accessible. Maybe this is the best 
bet, where I include a #f/#f entry in the table, and then I can use #:when to 
do the filtering easily:

(define ih (impersonate-hash (make-hash (list (cons #f #f)))
 (λ (h k) (values k (λ (h k v) v)))
 (λ (h k v) (values k v))
 (λ (h k) k)
 (λ (h k) (and (eq? k 'foo) k
(hash-set! ih 'foo 42)
(hash-set! ih 'bar 21)
(hash-set! ih 'zam 7)
(for/list ([(k v) (in-hash ih)]
 #:when k)
 (cons k v)) ; '((foo . 42))
(hash-ref ih 'foo) ; 42
(hash-ref ih 'bar) ; 21 
(hash-ref ih 'zam) ; 7

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] can `impersonate-hash` really filter out hash keys?

2017-06-30 Thread Robby Findler
Maybe you could make the impersonator (it would be a chaperone, really
in what I'm suggesting) signal an error if it gets one of the private
keys and then hand out only the hashes with the impersonator around
it, keeping the "raw" one around for code that is allowed to access
the private keys?

Robby


On Fri, Jun 30, 2017 at 11:55 AM, Matthew Butterick  wrote:
> I'd like to make a hash impersonator that has "private" keys that can be
> reached by `hash-ref`, but which are not reported by `hash-keys` and other
> operations that iterate over all keys.
>
> The docs for `impersonate-hash` [1] say that you can "use `key-proc` to
> filter keys extracted from the table". OK, sounds promising. But later, it
> says `key-proc` "must produce a replacement for the key". So apparently it's
> more of a map-like operation than a filter-like operation.
>
> For instance, this key-proc function intends to omit any key but 'foo. But
> AFAICT there is no value you can return that signals "please pretend this
> key does not exist."
>
> #lang racket
> (define ih (impersonate-hash (make-hash)
>  (λ (h k) (values k (λ (h k v) v)))
>  (λ (h k v) (values k v))
>  (λ (h k) k)
>  ;; key-proc below: can I omit keys? I think not
>  (λ (h k) (if (eq? k 'foo)
>   k
>   #f
> (hash-set! ih 'foo 42)
> (hash-set! ih 'bar 21)
> (hash-set! ih 'zam 7)
> (hash-keys ih) ; '(#f #f foo), want just '(foo)
>
>
>
> [1]
> http://docs.racket-lang.org/reference/chaperones.html?q=impersonate-hash#%28def._%28%28quote._~23~25kernel%29._impersonate-hash%29%29
>
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.

-- 
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.
For more options, visit https://groups.google.com/d/optout.