Skip to content

Instantly share code, notes, and snippets.

@jnape
jnape / List.hs
Created December 15, 2014 19:21
Haskell's missing core type classes
module MissingTypeClassesIntegration.List where
import MissingTypeClasses
import Prelude hiding (foldl, foldl1, foldr, foldr1, map, filter, zip)
instance RecursiveDataStructure [] where
unit = []
cons = (:)
instance LeftFoldable [] where
@jnape
jnape / BiFunctor.java
Last active August 29, 2015 14:20
Continuation-like iterable type
package com.jnape.palatable.lambda.applicative;
import com.jnape.palatable.lambda.functions.MonadicFunction;
import static com.jnape.palatable.lambda.functions.builtin.monadic.Identity.id;
@FunctionalInterface
public interface BiFunctor<A, B> {
default <C> BiFunctor<C, B> biMapL(MonadicFunction<? super A, ? extends C> fn) {
@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:4013726
Created November 4, 2012 20:52
Solution to Project Euler #3 in Haskell
module Euler3 where
{-
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
-}
factorOf :: Int -> Int -> Bool
a `factorOf` b = rem b a == 0