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.

54 lignes · 1673 octetsBrut · Historique
1// `std/collection` (ADR 0047): one trait over List, Array, Dict and
2// Bytes, with an associated element type, and generic helpers on it.
3
4import std/array
5import std/bytes
6import std/collection.{type Collection, type Elem}
7import std/dict
8import std/io
9import std/iter
10
11// Generic: works on any collection whose elements show.
12fn describe :: c: c -> String where Collection<c>, Show<Elem<c>>
13{
14 "{collection.size c} elements: {collection.to_list c}"
15}
16
17// Generic, through the lazy iterator.
18fn doubled_total :: c: c -> Int where Collection<c, Elem = Int>
19{
20 collection.iter c |> iter.map fn x { x * 2 } |> iter.fold 0 fn a x { a + x }
21}
22
23pub fn main :: -> () / <io>
24{
25 let xs = [3, 1, 4, 1, 5]
26 let arr = array.from_list [2, 7, 1]
27 let d = dict.from_list [("b", 2), ("a", 1)]
28 let b = bytes.from_list [0, 255, 16]
29 io.println (describe xs)
30 io.println (describe arr)
31 io.println (describe d)
32 io.println (describe b)
33 io.println (show [collection.sum xs, collection.sum arr, collection.sum b])
34 io.println (show (collection.sum [1.5, 2.25]))
35 io.println (show (collection.count xs fn x { x == 1 }))
36 io.println (show [collection.contains arr 7, collection.contains b 1])
37 io.println (show (collection.contains d ("a", 1)))
38 io.println
39 (show [collection.is_empty xs, collection.is_empty (dict.empty ())])
40 io.println (show (collection.fold d "" fn acc kv { acc <> kv.0 }))
41 io.println (show [doubled_total xs, doubled_total b])
42}
43//> 5 elements: [3, 1, 4, 1, 5]
44//> 3 elements: [2, 7, 1]
45//> 2 elements: [(a, 1), (b, 2)]
46//> 3 elements: [0, 255, 16]
47//> [14, 10, 271]
48//> 3.75
49//> 2
50//> [True, False]
51//> True
52//> [False, True]
53//> ab
54//> [28, 542]