about summary refs log tree commit diff stats
path: root/archive/1.vm/http-client.mu
blob: 8f04c2bc117de677e7a91ab66485c513d738e90a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# example program: reading a URL over HTTP

def main [
  local-scope
  $print [aaa] 10/newline
  google:&:source:char <- start-reading-from-network null/real-resources, [google.com/]
  $print [bbb] 10/newline
  n:num <- copy 0
  buf:&:buffer:char <- new-buffer 30
  {
    c:char, done?:bool <- read google
    break-if done?
    n <- add n, 1
    buf <- append buf, c
    {
      _, a:num <- divide-with-remainder n, 100
      break-if a
      $print n 10/newline
    }
    loop
  }
  result:text <- buffer-to-array buf
  open-console
  clear-screen null/screen  # non-scrolling app
  len:num <- length *result
  print null/real-screen, result
  wait-for-some-interaction
  close-console
]
an class="w"> } .Special { color: #c00000; } .muControl { color: #c0a020; } --> </style> <script type='text/javascript'> <!-- --> </script> </head> <body> <pre id='vimCodeElement'> <span class="Comment"># example program: compute the factorial of 5</span> <span class="muRecipe">def</span> main [ <span class="Constant">local-scope</span> x:num<span class="Special"> &lt;- </span>factorial <span class="Constant">5</span> $print <span class="Constant">[result: ]</span>, x, <span class="Constant">[ </span> <span class="Constant">]</span> ] <span class="muRecipe">def</span> factorial n:num<span class="muRecipe"> -&gt; </span>result:num [ <span class="Constant">local-scope</span> <span class="Constant">load-ingredients</span> <span class="Delimiter">{</span> <span class="Comment"># if n=0 return 1</span> zero?:boolean<span class="Special"> &lt;- </span>equal n, <span class="Constant">0</span> <span class="muControl">break-unless</span> zero? <span class="muControl">return</span> <span class="Constant">1</span> <span class="Delimiter">}</span> <span class="Comment"># return n * factorial(n-1)</span> x:num<span class="Special"> &lt;- </span>subtract n, <span class="Constant">1</span> subresult:num<span class="Special"> &lt;- </span>factorial x result<span class="Special"> &lt;- </span>multiply subresult, n ] <span class="Comment"># unit test</span> <span class="muScenario">scenario</span> factorial-test [ run [ <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>factorial <span class="Constant">5</span> ] memory-should-contain [ <span class="Constant">1</span><span class="Special"> &lt;- </span><span class="Constant">120</span> ] ] </pre> </body> </html> <!-- vim: set foldmethod=manual : -->