Skip to content

Instantly share code, notes, and snippets.

# -*- coding: utf-8 -*-
import requests
from bottle import run, route
from bs4 import BeautifulSoup
import re
exp = re.compile(r'\b(\w{6})\b')
def replace_6chr_word(word):
if len(word) == 6:
return word + u"\u2122"
@hodzanassredin
hodzanassredin / ddd.fsx
Created February 9, 2016 15:58
ddd simplified
type Request<'i,'o,'k> = 'i * ('o -> 'k)
let bindRequest bind f (s,k) = s, fun v -> bind(k v,f)
type Id = int
type Entity<'e> = Entity of Id * 'e
[<Measure>] type money
type User = {name : string; email : string; ballance : int<money>}
type Product = { name : string; quantity : int; price : int<money>}
type Email = {body:string; subject : string}
@hodzanassredin
hodzanassredin / gist:d230313d762d508caee1
Last active February 4, 2016 14:32
reader option functor
let howIsMyName t f = f >> Option.map t
//mapReaderT :: (m a -> n b) -> ReaderT r m a -> ReaderT r n b
type Reader<'environment,'a> = 'environment -> 'a
//func application
let run environment (action : Reader<_,_>) = action environment
@hodzanassredin
hodzanassredin / combinators-precond.fsx
Created January 29, 2016 11:47
parser ccombinators with preconditions
open System
/// Type that represents Success/Failure in parsing
type Result<'a> =
| Success of 'a
| Failure of string
/// Type that wraps a parsing function
type Parser<'T> = Parser of (string -> Result<'T * string>)
@hodzanassredin
hodzanassredin / mon.fs
Last active May 23, 2017 09:16
from (co)monad to (co)monoid
type IntG<'a> = IntG of int * 'a
let fmap f (IntG(i,v)) : IntG<_>= IntG(i,f v)
let mzeroInt32 = 0
let mappendInt32 a b = a + b
let destroyInt32 a = ()
let pairInt32 a = (a,a)
//monad
let ret x = IntG(mzeroInt32, x)
@hodzanassredin
hodzanassredin / resources.fs
Last active January 25, 2016 15:24
(co)monoidal resource management in fsharp
module FileTest =
open System.IO
type Resource = Dispose of (unit -> unit)
let mzero = Dispose(id)
let mappend (Dispose(a)) (Dispose(b)) = Dispose(a >> b)
let destroy (Dispose(r)) = r()
let rec pair (Dispose(d)) =
let lockObj = new obj()
@hodzanassredin
hodzanassredin / ddd.fsx
Last active June 11, 2016 15:07
monadic ddd in fsharp
type Request<'i,'o,'k> = 'i * ('o -> 'k)
let bindRequest bind f (s,k) = s, fun v -> bind(k v,f)
type Id = int
type Entity<'e> = Entity of Id option * 'e
[<Measure>] type money
type User = {name : string; email : string; ballance : int<money>}
type Product = { name : string; quantity : int; price : int<money>}
type Email = {body:string; subject : string}
@hodzanassredin
hodzanassredin / dslinterpreter.fsx
Created January 14, 2016 15:20
free dsl cofree interpreter
module Dsl =
type AdderF<'k> =
| Add of int * (bool -> 'k)
| Clear of 'k
| Total of (int -> 'k)
let fmap f d =
match d with
| Add(x,k) -> Add(x,f << k)
| Clear k -> Clear (f k)
@hodzanassredin
hodzanassredin / comonoid.fsx
Last active January 13, 2016 11:02
(free)comonoids in fsharp
open System.IO
type CoMonoid<'a> = {destroy : 'a -> unit; clone : 'a -> 'a * 'a}
type FreeComonoid<'a> = 'a
let freeC = {
destroy =fun (c: FreeComonoid<'a>) -> ();
clone = fun (c: FreeComonoid<'a>) -> (c, c)
}
@hodzanassredin
hodzanassredin / freeMaybe.fsx
Created January 12, 2016 21:58
free maybe implementation
type Maybe<'a> =
| Some of 'a
| None
let fmap f m =
match m with
| Some(a) -> Some(f a)
| None -> None
type Free<'a> =