Skip to content

Instantly share code, notes, and snippets.

View duarten's full-sized avatar
🤷‍♂️

Duarte Nunes duarten

🤷‍♂️
View GitHub Profile
class C
{
static C()
{
// Let's run the initialization on another thread!
var thread = new System.Threading.Thread(Initialize);
thread.Start();
thread.Join();
}
static void Initialize() { }
@duarten
duarten / find.clj
Created February 14, 2013 11:47
A function to search Hiccup data structures.
(def ^{:private true} not-nil? (comp not nil?))
(defprotocol Matcher
"Protocol for types that can be used to search a Hiccup data structure."
(matches [this elem]))
(defn- comp-name [x y]
(and (not-nil? y) (= (name x) (name y))))
(extend-protocol Matcher
@duarten
duarten / Partitions.scala
Created April 22, 2013 09:32
For a given list, returns all the possible partitions of that list.
type Partition[A] = List[List[A]]
def partitions[A](xs: List[A]): List[Partition[A]] = l match {
case Nil => Nil
case h :: Nil => List(List(List(h)))
case h :: t =>
val ps = partitions(t)
ps.map(List(h) :: _) ::: ps.flatMap(interpolate(h, _))
}
@duarten
duarten / conditional.c
Last active December 16, 2015 17:19
Conditionals without control flow instructions.
int conditional(int x, int y, int z) {
int nx = !x - 1;
return (nx & y) | (~nx & z);
}
@duarten
duarten / nSumCombinations.hs
Created May 13, 2013 17:22
Given a list of integrals, find all combinations of `n` values in the list such that their sum is `s`.
nSumCombinations :: (Integral a) => Int -> a -> [a] -> [[a]]
nSumCombinations _ _ [] = []
nSumCombinations 1 s xs = map (\x -> [x]) $ filter (== s) xs
nSumCombinations n s (x:xs) = (map (x:) $ nSumCombinations (n - 1) (s - x) xs) ++ (nSumCombinations n s xs)
//harry huang [huanghui.huang@gmail.com]
//
//After reading the original post [http://jboner.github.com/2008/10/06/real-world-scala-dependency-injection-di.html, ]
//the original cake pattern seems quite verbose for me, and it is quite invasive, so I spent a bit time
//and come up with an improved version of cake pattern, which I call it "Auto Cake DI". It is working
//well with any POST(plain old scala trait)/POSO(plain old scala object) which means that anything can be
//injected without modification or introducing new traits.
/*---------inject trait---------*/
trait Inject[+T] { def inject: T }
@duarten
duarten / comments
Last active December 25, 2015 14:19
On page 3, 2nd column bottom: a = ⊥, b = 1 or a = 1 <- I think it should be 2.
On page 4, start of 2nd column: "applied to as applied to" seems to have an
extra "applied to".
On page 5, overview: it's said that it's the transaction that has sibling items,
but from the rest of the paper it would seem better to say that it's the version
that has sibling items.
On page 5, in the description of RAMP-List, can it be said that two versions are
import java.util.{Timer, TimerTask}
import scala.concurrent.duration._
import rx.lang.scala.{Observable, Observer, Subscription, Scheduler}
import rx.lang.scala.observables.ConnectableObservable
import rx.lang.scala.subscriptions.{CompositeSubscription, MultipleAssignmentSubscription}
import rx.lang.scala.schedulers.NewThreadScheduler
object RxExplorations{
implicit class SchedulerOps(val s: Scheduler) extends AnyVal {
@duarten
duarten / CLQ.java
Created February 17, 2014 13:53
Benchmarking ConcurrentLinkedQueue
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CyclicBarrier;
import static java.lang.System.out;
public class CLQ {
public static final long WARMUP_ITERATIONS = 100L * 1000L;
public static final long ITERATIONS = 25L * 1000L * 1000L;
static private long[] consumptions;
#!/bin/bash
#
# Bash must have been compiled with this ability: --enable-net-redirections
# The device files below do not actually exist.
# Use /dev/udp for UDP sockets
exec 3<>/dev/tcp/host/port
# Write to the socket as with any file descriptor
echo "Write this to the socket" >&3