Skip to content

Instantly share code, notes, and snippets.

View benkolera's full-sized avatar

Ben Kolera benkolera

  • biza.io
  • Brisbane, Australia
View GitHub Profile
@benkolera
benkolera / sieve.scala
Created March 30, 2011 01:58
Prime Sieve. No arguments about whether 1 is prime... :p
import java.util.Calendar;
def sieve(primes:List[Int], possible:List[Int]):List[Int] =
possible match {
case Nil => primes
case firstPrime :: xs => {
sieve(firstPrime :: primes , xs.filter( _ % firstPrime != 0) )
}
}
import Test.QuickCheck
fizzBuzzinator a
| (mod a 15) == 0 = "fizzbuzz"
| (mod a 3) == 0 = "fizz"
| (mod a 5) == 0 = "buzz"
| otherwise = show a
fizzBuzz xs = unlines (map fizzBuzzinator xs)
@benkolera
benkolera / gist:1312659
Created October 25, 2011 13:05
Luhn Checksummed Numbers
import Data.Char
validateCC :: [Char] -> Bool
validateCC ccString =
let sum = luhnSum $ ccDigits ccString
in 0 == mod sum 10
ccDigits digits = reverse $ map digitToInt digits
luhnSum digits =
@benkolera
benkolera / gist:1318273
Created October 26, 2011 22:58
MaybeCake ... perl style. :) This one is nicer though: https://github.com/techtangents/maybecake/blob/master/MaybeCakeAnswer.hs
use MooseX::Declare;
use 5.10.1;
class Egg {}
class Coup {
has chook => ( is => 'ro' , isa => 'Chook' , required => 0);
}
class Chook {
@benkolera
benkolera / gist:1333486
Created November 2, 2011 12:14
Runtime Dependency Resolution ( by a haskell noob )
import qualified Data.Map as Map
import Data.List
import Control.Applicative
englishTranslator = ("ENGLISH: " ++)
germanTranslator = ("GERMAN: " ++)
windowsUnlines = intercalate "\r\n"
unixUnlines = intercalate "\n"
@benkolera
benkolera / ThisIsNotPerl.hs
Created December 17, 2011 07:39
Perl has broken my brain. This wont work; dummy!
prettyPrintDBInfo :: DBInfo -> String
prettyPrintDBInfo (DBInfo dbName (DBOptions useBString _ ) tbls) = unlines [
"DBInfo { ",
" dbName = \"" ++ dbName ++ "\"",
" , opts = DBOpts { useBString = " ++ (show useBString) ++ " }",
" , tbls = [" ,
(map prettyPrintTableInfo tbls) ,
" ] ",
"}"
]
@benkolera
benkolera / difm.hs
Created June 17, 2012 06:26
Downloads DI.fm playlists in pls format, optionally taking your listen key to download premium 256K mp3 lists.
{-# LANGUAGE TemplateHaskell #-}
import Data.Aeson (decode)
import Data.Aeson.TH (deriveFromJSON)
import Data.ByteString.Char8 (pack,unpack)
import Network.HTTP.Types (renderQuery)
import Data.Conduit.Binary (sinkFile)
import Control.Monad.IO.Class (liftIO)
import qualified Data.Conduit as C
import qualified Data.ByteString.Lazy as LBS
@benkolera
benkolera / gist:5737690
Created June 9, 2013 04:43
Overloading functions makes passing them in as arguments awkward.
import dispatch._ , Defaults._
import com.ning.http.client.{Request,AsyncHandler}
object Overloads {
def getWebPage(): Future[Res] = {
Http(url("http://www.google.com"))
}

Ben is a co-organiser of the Brisbane Functional Programming Group (bfpg.org) and a web app developer that is lucky enough to get to use Scala in his day job. Loves how static types and pure functional programming help to create code that is beautiful, concise and yet still can be reasoned about with minimal profanities.

@benkolera
benkolera / gist:5924340
Last active December 19, 2015 08:18
OptionT and State stack used to traverse an xml tree keeping history of where it went wrong.
import scalaz._
import std.option._
import syntax.monad._
import scala.xml.Node
object S {
// We stack OptionT on top of state to keep track of our state regardless
// of whether we find what we are looking for or not.
// Using OptionT has the awesome property that the monad will stop traversing