Skip to content

Instantly share code, notes, and snippets.

@hodzanassredin
hodzanassredin / Alternatives.fs
Last active August 29, 2015 14:21
hopac alternatives on top of async
module Promise =
open System.Threading.Tasks
type Promise<'a> = {signal: 'a -> bool;
future : Async<'a>;
cancel : unit -> bool}
let create<'a> () =
let tcs = new TaskCompletionSource<'a>()
let ta: Async<'a> = Async.AwaitTask tcs.Task
{signal = tcs.TrySetResult;
future = ta;
@hodzanassredin
hodzanassredin / Alternatives.fs
Last active May 13, 2020 22:16
hopac alternatives in fsharp
open System
module AsyncExt =
open System.Threading
let choice workflows =
Async.FromContinuations(fun (cont, _, _) ->
let cts = new CancellationTokenSource()
let completed = ref false
let lockObj = new obj()
let synchronized f = lock lockObj f
@hodzanassredin
hodzanassredin / Reducers.fs
Last active August 29, 2015 14:19
Simple reducers and Nessos streams in fsharp for blogpost
type OptionBuilder() =
member x.Bind(v,f) = Option.bind f v
member x.Return v = Some v
member x.ReturnFrom o = o
let opt = OptionBuilder()
//Clojure reducers
let reduceSeq reducingF start ss =
let mutable acc = start
@hodzanassredin
hodzanassredin / Reducer.fs
Last active August 29, 2015 14:09
reducers vs seq vs stream vs array
namespace test
module Reducer =
open System
open System.Text
open System.Collections.Generic
open System.Linq
open System.Threading.Tasks
type ReduceFunc<'T,'R> = 'R -> 'T -> 'R
@hodzanassredin
hodzanassredin / dict_keys.fs
Last active August 29, 2015 14:08
chcking perf of hash funcs
//output
//good keys
//struct custom 1.506934 sec 0.000000 collisions
//struct auto 4.832881 sec 0.776863 collisions
//struct auto explicit 3.166931 sec 0.776863 collisions
//bad keys
//struct custom 3.631251 sec 0.061893 collisions
//struct auto 10.340693 sec 0.777034 collisions
//struct auto explicit 8.893612 sec 0.777034 collisions
@hodzanassredin
hodzanassredin / bag.cs
Last active August 29, 2015 14:06
bagofwords
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace ConsoleApplication4{
class Program{
static string text = @"Functional programming is like describing your problem to a
mathematician. Imperative programming is like giving instructions to
an idiot.
-- arcus, #scheme on Freenode";
@hodzanassredin
hodzanassredin / WorkflowEngine.cs
Created July 8, 2014 07:13
WokflowEngine in c#
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Newtonsoft.Json;
using System.Reflection;
using System.IO;
using System.ComponentModel;
using System.Globalization;
@hodzanassredin
hodzanassredin / ComposableWorkflowMonad.cs
Last active August 29, 2015 14:03
Simple workflow engine in csharp
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Newtonsoft.Json;
using System.Reflection;
using System.IO;
using System.ComponentModel;
using System.Globalization;
@hodzanassredin
hodzanassredin / GeneriFn.cs
Last active August 29, 2015 14:03
simple forall representation in c#
using System;
using System.Collections.Generic;
namespace HK
{
public class ForallSample
{
// this is simplified example to show the idea
// we dont really need forall here
// but in case of two input and output values with different types we need it
@hodzanassredin
hodzanassredin / Rank.cs
Created June 26, 2014 21:25
Higher-Rank Polymorphism in c#
using System;
using System.Collections.Generic;
namespace HK
{
public class Forall{}
public interface IGeneric<T, TCONTAINER>
{
}