Skip to content

Instantly share code, notes, and snippets.

View AlexeyRaga's full-sized avatar

Alexey Raga AlexeyRaga

  • Arbor Networks
  • Sydney, Australia
View GitHub Profile
@AlexeyRaga
AlexeyRaga / Regex.hs
Created December 28, 2016 00:05
Simple naive regex matcher in Haskell
{-# LANGUAGE OverloadedStrings #-}
-- Code from https://www.schoolofhaskell.com/school/to-infinity-and-beyond/pick-of-the-week/a-regular-expression-matcher
import GHC.Exts (IsString(..))
data Regexp = Zero -- empty
| One -- epsilon
| Lit Char -- single character
| Plus Regexp Regexp -- union (+)
| Cat Regexp Regexp -- concatenation (.)
@AlexeyRaga
AlexeyRaga / tasks.json
Last active April 2, 2017 21:32
Haskell Stack + HSpec tasks for VSCode
{
"version": "0.1.0",
"command": "stack",
"suppressTaskName": true,
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": ["build"],
@AlexeyRaga
AlexeyRaga / Main.hs
Created February 17, 2016 10:07
[Haskell] RabbitMQ Conduit source
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
module Main where
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.ByteString.Char8 as BL (pack, unpack)
import Data.Conduit
import Data.Conduit.Binary (sinkHandle)
import Data.Conduit.List as CL
@AlexeyRaga
AlexeyRaga / Main.hs
Created February 14, 2016 10:34
Haskell decompress with error handling using Pipes
module Main where
import Data.ByteString (ByteString)
import Pipes
import Pipes.ByteString
import Pipes.GZip
import Pipes.Safe
import Pipes.Safe.Prelude (withFile)
import System.IO (IOMode(..))
@AlexeyRaga
AlexeyRaga / Main.hs
Last active February 14, 2016 03:36
Streaming ungzip with error handling
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Exception
import Control.Monad.Trans.Resource (runResourceT)
import Data.ByteString (ByteString)
import Data.Conduit (($$), ($=), catchC, yield)
import Data.Conduit.Binary
import Data.Conduit.Zlib
@AlexeyRaga
AlexeyRaga / wildcard-parsers-simple.hs
Created October 3, 2015 11:45
Simple example of wildcard expressions in Haskell
module Main where
data Result a = Success String a | Error deriving Show
data Parser a = Parser { parse :: String -> Result a }
--a parser that always succeeds with the provided value
success :: a -> Parser a
success a = Parser (\as -> Success as a)
--a parser that always fails
@AlexeyRaga
AlexeyRaga / gist:0e422563dcafa6282133
Created September 27, 2015 11:43 — forked from nikita-volkov/gist:6977841
Anonymous records. A solution to the problems of record-system.

#Anonymous records. A solution to the problems of record-system.

Please, beware that the proposal that follows has been implemented as a library.

The current record system is notorious for three major flaws:

  1. It does not solve the namespacing problem. I.e., you cannot have two records sharing field names in a single module. E.g., the following won't compile:

data A = A { field :: String }

let next = function
| d :: ds ->
let dss = Seq.append ds (Directory.GetDirectories d) |> List.ofSeq
let fss = Directory.GetFiles d
Some (fss, dss)
| [] -> None
Seq.unfold (next) ["/tmp"] |> Seq.concat
@AlexeyRaga
AlexeyRaga / imaginary.cs
Last active August 29, 2015 14:26
C# with types and options
//This is a weak point of C#: declaring classes is verbose.
//But it can be improved with C# 6 new syntax.
public class Age {
public int Value { get; private set; }
//Age is correct by construction,
//so if you have an Age then it is correct _by definition_ and you don't need to check anymore.
public Age(int value) {
if (!IsValidAge(value) throw new InvalidArgumentException("Age must be between 0 and 100");
Value = value;
@AlexeyRaga
AlexeyRaga / 1 - usage.scala
Last active August 29, 2015 14:24
Ackable Batching Streaming
import akka.actor.ActorSystem
import akka.stream.ActorFlowMaterializer
import akka.stream.scaladsl.Source
object Main extends App {
implicit val system = ActorSystem("mySystem")
implicit val materializer = ActorFlowMaterializer()
val srcProps = BatchingConsumingProducer.props(FakeKafkaConsumer.props, 5)