Skip to content

Instantly share code, notes, and snippets.

View SPY's full-sized avatar

Ilya Rezvov SPY

  • Google
  • Denver, USA
View GitHub Profile
@SPY
SPY / rbtree.ml
Last active December 16, 2015 20:54
Naive implementation of red-black tree with OCaml
module RBTree : sig
type 'a t
val empty : 'a t
val insert : 'a t -> 'a -> 'a t
val exists : 'a t -> 'a -> bool
val remove : 'a t -> 'a -> 'a t
val fold_left : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b
val fold_right : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b
val balanced : 'a t -> bool
val from_list : 'a list -> 'a t
@SPY
SPY / App.tsx
Last active November 18, 2015 10:56
import {actionCreators, Action} from '../actions/Action'
export class App extends React.Component<AppProps, {}> {
public static childContextTypes: any = {}
private getChildContext() {
const context: any = {}
const props = this.props as any
Object.keys(this.props).forEach(prop => {
if (typeof props[prop] == 'function' && prop in actionCreators) {
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}
-- works too
class Message params msg | msg -> params, params -> msg where
create :: params -> msg
data FooMessage = FooMessage { from, to :: Int } deriving (Show)
data FooMessageParams = FooMessageParams { _from, _to :: Maybe Int }
import asyncdispatch
type KeyNotFoundError* = object of IOError
proc get*(key: string): Future[string] =
let res = newFuture[string]()
sleepAsync(500).callback = proc(future: Future[void]) {.closure, gcsafe.} =
if key != "hello":
res.fail(newException(KeyNotFoundError, "Key " & key & " is not found"))
else:
proc get*(client: MemcacheAsyncClient, key: string): Future[string] =
let res = newFuture[string]()
client.sendCommand(CommandOpcode.Get, key = key.toRawData()).callback = proc(future: Future[Response]) {.closure, gcsafe.} =
let response = future.read()
if response.header.status == ResponseStatus.KeyNotFound:
res.fail(newException(KeyNotFoundError, "Key " & key & " is not found"))
else:
res.complete(response.value)
res
proc enumDef(x: NimNode): NimNode =
result = quote do:
type `x` = enum enOne, enTwo
macro one(x): stmt {. immediate .} =
echo treeRepr(x)
result = enumDef(x)
echo treeRepr(result)
one SuperEnum
@SPY
SPY / netdef.nim
Last active August 29, 2015 14:23
import macros, strutils
from sockets import ntohl, ntohs, htons, htonl
import endians
export sockets.ntohl
export sockets.ntohs
export sockets.htonl
export sockets.htons
const bigInts = ["int16", "uint16", "int32", "uint32", "int", "uint", "int64", "uint64"]
import parseopt, strutils, math
type
Options = object
a, b, step: float
report: int
filename: string
DataPoint = tuple[x: float, y: float]
Data = seq[DataPoint]
import Control.Applicative ((<$>))
import Data.List (partition)
import Data.Map (Map)
import qualified Data.Map as M
type Edge = [Int]
data Color = Red | Black deriving (Show, Eq)
type Paint = Map Int Color
import Control.Applicative ((<$>))
import Data.List (partition)
type Edge = [Int]
findCycle :: [Int] -> [Edge] -> Maybe [Int]
findCycle vs es =
if (not $ all (even . length . adjacent es) vs) || (segmentsCount vs es > 1)
then Nothing
else Just $ let (cycle, rest) = buildCycle (head vs) es in builder cycle rest