-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example.java
executable file
·109 lines (98 loc) · 2.99 KB
/
Example.java
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import static com.todesking.sandbox.jqjq.Jqjq.*;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.todesking.sandbox.jqjq.Jqjq.JqjqIterable;
import com.todesking.sandbox.jqjq.Jqjq.Tuple;
import com.todesking.sandbox.jqjq.Jqjq.WithIndex;
public class Example {
public static void main(String[] args) throws IOException {
}
static void pascalsTriangle() {
System.out.print("input triangle size: ");
final Scanner s = from(System.in).toScanner();
final int size = s.nextInt();
JqjqIterable<Integer> nums = from(1).to(1);
System.out.println(nums.join(" "));
for (int i : from(0).to(size - 2)) {
nums =
nums.appendFirst(0).appendLast(0).eachPair().transform(
new Function<Tuple<Integer>, Integer>() {
public Integer apply(Tuple<Integer> arg0) {
return arg0.get(0) + arg0.get(1);
}
});
System.out.println(nums.join(" "));
}
}
static void reversedPascalTriangle() {
final String prompt = "input numbers: ";
System.out.print(prompt);
for (String line : from(System.in).eachLine()) {
JqjqIterable<Integer> nums = from(line).split(" ").toInteger();
while (true) {
final String str = nums.join(" ");
if (str.length() == 0)
break;
System.out.println(str);
nums =
nums.eachPair().transform(
new Function<Tuple<Integer>, Integer>() {
public Integer apply(Tuple<Integer> arg0) {
return arg0.get(0) + arg0.get(1);
}
});
}
System.out.println(prompt);
}
}
static void echo() {
System.out.print("echo > ");
for (String line : from(System.in).eachLine()) {
System.out.println(line);
System.out.print("echo > ");
}
}
static void showSorted() {
showSorted(Arrays.asList(1, 2, 3, 4));
showSorted(Arrays.asList(1, 2, 4, 3));
}
private static void showSorted(final List<Integer> list) {
final boolean sorted =
from(list).eachPair().satisfyAll(new Predicate<Tuple<Integer>>() {
public boolean apply(Tuple<Integer> arg0) {
return arg0.get(0) <= arg0.get(1);
}
});
System.out.println("sorted(" + list + ") = " + sorted);
}
static void fizzBuzz() {
final JqjqIterable<String> fizzBuzz =
from(1).toInfinity().transform(new Function<Integer, String>() {
public String apply(Integer n) {
return n % 15 == 0 ? "FizzBuzz" : n % 3 == 0
? "Fizz"
: n % 5 == 0 ? "Buzz" : from(n).toString();
}
});
for (String s : fizzBuzz.take(100)) {
System.out.println(s);
}
}
static void print1to10() {
for (int i : from(1).to(10)) {
System.out.println(i);
}
}
static void showFileWithLineNo() throws IOException {
for (WithIndex<String> line : from(new File(from(System.in).readLine()))
.eachLine()
.withIndex()) {
System.out.println(line.index + ": " + line.value);
}
}
}