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
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: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
@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: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: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
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)
}
}
import Control.Concurrent.Lifted
import Control.Monad.Base
import Data.IORef.Lifted
import Data.Traversable
import qualified Data.HashSet as S
import qualified Data.HashMap.Strict as M
import System.IO.Unsafe
import Debug.Trace
:set prompt "ghci> "
@seanparsons
seanparsons / gist:32410870fd203bee4466
Created April 22, 2015 16:38
Trying to proxy some Thrift calls.
proxyCall :: (Typeable a) => String -> a -> Q Exp
proxyCall serviceName toProxy = do
let parameterCount n a' = case (typeRepArgs a') of
[x, xs] -> if tyConName $ typeRepTyCon x == "IO" then n else parameterCount (n + 1) xs
_ -> undefined
let noOfParameters = parameterCount 0 $ typeOf toProxy
fieldNames <- traverse (\n -> mkName ("a" ++ (show n))) [1..noOfParameters]
valueName <- fmap fromJust $ lookupValueName serviceName
return [p| $(valueName) resource $(fieldNames) = Pool.withResource pool (\(ThriftConnection _ client) -> $(toProxy) client $(fieldNames)) |]
@seanparsons
seanparsons / gist:51bc16518d933337cf5d
Created May 8, 2015 07:25
Baffling errors where GHC appears to be ignoring the types I've added to functions.
renderRoute :: HasRep as => S.Path as -> HVectElim as H.AttributeValue
renderRoute path =
let curryIt :: (HVect as -> H.AttributeValue) -> HVectElim as H.AttributeValue
curryIt = hVectCurry
uncurryIt :: HVectElim as Text -> HVect as -> Text
uncurryIt = hVectUncurry
renderIt :: S.Path as -> HVectElim as Text
renderIt = S.renderRoute
in curryIt $ fmap H.textValue $ uncurryIt $ renderIt path
type AppRWS a = RWST ReaderEnv () () IO a
type SpockTApp a = SpockT (RWST ReaderEnv () () IO) a