Skip to content

Instantly share code, notes, and snippets.

View benkolera's full-sized avatar

Ben Kolera benkolera

  • biza.io
  • Brisbane, Australia
View GitHub Profile
@benkolera
benkolera / gist:8817421
Created February 5, 2014 04:27
Argonaut Decode Directive
def decodeJson[A:DecodeJson] = Directive{ req:HttpRequest[Any] =>
def error(msg:String) = Result.Error(
BadRequest ~> ResponseString("Invalid JSON request: " + msg)
)
JsonRequest(req).decode[A] match {
case -\/(-\/(invalidJsonErr)) => error(invalidJsonErr)
case -\/(\/-((decodeFailErr,_))) => error(decodeFailErr)
case \/-(a) => Result.Success(a)
}
@benkolera
benkolera / gist:8914951
Created February 10, 2014 12:24
Gotta be a better way. Also, PLens feels weird with the setter being an Option (compared to Control.Lens.Prism, at least).
def eitherPLens[X,A,B](
lensLeft: X @?> A ,
lensRight: X @?> B
): X @?> (A \/ B) = PLens.plensg(
x => Some(
_.fold( lensLeft.setOr( x , _ , x ), lensRight.setOr( x , _ , x) )
),
x => lensLeft.get( x ).map( -\/[A](_) ).orElse(
lensRight.get( x ).map( \/-[B](_) )
)
@benkolera
benkolera / gist:9384051
Last active August 29, 2015 13:57
scalaz-stream Exchange.run
import scalaz._
import scalaz.stream._
import scalaz.stream.actor._
object Foo {
// I need to create a bidirectional link between a websocket and a tcp socket.
// To do the TCP side, I was hoping to use the new NIO stream from scalaz-stream (snapshot-0.4)
// The problem that I am hitting, though, is that I will have the requirement
@benkolera
benkolera / Main.hs
Last active June 9, 2017 19:12
Websocket <-> TCP Proxy : First Go
module Main where
import Prelude hiding (mapM_)
import Control.Concurrent (forkIO)
import Control.Concurrent.STM (STM,atomically)
import Control.Concurrent.STM.TQueue
( TQueue
, newTQueue
, readTQueue
, writeTQueue )
@benkolera
benkolera / gist:9655952
Created March 20, 2014 02:17
Example of serialising raw scala collections.
scala> import argonaut._, Argonaut._
import argonaut._
import Argonaut._
scala> Map( "foo" -> List("bar","baz") , "test" -> List() ) .jencode.spaces2
res7: String =
{
"foo" : [
"bar",
"baz"
@benkolera
benkolera / gist:9719910
Last active February 22, 2019 15:17
Installing GHC 7.8 on centos6 + EPEL 7.8
import scalaz._
import syntax.std.option._
import syntax.monad._
import syntax.show._
import xml._
import cursor._
import Xml._
object DecoderTest {
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import argonaut._
import Argonaut._
object InvariantFunctorBug {
case class RecordMeta(
externalId:String,
import argonaut._ , Argonaut._
object Recursive {
val jsonStr0 = """{
| "name": "name1",
| "entities": []
|}
""".stripMargin
@benkolera
benkolera / beginner_map.md
Last active August 29, 2015 14:01
FP Beginners Map Idea

An idea came up at lambda jam that I'm not sure whether it is feasible or even useful. A recurring problem that people seem to have (including me) is the sheer breadth of concepts in FP and the difficulty that that presents when trying to prioritise what they should learn next to get closer to being able to solve their problems. The idea was some kind of map / "fp skill tree" which could be used to get a high level picture of the ecosystem and how all the bits related.

For instance, a question that this tool could be used to answer would be:

  • I have a problem where I have some kind of longish computation and which I need to interleave effects into without making my code ugly and hard to compose. What can I use for that?
  • Pipes looks like a good start for this, but what prerequisite knowledge will I need to fully grok pipes? Where can I find documentation, books and tutorials on pipes?
  • Whoa, that depends on lens for some things and lens looks conceptually wide and dense, do I actually need to fully under