Skip to content

Instantly share code, notes, and snippets.

@hodzanassredin
hodzanassredin / free.fsx
Created January 12, 2016 16:04
free tests
type Toy<'r, 'next> =
| Output of 'r * 'next
| Bell of 'next
| Done
let fmap f a =
match a with
| Output (x,next) -> Output (a,f next)
| Bell(next) -> Bell(f next)
| Done -> Done
@hodzanassredin
hodzanassredin / cmarray.fsx
Last active January 10, 2016 22:12
fsharp comonad monad isomorphism
module CArray =
type CArray<'a> = CA of 'a array * int
let fmap f (CA(a, i)) = CA(Array.map f a, i)
let extract (CA(a, i)) = a.[i]
let extend f (CA(a, i)) =
let es' = Array.mapi (fun i _ -> f (CA(a,i))) a
in CA(es',i)
module MArray =
type CA<'a> = CA of 'a array * int
@hodzanassredin
hodzanassredin / image.fsx
Last active January 11, 2016 15:42
image processing comonad
[<AutoOpen>]
module Helpers =
let memoize f =
let dict = new System.Collections.Generic.Dictionary<_,_>()
fun n ->
match dict.TryGetValue(n) with
| (true, v) -> v
| _ ->
let temp = f(n)
dict.Add(n, temp)
@hodzanassredin
hodzanassredin / comonad.fsx
Last active January 8, 2016 11:37
comnads in fsharp
//extend :: (w a -> b) -> w a -> w b
//duplicate :: w a -> w (w a)
//extract :: w a -> a
module CoReader =
type CoReader<'e, 'a> = CoReader of env : 'e * value : 'a
let askC (CoReader(env, _)) = env
let extract (CoReader(_, value)) = value
let extend f w = CoReader(askC w, f w)
let duplicate w = extend id w
@hodzanassredin
hodzanassredin / ast.fsx
Last active January 21, 2016 19:48
ast builder
type Request<'i,'o,'k> = 'i * ('o -> 'k)
let bindRequest bind f (s,k) = s, fun v -> bind(k v,f)
type Toy<'r> =
| Output of Request<string,unit,Toy<'r>>
| Input of Request<unit, string,Toy<'r>>
| Pure of 'r
type ToyBuilder() =
member x.Bind(v:Toy<'a>,f:'a->Toy<'b>) =
@hodzanassredin
hodzanassredin / free.fs
Created January 4, 2016 21:10
fsharp free monad proto
// Learn more about F# at http://fsharp.net
// See the 'F# Tutorial' project for more help.
type Prj<'a> = Async<Option<'a>>
type ProjectBuilder() =
member this.Bind(x, f) =
async{
let! x = x
match x with
@hodzanassredin
hodzanassredin / patch.py
Created December 25, 2015 14:14
4x speedup in classification
from sklearn.feature_selection.base import SelectorMixin
#sklearn fix
def get_support(self, indices=False):
if hasattr(self, 'mask_cache'):
return self.mask_cache
mask = self._get_support_mask()
self.mask_cache = mask if not indices else np.where(mask)[0]
return self.mask_cache
SelectorMixin.get_support = get_support
@hodzanassredin
hodzanassredin / AsyncTcpListener.fs
Last active December 21, 2015 13:38
tcplistener benchmark async 4k
// Step 0. Boilerplate to get the paket.exe tool
open System
open System.IO
open System.Net
open System.Net.Sockets
open System.Threading
//open Hopac
module ServerM =
@hodzanassredin
hodzanassredin / common-session
Last active December 21, 2015 08:15
suave test
session [default=1] pam_permit.so
session requisite pam_deny.so
session required pam_permit.so
session optional pam_umask.so
session required pam_unix.so
session optional pam_systemd.so
session required pam_limits.so
@hodzanassredin
hodzanassredin / azure_cli.py
Last active November 12, 2015 10:47
python wrapper for azure cli
# -*- coding: utf-8 -*-
import subprocess
import logging
import tempfile
import json
import datetime
azure_cli = "\"C:\\Program Files (x86)\\Microsoft SDKs\\Azure\\CLI\\wbin\\azure.cmd\""
def exec_azure_cli_cmd(cmd, skip_start_lines, input = ""):