Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. I ran this:
  2.  
  3. |start %example-tapp-fetch
  4.  
  5. I got this:
  6.  
  7. + /~zod/home/1.270/app/example-tapp-fetch/hoon
  8. activated app home/example-tapp-fetch
  9. ford: %hood: syntax error at [67 6] in /~zod/home/0/app/example-tapp-fetch/hoon
  10. ford: %core on /~zod/home/0/app/example-tapp-fetch/hoon failed:
  11. >> [%mo-not-running %example-tapp-fetch %peer]
  12. > |start %example-tapp-fetch
  13. >=
  14.  
  15.  
  16. I ran this code:
  17.  
  18. :: Little app to demonstrate the structure of programs written with the
  19. :: transaction monad.
  20. ::
  21. :: Fetches the top comment of each of the top 10 stories from Hacker News
  22. ::
  23. /+ tapp, stdio
  24. ::
  25. :: Preamble
  26. ::
  27. =>
  28. |%
  29. +$ state
  30. $: top-comments=(list tape)
  31. ==
  32. +$ peek-data _!!
  33. +$ in-poke-data [%noun =cord]
  34. +$ out-poke-data ~
  35. +$ in-peer-data ~
  36. +$ out-peer-data
  37. $% [%comments (list tape)]
  38. ==
  39. ++ tapp (^tapp state peek-data in-poke-data out-poke-data in-peer-data out-peer-data)
  40. ++ stdio (^stdio out-poke-data out-peer-data)
  41. --
  42. =>
  43. |%
  44. :: Helper function to print a comment
  45. ::
  46. ++ comment-to-tang
  47. |= =tape
  48. ^- tang
  49. %+ welp
  50. %+ turn (rip 10 (crip tape))
  51. |= line=cord
  52. leaf+(trip line)
  53. [leaf+""]~
  54. ::
  55. :: All the URLs we fetch from
  56. ::
  57. ++ urls
  58. =/ base "https://hacker-news.firebaseio.com/v0/"
  59. :* top-stories=(weld base "topstories.json")
  60. item=|=(item=@ud `tape`:(welp base "item/" +>:(scow %ui item) ".json"))
  61. ==
  62. --
  63. =, async=async:tapp
  64. =, tapp-async=tapp-async:tapp
  65. =, stdio
  66. ::
  67. :: The app
  68. ::
  69. %- create-tapp-poke-peer-take:tapp
  70. ^- tapp-core-poke-peer-take:tapp
  71. |_ [=bowl:gall state]
  72. ::
  73. :: Main function
  74. ::
  75. ++ handle-poke
  76. |= =in-poke-data
  77. =/ m tapp-async
  78. ^- form:m
  79. ::
  80. :: If requested to print, just print what we have in our state
  81. ::
  82. ?: =(cord.in-poke-data 'print')
  83. ~& 'drumroll please...'
  84. ;< now=@da bind:m get-time
  85. ;< ~ bind:m (wait (add now ~s3))
  86. ~& 'Top comments:'
  87. %- (slog (zing (turn top-comments comment-to-tang)))
  88. (pure:m top-comments)
  89. ?: =(cord.in-poke-data 'poll')
  90. ;< ~ bind:m (wait-effect (add now.bowl ~s15))
  91. (pure:m top-comments)
  92. ::
  93. :: Otherwise, fetch the top HN stories
  94. ::
  95. =. top-comments ~
  96. ::
  97. :: If this whole thing takes more than 15 seconds, cancel it
  98. ::
  99. %+ (set-timeout _top-comments) (add now.bowl ~s15)
  100. ;< =top-stories=json bind:m (fetch-json top-stories:urls)
  101. =/ top-stories=(list @ud)
  102. ((ar ni):dejs:format top-stories-json)
  103. ::
  104. :: Loop through the first 5 stories
  105. ::
  106. =. top-stories (scag 5 top-stories)
  107. |- ^- form:m
  108. =* loop $
  109. ::
  110. :: If done, tell subscribers and print the results
  111. ::
  112. ?~ top-stories
  113. ;< ~ bind:m (give-result /comments %comments top-comments)
  114. (handle-poke %noun 'print')
  115. ::
  116. :: Else, fetch the story info
  117. ::
  118. ~& "fetching item #{+>:(scow %ui i.top-stories)}"
  119. ;< =story-info=json bind:m (fetch-json (item:urls i.top-stories))
  120. =/ story-comments=(unit (list @ud))
  121. ((ot kids+(ar ni) ~):dejs-soft:format story-info-json)
  122. ::
  123. :: If no comments, say so
  124. ::
  125. ?: |(?=(~ story-comments) ?=(~ u.story-comments))
  126. =. top-comments ["<no top comment>" top-comments]
  127. loop(top-stories t.top-stories)
  128. ::
  129. :: Else, fetch comment info
  130. ::
  131. ;< =comment-info=json bind:m (fetch-json (item:urls i.u.story-comments))
  132. =/ comment-text=(unit tape)
  133. ((ot text+sa ~):dejs-soft:format comment-info-json)
  134. ::
  135. :: If no text (eg comment deleted), record that
  136. ::
  137. ?~ comment-text
  138. =. top-comments ["<top comment has no text>" top-comments]
  139. loop(top-stories t.top-stories)
  140. ::
  141. :: Else, add text to state
  142. ::
  143. =. top-comments [u.comment-text top-comments]
  144. loop(top-stories t.top-stories)
  145. ::
  146. ++ handle-peer
  147. |= =path
  148. =/ m tapp-async
  149. ^- form:m
  150. ~& [%tapp-fetch-take-peer path]
  151. (pure:m top-comments)
  152. ::
  153. ++ handle-take
  154. |= =sign:tapp
  155. =/ m tapp-async
  156. ^- form:m
  157. :: ignore %poke/peer acknowledgements
  158. ::
  159. ?. ?=(%wake -.sign)
  160. (pure:m top-comments)
  161. ;< =state bind:m (handle-poke %noun 'fetch')
  162. =. top-comments state
  163. (pure:m top-comments)
  164. --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement