Skip to content

Instantly share code, notes, and snippets.

View seanparsons's full-sized avatar
💭
Setting A Status Message

Sean Parsons seanparsons

💭
Setting A Status Message
View GitHub Profile
implicit val myEnumDecodeJson: DecodeJson[MyEnum] = DecodeJson{hCursor =>
hCursor.focus match {
case `myEnumValue1Json` => DecodeResult.ok(MyEnumValue1)
case `myEnumValue2Json` => DecodeResult.ok(MyEnumValue2)
case _ => DecodeResult.fail("Expected MyEnum.", hCursor.history)
}
}
@seanparsons
seanparsons / gist:2c8a7ecd3b84e3019bd3
Created January 8, 2015 17:17
Haskell partial application pondering.
-- In this case it would be in theory be possible to create a single thunk for "ab" if pointless was partially applied as so:
-- pointless 1 2
-- The resulting Int -> Int method would then only have to evaluate ab once because otherwise it never changes.
-- The obvious possibility this could cause would be caching very large intermediate representations that would otherwise be
-- garbage collected.
pointless :: Int -> Int -> Int -> Int
pointless a b c =
let ab = a + b
in ab + c
@seanparsons
seanparsons / gist:5c1326d0e5a1721c9e8a
Created January 4, 2015 23:19
Baffling doctest error.
sean@Tower:~/workspace/rwst-wiring$ .cabal-sandbox/bin/doctest -isrc Control.Monad.Trans.Reader.Wiring
### Failure in src/Control/Monad/Trans/Reader/Wiring.hs:17: expression `:{
let composedResult = do
result1 <- promoteReader $ request1 "1"
result2 <- promoteReader $ request2 "2"
return [result1, result2] :: ReaderT (Database1, Database2) IO [String]
:}'
expected: >>> runReaderT composedResult (Database1, Database2)
["User1", "User2"]
but got:
@seanparsons
seanparsons / gist:c8a44e5fa9d70193f378
Created October 23, 2014 23:50
Monads: The Why, the What For, and especially the It's Like X, But!
-- To run this file you just need the ghc application from the Haskell Platform bundle
-- With that on a command line run the following (assuming the code is in Monads.hs):
-- ghc Monads.hs && ./Monads
-- Imports needed for later:
import System.IO
-- So pretty much every language has this concept:
-- doThingA();
-- doThingB();
-- If that wasn't hugely clear, it was the concept of calling one bit of code
@seanparsons
seanparsons / gist:f05e5b657a8197986dfa
Created July 16, 2014 17:22
List of failures or successes to a failure or list of successes.
scala> val list = List(1.right[String], "Fail".left[Int], 3.right[String])
list: List[scalaz.\/[String,Int]] = List(\/-(1), -\/(Fail), \/-(3))
scala> list.sequenceU
res1: scalaz.\/[String,List[Int]] = -\/(Fail)
scala> val list = List(1.right[String], 2.right[String], 3.right[String])
list: List[scalaz.\/[String,Int]] = List(\/-(1), \/-(2), \/-(3))
scala> list.sequenceU
path("metric" / Segment) { location =>
val _uri = "http://<url>/api/crowd_api/metric?mode=Select&location=" + location
val _result = scala.io.Source.fromURL(_uri).mkString
val _xml = XML.loadString(_result)
val _crowd = _xml \\ "crowd"
case class Crowd(c_location: String, c_metric: String)
implicit def CrowdCodecJson: CodecJson[Crowd] = casecodec2(Crowd.apply, Crowd.unapply)("c_location", "c_metric")
val crowd_ = _crowd.map{ n =>
@seanparsons
seanparsons / gist:7005543
Created October 16, 2013 10:08
Stacking ReaderT and OptionT.
import scalaz._,Scalaz._,scalaz.effect._,OptionT._,Kleisli._
type OptionTIO[+X] = OptionT[IO, X]
val reader1: ReaderT[Option, Int, String] = kleisli[Option, Int, String](n => n.toString.some)
val reader2: ReaderT[IO, Int, String] = kleisli[IO, Int, String](n => IO((n * 2).toString))
val convertedReader1: ReaderT[OptionTIO, Int, String] = reader1.mapK[OptionTIO, String](opt => optionT(IO(opt)))
val convertedReader2: ReaderT[OptionTIO, Int, String] = reader2.mapK[OptionTIO, String](io => io.liftM[OptionT])
@seanparsons
seanparsons / gist:6429556
Last active December 22, 2015 06:18
Possibly parallel and memoised fibonacci.
import Control.Parallel
nfib :: Int -> Int
nfib = (map fib [0 ..] !!)
where fib 0 = 0
fib 1 = 1
fib n = par n1 (pseq n2 (n1 + n2))
where n2 = nfib (n-2)
n1 = nfib (n-1)
@seanparsons
seanparsons / gist:6141023
Created August 2, 2013 15:54
Prettier ReaderT handling code.
def doThing[M[+_]: Monad, T, U, V]
(actionOne: () => ReaderT[M, T, Unit],
actionTwo: () => ReaderT[M, T, Unit],
actionThree: () => ReaderT[M, T, Unit],
actionFour: () => ReaderT[M, (T, U), Unit],
actionFive: () => ReaderT[M, T, Unit,
actionSix: () => ReaderT[M, V, Unit])
(): ReaderT[M, (T, U, V), Unit] = {
for {
_ <- actionOne().localFrom[(T, U, V)]
[error] (myproject/compile:compile) scala.reflect.internal.FatalError: class Universe does not have a member Quasiquote