16 Find
(require unstable/find) |
| |||||||||||||||||||||||||||||||||||
pred : (-> any/c any/c) | |||||||||||||||||||||||||||||||||||
x : any/c | |||||||||||||||||||||||||||||||||||
stop-on-found? : any/c = #f | |||||||||||||||||||||||||||||||||||
stop : (or/c #f (-> any/c any/c)) = #f | |||||||||||||||||||||||||||||||||||
get-children : (or/c #f (-> any/c (or/c #f list?))) = #f |
If stop-on-found? is true, the children of values satisfying pred are not examined. If stop is a procedure, then the children of values for which stop returns true are not examined (but the values themselves are; stop is applied after pred). Only the current branch of the search is stopped, not the whole search.
The search recurs through pairs, vectors, boxes, and the accessible fields of structures. If get-children is a procedure, it can override the default notion of a value’s children by returning a list (if it returns false, the default notion of children is used).
No cycle detection is done, so find on a cyclic graph may diverge. To do cycle checking yourself, use stop and a mutable table.
Examples: | |||||
> (find symbol? '((all work) and (no play))) | |||||
'(all work and no play) | |||||
> (find list? '#((all work) and (no play)) #:stop-on-found? #t) | |||||
'((all work) (no play)) | |||||
| |||||
'(-8) | |||||
| |||||
'(a) |
| |||||||||||||||||||||||||||||||||||
pred : (-> any/c any/c) | |||||||||||||||||||||||||||||||||||
x : any/c | |||||||||||||||||||||||||||||||||||
stop : (or/c #f (-> any/c any/c)) = #f | |||||||||||||||||||||||||||||||||||
get-children : (or/c #f (-> any/c (or/c #f list?))) = #f | |||||||||||||||||||||||||||||||||||
default : any/c = (lambda () (error ....)) |
Examples: | ||
> (find-first symbol? '((all work) and (no play))) | ||
'all | ||
> (find-first list? '#((all work) and (no play))) | ||
'(all work) | ||
| ||
-8 | ||
> (find-first symbol? (shared ([x (cons 'a x)]) x)) | ||
'a |