-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathrange.cxx
55 lines (43 loc) · 1.28 KB
/
range.cxx
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
46
47
48
49
50
51
52
53
54
55
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
int sq(int x) { return x * x; }
int main() {
printf("%d ", @range(10))...;
printf("\n"); // Prints '0 1 2 3 4 5 6 7 8 9 '
printf("%d ", @range(5:25:5))...;
printf("\n"); // Prints '5 10 15 20 '
printf("%d ", @range(25:5:-5))...;
printf("\n"); // Prints '24 19 14 9'
// Sum up integers from 0 to 9.
int sum = (... + @range(10));
// Sum up squares of integers from 0 to 9.
int sum_squares = (... + sq(@range(10)));
// Fill two vectors with ints.
std::vector v1 = [@range(3:18:3)...]; // 3, 6, 9, 12, 15
std::vector v2 = [@range(5:15:2)...]; // 5, 7, 9, 11, 13
printf("%d ", v1[:])...; printf("\n");
printf("%d ", v2[:])...; printf("\n");
// Get their dot product.
double dot = (... + (v1[:] * v2[:]));
printf("%f\n", dot);
// Get their L2 norm.
double l2 = sqrt(... + sq(v1[:] - v2[:]));
printf("%f\n", l2);
// Fill a vector with strings.
const char* days[] {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
// Print index/string pairs.
printf("%d: %s\n", @range(1:), days[:])...;
// Prints:
// 1: Sunday
// 2: Monday
// 3: Tuesday
// 4: Wednesday
// 5: Thursday
// 6: Friday
// 7: Saturday
}