]> gitweb.factorcode.org Git - factor.git/blob - extra/usa-cities/usa-cities.factor
Merge qualified, alias, symbols, constants into core
[factor.git] / extra / usa-cities / usa-cities.factor
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 ;
6 IN: usa-cities
7
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 ;
11
12 : states ( -- seq )
13     {
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
17     } ; inline
18
19 ERROR: no-such-state name ;
20
21 M: no-such-state summary drop "No such state" ;
22
23 MEMO: string>state ( string -- state )
24     dup states [ name>> = ] with find nip
25     [ ] [ no-such-state ] ?if ;
26
27 TUPLE: city
28 first-zip name state latitude longitude gmt-offset dst-offset ;
29
30 MEMO: cities ( -- seq )
31     "resource:extra/usa-cities/zipcode.csv" ascii <file-reader>
32     csv rest-slice [
33         7 firstn {
34             [ string>number ]
35             [ ]
36             [ string>state ]
37             [ string>number ]
38             [ string>number ]
39             [ string>number ]
40             [ string>number ]
41         } spread city boa
42     ] map ;
43
44 MEMO: cities-named ( name -- cities )
45     cities [ name>> = ] with filter ;
46
47 MEMO: cities-named-in ( name state -- cities )
48     cities [
49         tuck [ name>> = ] [ state>> = ] 2bi* and
50     ] with with filter ;
51
52 : find-zip-code ( code -- city )
53     cities [ first-zip>> <=> ] with search nip ;