Skip to content

Instantly share code, notes, and snippets.

View aruld's full-sized avatar
🎯
Focusing

Arul Dhesiaseelan aruld

🎯
Focusing
View GitHub Profile
@aruld
aruld / latency.txt
Created June 16, 2012 08:27 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers
--------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns
Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms
Read 4K randomly from SSD* 150,000 ns 0.15 ms
@aruld
aruld / LINQQueryExample.java
Created September 16, 2012 04:16
Query an ArrayList with LINQ sample ported from C# to Java 8 (http://msdn.microsoft.com/en-us/library/bb397937.aspx)
import java.util.ArrayList;
import java.util.List;
public class LINQQueryExample {
public static class Student {
public String firstName;
public String lastName;
public int[] scores;
@aruld
aruld / TweetUtil.scala
Created October 7, 2012 06:34
exists in scala
object TweetUtil {
def main(args: Array[String]) {
val keywords = List("brown", "fox", "dog", "pangram")
val tweet = "The quick brown fox jumps over a lazy dog. #pangram http://www.rinkworks.com/words/pangrams.shtml"
keywords.exists(tweet.contains)
(keywords.foldLeft(false)( _ || tweet.contains(_) ))
}
}
@aruld
aruld / TweetUtil.java
Last active October 11, 2015 10:48
exists in Java 8
// take 1
keywords.stream().anyMatch(keyword -> tweet.contains(keyword));
// take 2 using method reference
keywords.stream().anyMatch(tweet::contains)
// take 3 using reduce()
keywords.stream().reduce(false, (Boolean b, String keyword) -> b || tweet.contains(keyword), (l, r) -> l | r);
@aruld
aruld / MyList.java
Last active October 11, 2015 10:48
Updated to compile with the latest lambda build 94.
package java.util.stream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
public class MyList<E> extends ArrayList<E> {
public MyList(int initialCapacity) {
super(initialCapacity);
@aruld
aruld / MyStream.java
Last active October 11, 2015 10:48
MyStream interface
package java.util.stream;
import java.util.function.Predicate;
public interface MyStream<T> extends Stream<T> {
default boolean exists(Predicate<? super T> predicate) {
return anyMatch(predicate);
}
}
@aruld
aruld / TweetUtil.java
Created October 7, 2012 06:07
exists implementation in plain old java
public class TweetUtil {
public static void main(String[] args) {
final List<String> keywords = Arrays.asList("brown", "fox", "dog", "pangram");
final String tweet = "The quick brown fox jumps over a lazy dog. #pangram http://www.rinkworks.com/words/pangrams.shtml";
exists(keywords, tweet);
}
public static boolean exists(List<String> keywords, String tweet) {
@aruld
aruld / MyReferencePipeline.java
Last active October 11, 2015 10:48
Updated to compile with the latest lambda build 94.
package java.util.stream;
import java.util.Spliterator;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class MyReferencePipeline<T, U> extends ReferencePipeline<T, U> implements MyStream<U> {
public MyReferencePipeline(Supplier<? extends Spliterator<?>> source, int sourceFlags, boolean parallel) {
super(source, sourceFlags, parallel);
@aruld
aruld / GoogleVsApple.java
Last active October 11, 2015 12:17
Filter tweets matching keywords using anyMatch() and reduce() operations on the Stream
List<String> google = Arrays.asList("android", "Android", "galaxy", "Galaxy", "nexus", "Nexus");
List<String> apple = Arrays.asList("ios", "iOS", "iphone", "iPhone", "ipad", "iPad");
List<Tweet> tweets = TweetReader.fromTweetSet(TweetReader.allTweets);
// Using anyMatch and method reference
List<Tweet> googleTweets = tweets.stream().filter(t -> (google.stream().anyMatch(t.text::contains))).collect(toList());
List<Tweet> appleTweets = tweets.stream().filter(t -> (apple.stream().anyMatch(t.text::contains))).collect(toList());
@aruld
aruld / GoogleVsApple.java
Last active October 11, 2015 12:17
MyList usage with exists() API
// Run with -Xbootclasspath/p:target/classes as ReferencePipeline access is now package local, so use this trick
List<Tweet> googleTweets3 = tweets.stream().filter(tweet -> (new MyList<>(google).stream().exists(tweet.text::contains))).collect(toList());
List<Tweet> appleTweets3 = tweets.stream().filter(tweet -> (new MyList<>(apple).stream().exists(tweet.text::contains))).collect(toList());