Skip to content

Instantly share code, notes, and snippets.

View aruld's full-sized avatar
🎯
Focusing

Arul Dhesiaseelan aruld

🎯
Focusing
View GitHub Profile
Parallel.ForEach(students, student => {
student.GradePointAverage = student.Tests.Select(test => test.Grade * test.Weight).Sum();
} );
public static void MyParallelForEach<T>(IEnumerable<T> source, Action <T> body)
{
int numProcs = Environment.ProcessorCount;
int remainingWorkItems = numProcs;
using(var enumerator = source.GetEnumerator())
{
using(ManualResetEvent mre = new ManualResetEvent(false))
{
// Create each of the work items.
for (int p = 0; p < numProcs; p++) {
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.*;
public class Parallel {
static final Exception CONTINUE = new Exception("forEach Continue");
static {
CONTINUE.setStackTrace(new StackTraceElement[0]);
} // Delete irrelevant stack trace
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.*;
public class Parallel {
public static abstract class ForEach<O, K, I> {
protected static final Exception CONTINUE = new Exception("forEach Continue");
static {
CONTINUE.setStackTrace(new StackTraceElement[0]);
@aruld
aruld / StreamingProvider.java
Created February 8, 2013 08:07
Jetty ContentProvider
public interface StreamingProvider extends ContentProvider {
void write(OutputStream out) throws IOException;
}
@aruld
aruld / simple-5.0.4.patch
Created January 7, 2013 20:33
Patch for JERSEY-1641 (Upgrade to Simple 5.0.4 in Jersey 1.x)
Index: contribs/jersey-simple-server/src/main/java/com/sun/jersey/simple/impl/container/SimpleContainer.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- contribs/jersey-simple-server/src/main/java/com/sun/jersey/simple/impl/container/SimpleContainer.java (revision 5823)
+++ contribs/jersey-simple-server/src/main/java/com/sun/jersey/simple/impl/container/SimpleContainer.java (revision )
@@ -65,7 +65,7 @@
* This is the container that handles all HTTP requests. Requests are adapted
* for the enclosed {@link WebApplication} instances. This container can
@aruld
aruld / YFact.java
Created October 27, 2012 20:12 — forked from jonbodner/YFact.java
Generic Y Combinator in Java 8 using lambdas
//based on code from http://www.arcfn.com/2009/03/y-combinator-in-arc-and-java.html and the generic version https://gist.github.com/2571928
class YFact {
// T function returning a T
// T -> T
public static interface Func<T> {
T apply(T n);
}
// Higher-order function returning a T function
@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());
@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 / 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);