]> gitweb.factorcode.org Git - factor.git/blob - extra/usa-cities/usa-cities.factor
scryfall: parse mtga deck format
[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: accessors binary-search combinators combinators.smart csv
4 io.encodings.ascii kernel math.order math.parser sequences
5 summary ;
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>csv
32     rest-slice [
33         [
34             {
35                 [ string>number ]
36                 [ ]
37                 [ string>state ]
38                 [ string>number ]
39                 [ string>number ]
40                 [ string>number ]
41                 [ string>number ]
42             } spread
43         ] input<sequence city boa
44     ] map ;
45
46 MEMO: cities-named ( name -- cities )
47     cities [ name>> = ] with filter ;
48
49 MEMO: cities-named-in ( name state -- cities )
50     cities [
51         [ name>> = ] [ state>> = ] bi-curry bi* and
52     ] 2with filter ;
53
54 : find-zip-code ( code -- city )
55     cities [ first-zip>> <=> ] with search nip ;