Skip to content

Instantly share code, notes, and snippets.

View gdejohn's full-sized avatar

Griffin DeJohn gdejohn

View GitHub Profile
/// The future of Java:
/// - value classes
/// - all values are objects
/// - universal generics
/// - local-variable type inference
/// - records
/// - reconstructors
/// - sealed types
/// - switch expressions
/// - pattern matching
Comparison comparing<in Value>(Comparison(Value, Value)* comparators)(Value x, Value y)
=> comparators.spread(identity)(x, y).find(not(equal.equals)) else equal;
@gdejohn
gdejohn / Factorial.java
Last active January 31, 2018 07:05
Anonymous recursion in Java
import java.util.function.Function;
import java.util.function.UnaryOperator;
class Factorial {
static <T, R> Function<T, R> fix(UnaryOperator<Function<T, R>> operator) {
return operator.apply(argument -> fix(operator).apply(argument));
}
static Function<Integer, Integer> factorial = fix(f -> n -> n == 0 ? 1 : n * f.apply(n - 1));
}
@gdejohn
gdejohn / Partition.hs
Last active December 1, 2017 04:24
Enumerate partitions of a set with restrictions on the order, number, and sizes of parts.
-- |Enumerate restricted partitions of a set into a sequence of sets of subsets.
partition :: [(Int, Int)] -- ^ The i_th pair (k, n) specifies the number n of subsets in the i_th set and the size k of those subsets
-> [a] -- ^ The set to partition (the elements are assumed to be pairwise distinct)
-> [([[[a]]], [a])] -- ^ The partitions, each paired with the leftover elements from the original set
partition [] xs = [([], xs)]
partition _ [] = []
partition [(0, 1)] xs = [([[[]]], xs)]
partition [(1, 1)] (x : xs) =
([[[x]]], xs) : [(ysss, x : zs) | (ysss, zs) <- partition [(1, 1)] xs]
partition [(k, 1)] (x : xs) =
@gdejohn
gdejohn / queens.hs
Created April 11, 2017 16:22
Enumerate solutions to the n queens problem
import Control.Applicative (liftA2)
import Data.List (inits, permutations)
queens :: Int -> [[(Int, Int)]]
queens n = filter p $ liftA2 map zip permutations $ take n [0 ..]
where p x = and $ zipWith f x $ inits x
f (a, b) = all $ \(c, d) -> abs (a - c) /= abs (b - d)
@gdejohn
gdejohn / Expression.ceylon
Last active May 16, 2016 08:54
Ceylon solution to the expression problem.
"The datatype."
shared interface Expression {
"The initial operation."
shared formal Integer evaluate();
}
"The initial case of the datatype."
shared interface Literal satisfies Expression {
"The value of the literal expression."
shared formal Integer literal;
@gdejohn
gdejohn / cos.hs
Last active March 25, 2016 07:32
Fixed point of cosine function
x = fst $ fromJust $ find (uncurry (==)) $ liftA2 ($) zip tail $ iterate cos 1
import Data.List (nubBy)
primes = nubBy (((> 1) .) . gcd) [2..]
import Control.Applicative (liftA2)
import Control.Monad (guard)
import Data.Functor ((<$))
import Data.List (unfoldr)
import Data.Tuple (swap)
digits :: Integer -> [Integer]
digits = reverse . unfoldr (liftA2 (<$) (swap . (`divMod` 10)) (guard . (> 0)))
mapEveryNth n f = zipWith ($) $ tail $ cycle $ f : replicate (n - 1) id