Created
May 27, 2014 14:47
-
-
Save kurogelee/54aac03d5c75993e89c9 to your computer and use it in GitHub Desktop.
Javaのラムダ使うAPIとClojureをちょこっと比較してみる ref: http://qiita.com/kurogelee/items/c032ae51c82a80cd7a97
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static <T> Stream<T> cycle(T t0, T... ts){ | |
return Stream.generate(new Supplier<T>() { | |
int index = -1; | |
@Override | |
public T get() { | |
index = (index + 1) % (ts.length + 1); | |
if(index == 0){ | |
return t0; | |
} | |
return ts[index - 1]; | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(->> (range 1 10) | |
(drop 3) | |
(take 5) | |
(reduce *) | |
println) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn map-vals-with-key [f hmap] | |
(reduce (fn [hmap [k v]] (assoc hmap k (f k v))) hmap hmap)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static <K, V> Map<K, V> mergeWith(Map<K, V> m1, Map<K, V> m2, BinaryOperator<V> f){ | |
m2.forEach((k2, v2) -> { | |
m1.computeIfPresent(k2, (k1, v1) -> f.apply(v1, v2)); | |
m1.putIfAbsent(k2, v2); | |
}); | |
return m1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(->> (range 1 10) | |
(filter #(zero? (rem % 2))) | |
(map #(* % 3)) | |
(#(doseq [i %] (println i)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IntStream.range(1, 10) | |
.filter(i -> i % 2 == 0) | |
.map(i -> i * 3) | |
.forEach(System.out::println); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IntStream.range(1, 10) | |
.skip(3) | |
.limit(5) | |
.reduce((i, j) -> i * j) | |
.ifPresent(System.out::println); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment