Skip to content

Instantly share code, notes, and snippets.

@kurogelee
Created May 27, 2014 14:47
Show Gist options
  • Save kurogelee/54aac03d5c75993e89c9 to your computer and use it in GitHub Desktop.
Save kurogelee/54aac03d5c75993e89c9 to your computer and use it in GitHub Desktop.
Javaのラムダ使うAPIとClojureをちょこっと比較してみる ref: http://qiita.com/kurogelee/items/c032ae51c82a80cd7a97
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];
}
});
}
(->> (range 1 10)
(drop 3)
(take 5)
(reduce *)
println)
(defn map-vals-with-key [f hmap]
(reduce (fn [hmap [k v]] (assoc hmap k (f k v))) hmap hmap))
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;
}
(->> (range 1 10)
(filter #(zero? (rem % 2)))
(map #(* % 3))
(#(doseq [i %] (println i))))
IntStream.range(1, 10)
.filter(i -> i % 2 == 0)
.map(i -> i * 3)
.forEach(System.out::println);
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