Skip to content

Instantly share code, notes, and snippets.

View monkeygroover's full-sized avatar

Richard Bowker monkeygroover

View GitHub Profile
object ScalaJSExample extends js.JSApp{
def main() = {
val xs = Seq(1, 2, 3)
println(xs.toString)
val ys = Seq(4, 5, 6)
println(ys.toString)
val zs = for{
x <- xs
y <- ys
} yield x * y
import shapeless._
import spray.json._
object Main extends App {
case class Test(optionalBlah: Option[String])
case class Test2(nested: Test)
import DefaultJsonProtocol._
implicit val testFormat = DefaultJsonProtocol.jsonFormat1(Test)
@monkeygroover
monkeygroover / gist:0e4e2f5648b91c3bc6c4
Last active August 29, 2015 14:23
shapelessspray
import org.ensime.json.FamilyFormats
import spray.json._
object MainApp extends App {
case class Foo(blah: String, blah2: Option[String])
case class Bar(boo: List[Foo])
val wat = Bar(Foo("dfsdf", Some("dsfd")) :: Foo("feefef", None) :: Nil)
@monkeygroover
monkeygroover / gist:189696a2cf821e0cdf01
Created November 5, 2015 23:01
Either to Coproduct
case class ResultA()
case class ResultB()
case class ResultC()
def test(): Future[Either[ResultA, ResultB]] = ???
type Results = ResultA :+: ResultB :+: ResultC :+: CNil
val foo = for {
x <- test()
case class PermutingCheckout(skuRules: Map[String, SKUPricer]) extends TotalCalculator {
// as above but runs the rules in all permuted orders to find the best order to minimise the price
val calculateTotal = (items: List[String]) =>
items.foldMap(x => Map(x -> 1)) // create a map of SKU -> count of 'scanned' items
.map { case (sku, skuCount) =>
// get the rules for the SKU (if they exist) and map them to get the results for each SKU group
skuRules.get(sku).map(rule => (rule.getPrice(skuCount)).success)
.getOrElse(s"'$sku' rule not found".failureNel)
}.reduce(_ |+| _) // sum the results for each SKU to get the final total
}
@monkeygroover
monkeygroover / gist:fe12b37d63cf5c33f37a
Last active March 10, 2016 14:27
Kleisli compositions
val foo = (n: Int) => n.toString
val bar = (s: String) => s.toInt
// composing
val f0 = bar compose foo
println(f0(3))
//what if the functions could fail..
let factors number = seq {
for divisor in 1 .. (float >> sqrt >> int) number do
if number % divisor = 0 then
yield divisor
if number <> 1 then yield number / divisor //special case condition: when number=1 then divisor=(number/divisor), so don't repeat it
}
let rec triangle n = match n with
| 0 -> 0
| 1 -> 1
@monkeygroover
monkeygroover / euler14.fsx
Last active March 30, 2016 21:00
euler14
//n → n/2 (n is even)
//n → 3n + 1 (n is odd)
let collatzSeq = Seq.unfold (fun n -> match n with
| 0L -> None
| 1L -> Some(1L, 0L)
| n -> Some(n, if n % 2L = 0L then n/2L else 3L*n+1L) )
let r = {1L..999999L} |> Seq.map(fun i -> (i, Seq.length(collatzSeq(i)))) |> Seq.maxBy(fun (i,s) -> s)
printfn "%A" r
@monkeygroover
monkeygroover / euler16.fsx
Last active March 30, 2016 20:59
euler16
let units bi = (bi % 10I, bi/10I)
let rec sum bi = match units(bi) with
| (u, r) when r = 0I -> u
| (u, r) -> u + sum(r)
let result = sum(pown 2I 1000)
printfn "%A" result
let units bi = (bi % 10I, bi/10I)
let rec sum bi = match units(bi) with
| (u, r) when r = 0I -> u
| (u, r) -> u + sum(r)
let rec fac(n:int): bigint = match n with
| 1 -> bigint(1)
| n -> bigint(n) * fac(n - 1)