2008-04-29 21:44:07 -04:00
|
|
|
! Copyright (C) 2008 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-07-13 20:50:37 -04:00
|
|
|
USING: io.files io.encodings.ascii sequences generalizations
|
2008-12-17 19:10:01 -05:00
|
|
|
math.parser combinators kernel memoize csv summary
|
2009-01-08 18:48:17 -05:00
|
|
|
words accessors math.order binary-search combinators.smart ;
|
2008-04-29 21:44:07 -04:00
|
|
|
IN: usa-cities
|
2008-04-29 21:43:50 -04:00
|
|
|
|
|
|
|
SINGLETONS: AK AL AR AS AZ CA CO CT DC DE FL GA HI IA ID IL IN
|
|
|
|
KS KY LA MA MD ME MI MN MO MS MT NC ND NE NH NJ NM NV NY OH OK
|
|
|
|
OR PA PR RI SC SD TN TX UT VA VI VT WA WI WV WY ;
|
|
|
|
|
|
|
|
: states ( -- seq )
|
|
|
|
{
|
|
|
|
AK AL AR AS AZ CA CO CT DC DE FL GA HI IA ID IL IN KS KY
|
|
|
|
LA MA MD ME MI MN MO MS MT NC ND NE NH NJ NM NV NY OH OK
|
|
|
|
OR PA PR RI SC SD TN TX UT VA VI VT WA WI WV WY
|
|
|
|
} ; inline
|
|
|
|
|
|
|
|
ERROR: no-such-state name ;
|
|
|
|
|
|
|
|
M: no-such-state summary drop "No such state" ;
|
|
|
|
|
|
|
|
MEMO: string>state ( string -- state )
|
2008-06-28 03:36:20 -04:00
|
|
|
dup states [ name>> = ] with find nip
|
2015-08-13 19:13:05 -04:00
|
|
|
[ ] [ no-such-state ] ?if ;
|
2008-04-29 21:43:50 -04:00
|
|
|
|
|
|
|
TUPLE: city
|
|
|
|
first-zip name state latitude longitude gmt-offset dst-offset ;
|
|
|
|
|
|
|
|
MEMO: cities ( -- seq )
|
2009-02-15 21:45:06 -05:00
|
|
|
"resource:extra/usa-cities/zipcode.csv" ascii file>csv
|
|
|
|
rest-slice [
|
2009-01-08 18:48:17 -05:00
|
|
|
[
|
|
|
|
{
|
|
|
|
[ string>number ]
|
|
|
|
[ ]
|
|
|
|
[ string>state ]
|
|
|
|
[ string>number ]
|
|
|
|
[ string>number ]
|
|
|
|
[ string>number ]
|
|
|
|
[ string>number ]
|
|
|
|
} spread
|
|
|
|
] input<sequence city boa
|
2008-04-29 21:43:50 -04:00
|
|
|
] map ;
|
|
|
|
|
|
|
|
MEMO: cities-named ( name -- cities )
|
|
|
|
cities [ name>> = ] with filter ;
|
|
|
|
|
|
|
|
MEMO: cities-named-in ( name state -- cities )
|
|
|
|
cities [
|
2009-11-05 23:22:21 -05:00
|
|
|
[ name>> = ] [ state>> = ] bi-curry bi* and
|
2014-07-22 09:09:26 -04:00
|
|
|
] 2with filter ;
|
2008-04-29 21:43:50 -04:00
|
|
|
|
|
|
|
: find-zip-code ( code -- city )
|
2008-07-15 18:16:08 -04:00
|
|
|
cities [ first-zip>> <=> ] with search nip ;
|