Skip to content

Instantly share code, notes, and snippets.

@jnape
jnape / gist:2466342
Created April 22, 2012 19:33
Aggregate List of all ancestors for a particular type in Java, using foldLeft and recursion
package example;
import com.jnape.dynamiccollection.lambda.Accumulator;
import com.jnape.dynamiccollection.list.DynamicList;
import java.util.List;
import static com.jnape.dynamiccollection.DynamicCollectionFactory.list;
public class Example {
@jnape
jnape / gist:2571670
Created May 1, 2012 21:42
JavaScript Template
var Template = function(template) {
this.template = template;
this.bindings = {};
};
Template.prototype.bind = function(variable, value) {
this.bindings[variable] = value;
return this;
}
@jnape
jnape / gist:2623606
Created May 6, 2012 18:14
algorithm for creating groups from list in java
package com.jnape.dynamiccollection.operation;
import com.jnape.dynamiccollection.list.DynamicArrayList;
import com.jnape.dynamiccollection.list.DynamicList;
import java.util.List;
import static java.lang.Math.min;
public class InGroupsOf {
@jnape
jnape / gist:3266786
Created August 5, 2012 19:23
Functional solution for computing geometric mean in Java
package com.jnape.dynamiccollection;
import com.jnape.dynamiccollection.lambda.Accumulator;
import static com.jnape.dynamiccollection.factory.DynamicListFactory.list;
import static java.lang.Math.pow;
public class GeometricMean {
public static final Accumulator<Integer, Integer> TIMES = new Accumulator<Integer, Integer>() {
@jnape
jnape / gist:3805024
Created September 29, 2012 19:34
Levenshtein Ladders

Levenshtein Ladders: A Team Game with Which to Teach Levenshtein Differences

This is a derivative of a word ladders, or word "doublets", in which a word is intended to be transmuted into a different word through successive alterations of individual letters, presuming each alteration results in a real word, each time. The game can be further altered through the constraints of scoring based on Levenshtein Differences, a way to measure the difference between two words through the number or alterations necessary to morph the starting word into the destination word. Furthermore, the added constraint of reversal can be accomplished, but at the cost of a 2 point addition, and only under the condition that every word up to, but excluding, the last transformation must be a word, with the last transformation being the reverse of the destination word. Scoring is symmetric to golf, in which lower numbers are better scores, and higher numbers are worse scores.

Examples:

[Cat => Dog] Cat -> Cot (+1) -> Dot (+1)

@jnape
jnape / gist:3895906
Created October 15, 2012 22:09
"Run with Jasmine" TextMate CoffeeScript Command
# Using the Bundle Editor, add a new command for the CoffeeScript bundle
# Save: Nothing
# Input: Entire Document
# Output: Show as Tool Tip
# Command(s):
input="$TM_FILEPATH"
regex=".spec.(js|coffee)$"
jasmine=`which jasmine-node`
@jnape
jnape / gist:3925124
Created October 20, 2012 22:56
Leibniz formula for π using Dynamic Collections 1.2.8
package com.jnape.dynamiccollection;
import com.jnape.dynamiccollection.lambda.Function;
import com.jnape.dynamiccollection.list.DynamicList;
import static com.jnape.dynamiccollection.lambda.library.numeric.accumulator.Add.plus;
import static com.jnape.dynamiccollection.lambda.library.numeric.accumulator.Subtract.minus;
import static com.jnape.dynamiccollection.list.NumericDynamicArrayList.fromTo;
public class LeibnizSeries {
@jnape
jnape / gist:3984985
Created October 31, 2012 05:23
Symmetric set difference in Haskell
module Example (
(∆)
) where
unique :: (Eq a) => [a] -> [a]
unique [] = []
unique (x:xs) = x:(unique (filter (/= x) xs))
without :: (Eq a) => [a] -> [a] -> [a]
xs `without` ys = [ x | x <- xs, not (x `elem` ys)]
@jnape
jnape / gist:4013713
Created November 4, 2012 20:46
Solution to Project Euler #1 in Haskell
module Euler1 where
{-
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
-}
import Data.List (nub)
@jnape
jnape / gist:4013723
Created November 4, 2012 20:51
Solution to Project Euler #2 in Haskell
module Euler2 where
{-
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
-}