blob: e3d9bcd2b897381ff4a35226c0cbb54e0b25e907 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#include <string>
#include <vector>
#include <mutex>
#include <cstdlib>
#include <ctime>
#include <cstdio>
fill: (
out x: std::string,
in value: std::string,
in count: int
)
pre( value.size() >= count, "fill: value must contain at least count elements" )
= {
x = value.substr(0, count);
}
main: () -> int =
{
x: std::string; // note: uninitialized!
// if flip_a_coin() {
// x = "xyzzy";
// }
// else {
// // fill( out x, "plugh", 40 ); // note: constructs x!
// }
print_decorated(x);
}
// Print! A one-expression function body...
//
print_decorated: (x:_) = std::cout << ">> [" << x << "]\n";
// Flip a coin! Exercise <mutex> <cstdlib> <ctime> and 'as'...
//
rand_init_flag: std::once_flag = ();
flip_a_coin: () -> bool = {
std::call_once(rand_init_flag, srand, time(nullptr) as unsigned );
return std::rand()%2;
}
|