Skip to content

Instantly share code, notes, and snippets.

View nikita-volkov's full-sized avatar

Nikita Volkov nikita-volkov

View GitHub Profile
import Control.Proxy
import Control.Proxy.TCP
main :: IO ()
main = serve (Host "0.0.0.0") "8000" $ \(socket,_) ->
runProxy $ socketReadS 4096 socket >-> socketWriteD socket
@pthariensflame
pthariensflame / IndexedState.md
Last active June 15, 2022 18:42
An introduction to the indexed state monad in Haskell, Scala, and C#.

The Indexed State Monad in Haskell, Scala, and C#

Have you ever had to write code that made a complex series of succesive modifications to a single piece of mutable state? (Almost certainly yes.)

Did you ever wish you could make the compiler tell you if a particular operation on the state was illegal at a given point in the modifications? (If you're a fan of static typing, probably yes.)

If that's the case, the indexed state monad can help!

Motivation

import language.experimental.macros
import language.implicitConversions
import scala.reflect.macros.Context
import scala.reflect.runtime.universe.Tree
class ReflectiveClosure[A, B](val tree: Tree, fn: Function1[A, B]) extends Function1[A, B] {
def apply(x: A) = fn(x)
}
object ReflectiveClosure {
anonymous
anonymous / gist:4450690
Created January 4, 2013 07:35
import scala.reflect.macros.Context
import language.experimental.macros
case class EnclosingApplication(tree: scala.reflect.runtime.universe.Tree)
object Macros {
def impl(c: Context): c.Expr[EnclosingApplication] = {
import c.universe._
import treeBuild._
@jvranish
jvranish / valueLevelClasses.hs
Created May 2, 2012 17:28
An experiment with mixing value level typeclasses and implicit parameters
{-# LANGUAGE Rank2Types
, RebindableSyntax
, ImplicitParams
, NoMonomorphismRestriction #-}
import Data.Maybe
import Data.Function
import Data.String
import Prelude (undefined, error, String, (++))
@xeno-by
xeno-by / gist:2559714
Created April 30, 2012 16:19
Mixing in a trait dynamically
Answers http://stackoverflow.com/questions/10373318/mixing-in-a-trait-dynamically.
Compile as follows:
scalac Common_1.scala Macros_2.scala
scalac Common_1.scala Test_3.scala -cp <path to the result of the previous compilation>
Tested in 2.10.0-M3, will most likely not compile by the time 2.10.0 final is released, because we're actively rehashing the API.
However the principles will remain the same in the final release, so the concept itself is okay.
upd. Code updated for 2.10.0-M7.
upd. Code updated for 2.10.0-RC1.
@nuxlli
nuxlli / sublime_text_2_useful_shortcuts.md
Created September 9, 2011 18:51 — forked from lucasfais/gist:1207002
Sublime Text 2 - Useful Shortcuts

Sublime Text 2 - Useful Shortcuts

Tested in Mac OS X: super == command

Open/Goto


  • super+t: go to file
  • super+ctrl+p: go to project
  • super+r: go to methods
@retronym
retronym / type-bounds.scala
Created December 16, 2009 11:17
Tour of Scala Type Bounds
class A
class A2 extends A
class B
trait M[X]
//
// Upper Type Bound
//
def upperTypeBound[AA <: A](x: AA): A = x