Skip to content

Instantly share code, notes, and snippets.

@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
@jnape
jnape / gist:4173874
Created November 30, 2012 05:13
Fibonacci numbers using Streams in Dynamic-Collections v1.2.9
package com.jnape.dynamiccollection;
import com.jnape.dynamiccollection.lambda.Function;
import com.jnape.dynamiccollection.list.CachingStream;
import com.jnape.dynamiccollection.list.DynamicList;
import com.jnape.dynamiccollection.list.Stream;
import static com.jnape.dynamiccollection.lambda.library.numeric.accumulator.Add.add;
import static com.jnape.dynamiccollection.list.NumericDynamicArrayList.numbers;
@jnape
jnape / gist:4277824
Last active October 14, 2015 00:08
Sieve of Eratosthenes as infinite list of primes in Haskell
module Primes where
main :: IO ()
main = print $ take 1000 primes
(√) :: Int -> Int
(√) = floor . sqrt . fromIntegral
primes :: [Int]
primes = 2 : sieve [3, 5..]
@jnape
jnape / gist:4366618
Created December 23, 2012 23:06
Prime factorization in Haskell using unfoldr
module PrimeFactorization where
import Data.List
primeFactorsOf :: Int -> [Int]
primeFactorsOf n = (nub . unfoldr (2 `factorOutOf`)) n
where k `factorOutOf` n
| n < 2 = Nothing
| n `mod` k == 0 = Just (k, n `div` k)
| otherwise = (k+1) `factorOutOf` n
@jnape
jnape / gist:4673922
Created January 30, 2013 15:16
Hack night run-length compression snippet
module Snippet where
import Data.List
compress :: String -> String
compress = (foldl1 (++)) . (foldl (\acc xs -> acc ++ [(show.length) xs, (head xs):[]]) []) . group
compressions :: String -> [String]
compressions x = let n = compress x in n:compressions n
@jnape
jnape / groupsort.hs
Last active December 13, 2015 20:18
Haskell implementation of GroupSort
module GroupSort (
groupsort
) where
groupsort :: Ord a => [a] -> [a]
groupsort [] = []
groupsort xs = let
groups = group xs
n = length xs
k = length groups
@jnape
jnape / FutureEither.scala
Created April 3, 2013 09:03
Making dealing with Future[Either[L, R]] palatable in Scala
package com.thoughtworks.futureeither
import concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import concurrent.duration.FiniteDuration
import java.util.concurrent.TimeUnit
class FutureEither[L, R](private val future: Future[Either[L, R]]) {
def flatMap[R2](block: R => FutureEither[L, R2]): FutureEither[L, R2] = {
@jnape
jnape / Brainfuck.scala
Last active December 15, 2015 20:19
A Brainfuck interpreter written in Scala
package com.thoughtworks.futureeither
import scala.collection.mutable
abstract sealed class LoopOutcome {
def endOrElse(fn: => Any) = this match {
case Repeat() => fn
case _ =>
}
@jnape
jnape / use_mocking.py
Created June 12, 2013 16:42
Python Decorator that automatically unstubs all Mockito stubs after each test case
from unittest2 import TestCase
from mockito import when, unstub
from uuid import UUID
import uuid
def use_mocking(test_suite):
wrapped_tearDown = test_suite.tearDown if 'tearDown' in dir(test_suite) else lambda self: True
def tearDown(self):
@jnape
jnape / who_does_the_deployment.pro
Created August 6, 2013 05:29
ThoughtWorks P2, Issue 03 Puzzle Solution
% The person who has to deploy to Mobile is not Max or Sam
% Neither Max or Charlie are fans of Copying Files
% The one who deploys quarterly does so to a Mobile target
% Andy deploys weekly
% The person making use of a third party PAAS is not Andy or Charlie
% Sam is either the Copying Files or the System Package enthusiast
% The person who makes VM Images is not Charlie and doesn’t own deploy to a PAAS
% The person who deploys to the PAAS is does so more frequently than the person who deploys to the Desktop
% Of the System Packaging fan and Charlie, one deploys monthly and the other deploys to Mobile
% The person who deploys to the IAAS is not Max