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 fibSeq = Seq.unfold (fun s -> Some(fst s, (snd s, fst s + snd s))) (1I,1I)
let result = fibSeq
|> Seq.takeWhile(fun x -> x < pown 10I 999)
|> Seq.length
|> (+) 1
printfn "%A" result
type DivisorType = Deficient | Perfect | Abundant
let divisors number = {1 .. number/2} |> Seq.filter(fun n -> number % n = 0)
let sumDivisors = divisors >> Seq.sum
let divisorSumType number = match (number - sumDivisors number) with
| 0 -> Perfect
| d when d > 0 -> Deficient
| d -> Abundant
@monkeygroover
monkeygroover / euler22.fsx
Last active March 30, 2016 20:59
euler22
open System.IO
let wordValue (word: string) = Seq.map(fun c -> (int c) - (int 'A') + 1) word
|> Seq.sum
let result = File.ReadLines("p022_names.txt")
|> Seq.collect(fun line -> line.Split ',')
|> Seq.map(fun quotedWord -> quotedWord.Trim [|'"'|] )
|> Seq.sort
|> Seq.mapi(fun i word -> (i + 1) * wordValue word)
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 d n = Seq.sum(factors n) - n
let all = {1..9999} |> Seq.map(fun a -> (a, d(a))) |> Map.ofSeq