Mercurial > repos > other > SevenLanguagesInSevenWeeks
annotate 2-Io/day3-concurrency.io @ 90:c27c87cd0f08
Add most of the Day 2 exercises for Haskell
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 16 Jun 2019 21:09:33 +0100 |
parents | 03782444978f |
children |
rev | line source |
---|---|
49
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 # Concurrency and coroutines don't appear to need any special syntax beyond "yield" |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 # and a slight tweak to how you invoke the method/send the message |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 vizzini := Object clone |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 vizzini talk := method( |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 "Fezzik, are there rocks ahead?" println |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 yield |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 "No more rhymes now, I mean it." println |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 yield |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 ) |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 fezzik := Object clone |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 fezzik rhyme := method( |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 yield |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 "If there are, we'll all be dead." println |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 yield |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 "Anoybody want a peanut?" println |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 ) |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 # The magic happens here - call it with "@@" and it gets kicked off in a thread |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 vizzini @@talk; fezzik @@rhyme |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 # And further magic with classes/functions we never knew existed before, |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 # which holds the thread open until the threads are done |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
24 # (Initially this looked like two lines of Coroutine output joined together in print!) |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
25 #Coroutine currentCoroutine pause |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
26 # It's not clear WHY they're yielding to each other. Nothing in the code appears to |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 # create a relationship, and in other languages then the threads would run at |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
28 # arbitrary slices and (semi-randomly) intersperse their output without the yields. |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
29 # But Io. |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
30 # Oh, and apparently we can't use it when we want to do other things afterwards, |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
31 # because we get "Scheduler: nothing left to resume so we are exiting". So |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
32 # it isn't the same as "join" in other languages to wait for threads to finish/merge |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
33 |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
34 # Apparently "asyny messages" (@@) makes something an actor |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
35 slower := Object clone |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
36 slower start := method(wait(2); writeln("2s delay")) |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
37 faster := Object clone |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
38 faster start := method(wait(1); writeln("1s delay")) |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
39 # Even though slower starts first, they should be threaded so faster should print first |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
40 slower @@start; faster @@start; wait(3) |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
41 |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
42 |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
43 # Futures use a single "@" and are more sensible. They return a placeholder straight away |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
44 # but let you carry on with other things, then block when you call them until the value |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
45 # is *actually* available |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
46 slow_method := method(wait(3); return "Some slow value") |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
47 "Calling slow method" println |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
48 value := @slow_method |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
49 "Now we do other stuff…" println |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
50 "And a bit more" println |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
51 "And now we try to print the value…" println |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
52 value println |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
53 "That should have paused, but not due to the outer code" println |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
54 "Wut?" println |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
55 "Now everything prints twice!" println |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
56 "What is going on?" println |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
57 "It's as if Io created a second thread for no discernable reason!" println |
03782444978f
Add coroutine, actor and futures code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
58 "But it isn't anything to do with our slower/faster threads, because everything before printing 'value' is fine" println |