-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.swift
84 lines (73 loc) · 1.68 KB
/
main.swift
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
func make_isprime() -> (Int -> Bool) {
var memo = Int[]()
func isprime(n: Int) -> Bool {
for prime in memo {
if n % prime == 0 { return false }
}
memo.append(n)
return true
}
return isprime
}
func intifyarg1() -> Int? {
if C_ARGC == 2 {
let limitstr = String.fromCString(C_ARGV[1])
if let limit = limitstr.toInt() {
return limit
}
}
return nil
}
/*
if let limit = intifyarg1() {
let primes = filter(count(start: 2), make_isprime())
let limited = takewhile(primes, { $0 <= limit })
let s = " ".join(map(limited, { $0.description }))
println("\(s)")
} else {
println("\(C_ARGV[0]) LIMIT")
}
*/
println("*** groupby/islice ***")
let a = [0, 0, 0, 1, 1, 2, 2, 2, 3]
let g = groupby_nokey(a)
for (key, group) in islice(g, start: 1) {
println("\(key): \(group)")
}
println("*** groupby/take ***")
let g2 = groupby_nokey(a)
for (key, group) in take(g2, 2) {
println("\(key): \(group)")
}
println("*** zip ***")
let b = [1, 2, 3]
let c = ["spam", "eggs"]
for t in zip(b, c) {
println("\(t)")
}
println("*** product2 ***")
for t in product2(b, c) {
println("\(t)")
}
println("*** product ***")
for t in product(c, c, c) {
println("\(t)")
}
println("*** self_product ***")
for t in self_product(c, 2) {
println("\(t)")
}
println("*** tabulate/take ***")
let squares = tabulate({ $0*$0 })
let squares15 = take(squares, 5)
println("\(squares15)")
let squares1a = tabulate_n(1, { $0*$0 })
let squares15a = take(squares1a, 4)
println("\(squares15a)")
println("*** sum/sum1 ***")
println("\(sum1(squares15a))")
/*
for (key, group) in groupby(a, { $0 / 2 }) {
println("\(key): \(group)")
}
*/