Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Hashtable iteration: performing early breaks

22 views
Skip to first unread message

WJ

unread,
Oct 16, 2015, 5:59:35 PM10/16/15
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
Frode Vatvedt Fjeld wrote:

> (defun confirm-loaded (n-val)
> "Scan the current contents of *data* and confirm whether or not n-val
> already exists."
> (let ((match-key nil))
> (prog
> (maphash #'(lambda (k v)
> (when (equalp n-val v)
> (setf match-key k)
> (go done)))
> *data*)
> done)
> match-key))

Gauche Scheme:

(use gauche.generator :only (x->generator))

(define (hash-table-val-exists? ht val)
(generator-find
(lambda (k.v) (equal? val (cdr k.v)))
(x->generator ht)))

--
What I would most desire would be the separation of the white and black
races. --- A. Lincoln, July 17, 1858

WJ

unread,
Oct 31, 2015, 7:36:17 AM10/31/15
to
WJ wrote:

> Frode Vatvedt Fjeld wrote:
>
> > (defun confirm-loaded (n-val)
> > "Scan the current contents of data and confirm whether or not n-val
> > already exists."
> > (let ((match-key nil))
> > (prog
> > (maphash #'(lambda (k v)
> > (when (equalp n-val v)
> > (setf match-key k)
> > (go done)))
> > data)
> > done)
> > match-key))
>
> Gauche Scheme:
>
> (use gauche.generator :only (x->generator))
>
> (define (hash-table-val-exists? ht val)
> (generator-find
> (lambda (k.v) (equal? val (cdr k.v)))
> (x->generator ht)))

The problem is to find the key corresponding to a value
without scanning the entire hash-table.

MatzLisp (Ruby):

h = { "a" => 100, "b" => 200, "c" => 300, "d" => 300 }
h.key(200) #=> "b"
h.key(300) #=> "c"
h.key(999) #=> nil

--
A separation of the races is the only perfect preventive of amalgamation; but
as an immediate separation is impossible, the next best thing is to keep them
apart where they are not already together. If white and black people never get
together in Kansas, they will never mix blood in Kansas.
--- A. Lincoln, June 26, 1857

WJ

unread,
Jun 28, 2016, 3:52:09 AM6/28/16
to
WJ wrote:

> Frode Vatvedt Fjeld wrote:
>
> > (defun confirm-loaded (n-val)
> > "Scan the current contents of *data* and confirm whether or not n-val
> > already exists."
> > (let ((match-key nil))
> > (prog
> > (maphash #'(lambda (k v)
> > (when (equalp n-val v)
> > (setf match-key k)
> > (go done)))
> > *data*)
> > done)
> > match-key))
>
> Gauche Scheme:
>
> (use gauche.generator :only (x->generator))
>
> (define (hash-table-val-exists? ht val)
> (generator-find
> (lambda (k.v) (equal? val (cdr k.v)))
> (x->generator ht)))

OCaml:

let val_exists ht value =
try (Hashtbl.iter
(fun k v -> if v = value then raise Exit)
ht ;
false)
with Exit -> true ;;

--
Europe is not going to be the monolithic societies that they once were in the
last century.... They are now going into a multicultural mode. Jews will be
resented because of our leading role. --- Barbara Spectre
archive.org/download/DavidDuke_videos/HowZionistsDivideAndConquer-fjjszxkm55o.ogv
0 new messages