Skip to content

Instantly share code, notes, and snippets.

@rgantt
rgantt / itr-rev.java
Last active August 29, 2015 14:02
Musings in generic list reversal
public static <A> Node<A> reverse(final Node<A> head) {
Node<A> curr;
Node<A> previous = null;
while (head != null) {
curr = head.next;
head.next = previous;
previous = head;
head = curr;
}
return previous;
@rgantt
rgantt / tree-search-algorithms.scala
Last active August 29, 2015 13:58
Tree searches
package com.ryangantt.scala
import scala.collection.mutable.Queue
import scala.collection.mutable.ArrayBuffer
abstract class Element[+T]
case class Node[T](val left: Element[T], val right: Element[T], val value: Leaf[T]) extends Element[T]
case class Leaf[T](val value: T) extends Element[T]
case object Sentinel extends Element
@rgantt
rgantt / Makefile
Last active January 3, 2016 01:29
Grabs the stock price (from the Yahoo! finance API) of all passed symbols
all: atdgen stock_price
stock_price:
corebuild -pkg core_extended,netclient,yojson,atdgen stock_price.native
atdgen:
atdgen -j yfinance.atd
atdgen -t yfinance.atd
clean:
package com.ryangantt.monad;
/**
* Implementation of the identity monad
*/
public class Identity<T> {
public T value;
public T getValue() {
return value;
@rgantt
rgantt / monoid.java
Last active December 31, 2015 01:49
public interface Monoid<T> {
public T apply(T a, T b);
}
public class StringMonoid implements Monoid<String> {
public static final String IDENTITY = StringUtils.EMPTY_STRING;
public String apply(String a, String b) {
return a + b;
}
@rgantt
rgantt / list_filter.java
Last active December 29, 2015 04:59
functional programming fanservice
/**
* FP fanservice
*/
private interface Predicate<T> {
public boolean evaluate(final T item);
}
protected <T> Collection<T> filter(final Collection<T> collection, final Predicate<T> predicate) {
final Collection<T> filtered = new ArrayList<T>();
@rgantt
rgantt / Trie.java
Last active December 18, 2015 02:29
Trie in Java
package com.ryangantt.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@rgantt
rgantt / latency.txt
Created July 27, 2012 19:13 — 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
@rgantt
rgantt / alias-abuse.php
Created August 31, 2011 15:35
PHP 5.4.0alpha3 traits abuse
<?php
/**
* ============
* Defect: can alias-override class constructor, but only when using legacy constructor naming convention
* ============
*/
trait SomeTrait {
private function constructor() {
echo "this is abuse\n";
}
@rgantt
rgantt / closure-class.php
Created August 26, 2011 21:46
Closures in classes FTW
<?php
/** inside of a method */
$that = $this;
return $this->argmax( $candidates, function ( $w ) use ( $that ) {
$that->nwords[ $w ];
});