tobogganSe connecter

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.

29 lignes · 928 octetsBrut · Historique
1// Fusion (issue #198, ADR 0043): pure list pipelines fuse into one pass.
2// Both back ends run the same optimised Core, so both must print the
3// same. The expected outputs are computed by hand, fused or not.
4
5import std/io
6import std/list
7
8pub fn main :: -> () / <io>
9{
10 let xs = list.range 1 10
11 // map ∘ map
12 io.println (show (list.map (list.map xs fn x { x * 2 }) fn x { x + 1 }))
13 // the epic's pipeline: fold ∘ filter ∘ map ∘ range
14 let doubled = list.map xs fn x { x * 2 }
15 io.println
16 ( show
17 (list.fold (list.filter doubled fn x { x % 3 == 0 }) 0 fn a x { a + x })
18 )
19 // length ∘ map
20 io.println (show (list.length (list.map xs fn x { x * 10 })))
21 // an effectful stage keeps its order (the gate refuses to fuse it)
22 list.each (list.map [1, 2, 3] fn x { x * 2 }) fn x { io.println "seen {x}" }
23}
24//> [3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
25//> 36
26//> 10
27//> seen 2
28//> seen 4
29//> seen 6