Minor fix: use blank?
parent
1b7a373ea8
commit
4c6a05de50
|
@ -4,7 +4,8 @@
|
|||
! Simple CSV Parser
|
||||
! Phil Dawes phil@phildawes.net
|
||||
|
||||
USING: kernel sequences io namespaces combinators ;
|
||||
USING: kernel sequences io namespaces combinators
|
||||
unicode.categories ;
|
||||
IN: csv
|
||||
|
||||
DEFER: quoted-field
|
||||
|
@ -29,7 +30,7 @@ DEFER: quoted-field
|
|||
|
||||
! trims whitespace from either end of string
|
||||
: trim-whitespace ( str -- str )
|
||||
[ "\s\t" member? ] trim ; inline
|
||||
[ blank? ] trim ; inline
|
||||
|
||||
: quoted-field ( -- endchar )
|
||||
"\"" read-until ! "
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
USING: io.files io.encodings.ascii sequences sequences.lib
|
||||
math.parser combinators kernel memoize csv symbols inspector
|
||||
words accessors math.order sorting ;
|
||||
IN: zip-codes
|
||||
|
||||
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 )
|
||||
dup states [ word-name = ] with find nip
|
||||
[ ] [ no-such-state ] ?if ;
|
||||
|
||||
TUPLE: city
|
||||
first-zip name state latitude longitude gmt-offset dst-offset ;
|
||||
|
||||
MEMO: cities ( -- seq )
|
||||
"resource:extra/zip-codes/zipcode.csv" ascii <file-reader>
|
||||
csv rest-slice [
|
||||
7 firstn {
|
||||
[ string>number ]
|
||||
[ ]
|
||||
[ string>state ]
|
||||
[ string>number ]
|
||||
[ string>number ]
|
||||
[ string>number ]
|
||||
[ string>number ]
|
||||
} spread city boa
|
||||
] map ;
|
||||
|
||||
MEMO: cities-named ( name -- cities )
|
||||
cities [ name>> = ] with filter ;
|
||||
|
||||
MEMO: cities-named-in ( name state -- cities )
|
||||
cities [
|
||||
tuck [ name>> = ] [ state>> = ] 2bi* and
|
||||
] with with filter ;
|
||||
|
||||
: find-zip-code ( code -- city )
|
||||
cities [ first-zip>> <=> ] binsearch* ;
|
Can't render this file because it is too large.
|
Loading…
Reference in New Issue