about summary refs log blame commit diff stats
path: root/continuation5.mu
blob: 295cb9c9112d0e071aff5b155bcab7d29c9606f8 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
24
25
December Adventure

Goal

In lieu of doing the Advent of Code I aim to do a little bit of programming every day in December.

A toot from a friend in response to my reading more about programming then actually programming:

One program (that you work on every day). If it's done in less than 31 days, you start a new one immediately. If it's not done in 31 days, you still count it 100% as a win because the goal wasn't to finish it, it was to work on it.

The goal isn't necessarily to finish a thing, but to do the thing; I've a tendancy to get stuck in a rut reading-reading-reading and not locking things into the vault with actual playtime...this is the curse of liking programing systems and languages more than the actual act of programming.

So, my goal for December is to do the damn thing -- not just read about it.

What?

This remains an open question -- I'm thinking of a pokedex to start.

re>



                  


             
                           


                
                                                                                              











                                                                     
             

                 
                          



                  
                                                                          

        
                                                    
                                                                           
 
# Example program showing that a 'paused' continuation can be 'resumed' with
# inputs.
#
# Print out a list of numbers, first adding 0 to the first, 1 to the second, 2
# to the third, and so on.
#
# To run:
#   $ git clone https://github.com/akkartik/mu
#   $ cd mu
#   $ ./mu continuation5.mu
#
# Expected output:
#   1
#   3
#   5

def main [
  local-scope
  l:&:list:num <- copy null
  l <- push 3, l
  l <- push 2, l
  l <- push 1, l
  k:continuation, x:num, done?:bool <- call-with-continuation-mark 100/mark, create-yielder, l
  a:num <- copy 1
  {
    break-if done?
    $print x 10/newline
    k, x:num, done?:bool <- call k, a  # resume; x = a + next l value
    a <- add a, 1
    loop
  }
]

def create-yielder l:&:list:num -> n:num, done?:bool [
  local-scope
  load-inputs
  a:num <- copy 0
  {
    done? <- equal l, null
    break-if done?
    n <- first l
    l <- rest l
    n <- add n, a
    a <- return-continuation-until-mark 100/mark, n, done?  # pause/resume
    loop
  }
  return-continuation-until-mark 100/mark, -1, done?
  assert false, [called too many times, ran out of continuations to return]
]