Skip to content

Instantly share code, notes, and snippets.

Chris Lewis chrislewis

Block or report user

Report or block chrislewis

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View sumTwoTrailRec.hs
sumTwoTrailRec (x : xs) (y : ys) total carry =
sumTwoTrailRec xs ys ((mod sum 10) : total) nextCarry where
sum = x + y + carry
nextCarry = div sum 10
sumTwoTrailRec (x : xs) [] total carry =
sumTwoTrailRec xs [] ((mod (x + carry) 10) : total) (div (x + carry) 10)
sumTwoTrailRec [] (y : ys) total carry =
sumTwoTrailRec [] ys ((mod (y + carry) 10) : total) (div (y + carry) 10)
sumTwoTrailRec [] [] total carry =
reverse $ if carry > 0 then (carry : total) else total
View addTwoNumbers.hs
-- solves bug of (9 + 1) in the case of a carry and mismatched list lengths
addTwoNumbers (l1 : tail1) (l2 : tail2) carry =
(mod sum 10) : (addTwoNumbers tail1 tail2 newCarry) where
sum = l1 + l2 + carry
newCarry = div sum 10
addTwoNumbers (l1 : tail1) [] carry =
(mod (l1 + carry) 10) : (addTwoNumbers tail1 [] (div (l1 + carry) 10))
addTwoNumbers [] (l2 : tail2) carry =
(mod (l2 + carry) 10) : (addTwoNumbers [] tail2 (div (l2 + carry) 10))
addTwoNumbers [] [] carry =
View SumTwoFoldl.hs
sumTwoFoldl :: [Int] -> [Int] -> [Int]
sumTwoFoldl x y =
let (sum, carry) = foldl accum ([], 0) $ zip x y
accum (total, carry) (x, y) =
let t = x + y + carry
(n, nc) =
if t > 9
then (t - 10, 1)
else (t, 0)
in (n : total, nc)
View SumTwo.hs
sumTwo :: [Int] -> [Int] -> [Int]
sumTwo x y = doit x y [] 0
where
doit [] [] sum 0 = reverse sum
doit [] [] sum carry = doit [] [] (carry : sum) 0
doit (x : xs) (y : ys) sum carry =
let tot = x + y + carry
(n, nc) =
if tot > 9
then (tot - 10, 1)
@chrislewis
chrislewis / gist:3416494
Created Aug 21, 2012
reader monad for java.util.Properties example
View gist:3416494
/* Start by creating a reader for some fake yet conceivable type for executing SQL queries. */
val dbReader: Reader[Properties, JdbcExecutor] =
for {
driver <- read[String]("db.driver")
uri <- read[String]("db.uri")
user <- read[String]("db.username")
password <- read[String]("db.password")
name <- read[String]("db.pool.name")
minCons <- read[Int]("db.pool.minConnections")
View OneOf3.scala
/** A boxed union of 3 types; same principle applies for > 3. */
sealed trait OneOf3[A, B, C] {
/** There are plenty of useful methods we could define, but folds provides
an alternative to explicit pattern matching and a basis for other
useful things. */
def fold[X](fax: A => X, fbx: B => X, fcx: C => X): X
}
/* Implementations are straight forward: */
@chrislewis
chrislewis / gist:4409778
Created Dec 29, 2012
A Reader Either monad transformer for java.util.Properties with Scalaz7
View gist:4409778
/*
* This is a rendition of https://gist.github.com/3416494. Scalaz7 provides the
* Reader and we take it a step further by constructing and using a Reader Either
* monad transformer with pure error handling, instead of the original impure Reader
* monad that would throw exceptions on parse errors.
*/
import scalaz._
import Scalaz._
import java.util.Properties
@chrislewis
chrislewis / market_maker.pl
Created Oct 12, 2012
Andrew's market-making bot in perl
View market_maker.pl
#!/usr/bin/perl
use JSON;
use IO::Socket;
my $pid=$$;
my $user = "marketmaker";
my $pass = "liquidity";
my $host = "10.10.9.115";
my $port = "10000";
View gist:2919454
package com.example.foobie
import unfiltered.jetty.Http
import unfiltered.response._
import unfiltered.request._
trait Resolver[A] {
type B
}
@chrislewis
chrislewis / comprehensible.scala
Created Mar 11, 2012
general type and implicit conversion to add support for using a general Bind instance in a for comprehension
View comprehensible.scala
trait Bind[M[_]] {
def fmap[A, B](ma: M[A], f: A => B): M[B]
def bind[A, B](ma: M[A], f: A => M[B]): M[B]
}
trait Monad[M[_]] extends Bind[M]{
def unit[A](a: => A): M[A]
You can’t perform that action at this time.