]> gitweb.factorcode.org Git - factor.git/commitdiff
hacker-news: Fix using, fix bug printing HN company posts. Refactor a bit
authorDoug Coleman <doug.coleman@gmail.com>
Thu, 30 Jul 2015 04:16:16 +0000 (21:16 -0700)
committerDoug Coleman <doug.coleman@gmail.com>
Thu, 30 Jul 2015 04:16:34 +0000 (21:16 -0700)
extra/hacker-news/hacker-news.factor

index 692b5ce138fb214bb83d4f5860e7e00b7294fe50..c22a77ba44ef42f07d9e5a8987689d5d6a3a1c01 100644 (file)
@@ -1,26 +1,26 @@
 ! Copyright (C) 2012 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors assocs classes.tuple colors.constants
-colors.hex combinators formatting fry hashtables http.client io
-io.styles json json.reader kernel make math math.parser
-sequences splitting ui urls ;
+colors.hex combinators concurrency.combinators formatting fry
+hashtables http.client io io.styles json json.reader kernel make
+math math.parser sequences splitting ui urls ;
 IN: hacker-news
 
 <PRIVATE
 
-: json-null>f ( obj -- obj/f )
-    dup json-null = [ drop f ] when ;
+: hacker-news-recent-ids ( -- seq )
+    "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty"
+    http-get nip json> ;
 
-: hacker-news-items ( n -- seq )
-    [
-        "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty"
-        http-get nip json>
-    ] dip head
-    [
-        number>string
-        "https://hacker-news.firebaseio.com/v0/item/" ".json?print=pretty" surround
-        http-get nip json>
-    ] parallel-map ;
+: hacker-news-id>json-url ( n -- url )
+    number>string
+    "https://hacker-news.firebaseio.com/v0/item/" ".json?print=pretty" surround ;
+
+: hacker-news-items ( seq -- seq' )
+    [ hacker-news-id>json-url http-get nip json> ] parallel-map ;
+
+: hacker-news-recent-items ( n -- seq )
+    [  hacker-news-recent-ids ] dip head hacker-news-items ;
 
 : write-title ( title url -- )
     '[
@@ -57,9 +57,13 @@ PRIVATE>
         [ dup "by" of [ " by " write-text [ "by" of ] [ post>user-url ] bi write-link ] [ drop ] if ]
         ! [ dup postedAgo>> [ " " write-text postedAgo>> write-text ] [ drop ] if ]
         [
-            " | " write-text
-            [ "descendants" of [ "discuss" ] [ "%d comments" sprintf ] if-zero ]
-            [ post>comments-url ] bi write-link nl nl
+            dup "decendants" of [
+                " | " write-text
+                [ "descendants" of [ "discuss" ] [ "%d comments" sprintf ] if-zero ]
+                [ post>comments-url ] bi write-link
+            ] [
+                drop
+            ] if nl nl
         ]
     } cleave ;
 
@@ -74,6 +78,6 @@ PRIVATE>
     } assoc-union format nl ;
 
 : hacker-news. ( -- )
-    25 hacker-news-items
+    30 hacker-news-recent-items
     banner.
     [ 1 + post. ] each-index ;