Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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: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: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: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: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.
-}
@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: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)]