case>quot ( default assoc -- quot )
Factor documentation > Factor handbook > The language > Combinators > Quotation construction utilities
Prev:cond>quot ( assoc -- quot )
Next:alist>quot ( default assoc -- quot )


Vocabulary
combinators

Inputs and outputs
defaulta quotation
assoca sequence of pairs of quotations
quota quotation


Word description
Creates a quotation that when called, has the same effect as applying case to assoc.

This word uses three strategies:
If the assoc only has a few keys, a linear search is generated.
If the assoc has a large number of keys which form a contiguous range of integers, a direct dispatch is generated using the dispatch word together with a bounds check.
Otherwise, an open-coded hashtable dispatch is generated.


Definition
USING: accessors assocs combinators.private kernel math
sequences words ;

IN: combinators

: case>quot ( default assoc -- quot )
<reversed> dup keys {
{ [ dup empty? ] [ 2drop ] }
{
[ dup [ length 4 <= ] [ [ word? ] any? ] bi or ]
[ drop linear-case-quot ]
}
{
[ dup contiguous-range? ]
[ drop dispatch-case-quot ]
}
{
[ dup [ wrapper? ] any? not ]
[ drop hash-case-quot ]
}
{
[ dup [ wrapper? ] all? ]
[
drop [ [ wrapped>> ] dip ] assoc-map
hash-case-quot
]
}
[ drop linear-case-quot ]
} cond ;