-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHINTS.txt
177 lines (163 loc) · 5.41 KB
/
HINTS.txt
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
This file contains hints that don't give you the solution, but point you in the
right direction.
Reload this file after moving to the next step, for further hints.
// @BEGIN_VERSION_ONLY HELLO_GENERICS
Hello Generics
--------------
You're on your own on this first one...
// @END_VERSION_ONLY HELLO_GENERICS
// @BEGIN_VERSION_ONLY HELLO_GENERICS2
Hello Generics2
---------------
public static <T> Collection<T> repeat(T repeatThis, int times) {
...
}
// @END_VERSION_ONLY HELLO_GENERICS2
// @BEGIN_VERSION_ONLY HELLO_FUNCTION
Hello Function
--------------
public static Function<Integer, Integer> square = new Function<Integer, Integer>() {
...
}
// @END_VERSION_ONLY HELLO_FUNCTION
// @BEGIN_VERSION_ONLY HELLO_BINARY_FUNCTION
Hello Binary Function
---------------------
public interface Function2<A1, A2, R> {
public R apply(A1 a1, A2 a2);
}
// @END_VERSION_ONLY HELLO_BINARY_FUNCTION
// @BEGIN_VERSION_ONLY TO_BACKGROUND_FUNCTION
To Background Function
----------------------
Create a thread pool using Executors.newFixedThreadPool(10). Look at the docs
for java.util.concurrent.ExecutorService.submit. Create a new Function from F
to Future<T> that submits to the ExecutorService a Callable<T> that applies
the original Function<T> to the input of the new Function.
Something like this, perhaps?
public static <F,T> Function<F, Future<T>> toBackgroundFunction(final Function<F, T> f) {
return new Function<F, Future<T>>() {
public Future<T> apply(final F from) {
return threadPool.submit(new Callable<T>() {
...
});
}
};
}
// @END_VERSION_ONLY TO_BACKGROUND_FUNCTION
// @BEGIN_VERSION_ONLY BACKGROUND_TRANSFORM
Background Transform
--------------------
public static <F,T> Collection<Future<T>> transformInBackground(Collection<F> fromCollection, Function<F, T> f) {
...
}
The resulting collection is lazy, which will create problems. Look into
ImmutableList.copyOf to go non-lazy.
// @END_VERSION_ONLY BACKGROUND_TRANSFORM
// @BEGIN_VERSION_ONLY FROM_FUTURE_FUNCTION
From Future Function
--------------------
public static <A> Function<Future<A>, A> fromFuture() {
...
}
Do you 'get' it?
// @END_VERSION_ONLY FROM_FUTURE_FUNCTION
// @BEGIN_VERSION_ONLY GET_ALL
Get All
-------
Note Java's crappy type inference:
return transform(coll, Collections3.fromFuture()); // does not work
return transform(coll, fromFuture()); // does not work
You must write something like:
return transform(coll, Collections3.<A>fromFuture());
// @END_VERSION_ONLY GET_ALL
// @BEGIN_VERSION_ONLY REGULAR_TRANSFORM
Regular Transform
-----------------
Nothing to do in this step, just run the test and watch it fail.
// @END_VERSION_ONLY REGULAR_TRANSFORM
// @BEGIN_VERSION_ONLY PARALLEL_TRANSFORM
Parallel Transform
------------------
public static <F,T> Collection<T> parallelTransform(Collection<F> fromCollection, Function<F, T> f) {
...
}
Get all the results that have been transformed in the background.
Use your building blocks: transformInBackground and getAll.
// @END_VERSION_ONLY PARALLEL_TRANSFORM
// @BEGIN_VERSION_ONLY REDUCE
Reduce
------
Use an iterator to check if no values, to get the first value, etc.
// @END_VERSION_ONLY REDUCE
// @BEGIN_VERSION_ONLY FOLD
Fold
----
This is easier than reduce!
// @END_VERSION_ONLY FOLD
// @BEGIN_VERSION_ONLY GATHER_WORDS_WHITESPACE
Gather Words, Whitespace
------------------------
No hints yet.
// @END_VERSION_ONLY GATHER_WORDS_WHITESPACE
// @BEGIN_VERSION_ONLY GATHER_WORDS_PUNCTUATION
Gather Words, Remove Punctuation
--------------------------------
No hints yet.
// @END_VERSION_ONLY GATHER_WORDS_PUNCTUATION
// @BEGIN_VERSION_ONLY GATHER_WORDS_LOWERCASE
Gather Words, Ignore Case
-------------------------
No hints yet.
// @END_VERSION_ONLY GATHER_WORDS_LOWERCASE
// @BEGIN_VERSION_ONLY COUNT_WORDS
Count Words
-----------
This is the pseudo-code for counting stuff in a collection:
(fold (function [counts x]
(assoc counts x (inc (get counts x 0))))
{}
coll))
'counts' is a Map that contains the accumulated counts.
'x' will be the element to count.
{} is an empty Map used as the initial value.
Get the value of x from the Map counts, or zero if no x in counts:
(get counts x 0)
Increase it by one:
(inc (get counts x 0))
Associate to the key 'x', in the Map 'counts', the above number as the value:
(assoc counts x (inc (get counts x 0)))
Call the function first with the empty map and the first entry in coll, then
the resulting map and the second entry in coll, etc:
(fold (function [counts x]
(assoc counts x (inc (get counts x 0))))
{}
coll))
// @END_VERSION_ONLY COUNT_WORDS
// @BEGIN_VERSION_ONLY SORT_COUNTED_WORDS
Sort Counted Words
------------------
Transform the Map to a collection of WordCountPairs, then sort.
// @END_VERSION_ONLY SORT_COUNTED_WORDS
// @BEGIN_VERSION_ONLY REPEAT_STR
Repeat String
-------------
StringBuilder
// @END_VERSION_ONLY REPEAT_STR
// @BEGIN_VERSION_ONLY HISTOGRAM_ENTRY
Histogram Entry
---------------
(let [r (- width (count word))]
(str word (repeat-str " " r) " " (repeat-str \# n))))
// @END_VERSION_ONLY HISTOGRAM_ENTRY
// @BEGIN_VERSION_ONLY HISTOGRAM
Histogram
---------
Use Collections2.max to find the max word length and use that as the width to
histogramEntry. Interpose line.separator between the entries.
// @END_VERSION_ONLY HISTOGRAM
// @BEGIN_VERSION_ONLY DESIGN_QUESTIONS
Design Questions
----------------
No hints yet.
// @END_VERSION_ONLY DESIGN_QUESTIONS