Skip to content

Instantly share code, notes, and snippets.

@jnape
jnape / List.hs
Created Dec 15, 2014
Haskell's missing core type classes
View List.hs
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 Aug 29, 2015
Continuation-like iterable type
View BiFunctor.java
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
JavaScript Template
View gist:2571670
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
algorithm for creating groups from list in java
View gist:2623606
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 Aug 5, 2012
Functional solution for computing geometric mean in Java
View gist:3266786
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 Sep 29, 2012
Levenshtein Ladders
View gist:3805024

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 Oct 15, 2012
"Run with Jasmine" TextMate CoffeeScript Command
View gist:3895906
# 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 Oct 20, 2012
Leibniz formula for π using Dynamic Collections 1.2.8
View gist:3925124
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 Oct 31, 2012
Symmetric set difference in Haskell
View gist:3984985
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 Nov 4, 2012
Solution to Project Euler #3 in Haskell
View gist:4013726
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