louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#115 · main · 21 min 41 s
0Étoiles
0Forks
40Issues
| 1 | // Effect handlers in hot loops: a tail-resumptive `fn` operation, and a |
| 2 | // `ctl` generator (stack switching). |
| 3 | |
| 4 | import std/io |
| 5 | |
| 6 | effect counter |
| 7 | { |
| 8 | fn tick :: n: Int -> Int |
| 9 | } |
| 10 | |
| 11 | effect gen |
| 12 | { |
| 13 | ctl yield :: x: Int -> () |
| 14 | } |
| 15 | |
| 16 | fn ticks :: i: Int acc: Int -> Int / <counter> |
| 17 | { |
| 18 | if i == 0 |
| 19 | { |
| 20 | acc |
| 21 | } |
| 22 | else |
| 23 | { |
| 24 | ticks (i - 1) (acc + tick i) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | fn count :: i: Int n: Int -> () / <gen> |
| 29 | { |
| 30 | if i < n |
| 31 | { |
| 32 | yield i |
| 33 | count (i + 1) n |
| 34 | } |
| 35 | else |
| 36 | { |
| 37 | () |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | fn total :: action: fn() -> () / <gen | e> -> Int / <e> |
| 42 | { |
| 43 | handle |
| 44 | { |
| 45 | action () |
| 46 | } |
| 47 | with |
| 48 | { yield x -> x + resume (), return _ -> 0 } |
| 49 | } |
| 50 | |
| 51 | pub fn main :: -> () / <io> |
| 52 | { |
| 53 | let fast = handle { ticks 2000000 0 } with { tick n -> n % 3 } |
| 54 | let slow = total fn { count 0 100000 } |
| 55 | io.println (show fast <> " " <> show slow) |
| 56 | } |
| 57 | //> 2000001 4999950000 |