1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: io.files io.encodings.ascii sequences generalizations
4 math.parser combinators kernel memoize csv summary
5 words accessors math.order binary-search combinators.smart ;
8 SINGLETONS: AK AL AR AS AZ CA CO CT DC DE FL GA HI IA ID IL IN
9 KS KY LA MA MD ME MI MN MO MS MT NC ND NE NH NJ NM NV NY OH OK
10 OR PA PR RI SC SD TN TX UT VA VI VT WA WI WV WY ;
14 AK AL AR AS AZ CA CO CT DC DE FL GA HI IA ID IL IN KS KY
15 LA MA MD ME MI MN MO MS MT NC ND NE NH NJ NM NV NY OH OK
16 OR PA PR RI SC SD TN TX UT VA VI VT WA WI WV WY
19 ERROR: no-such-state name ;
21 M: no-such-state summary drop "No such state" ;
23 MEMO: string>state ( string -- state )
24 dup states [ name>> = ] with find nip
25 [ ] [ no-such-state ] ?if ;
28 first-zip name state latitude longitude gmt-offset dst-offset ;
30 MEMO: cities ( -- seq )
31 "resource:extra/usa-cities/zipcode.csv" ascii <file-reader>
43 ] input<sequence city boa
46 MEMO: cities-named ( name -- cities )
47 cities [ name>> = ] with filter ;
49 MEMO: cities-named-in ( name state -- cities )
51 tuck [ name>> = ] [ state>> = ] 2bi* and
54 : find-zip-code ( code -- city )
55 cities [ first-zip>> <=> ] with search nip ;