Skip to content

Instantly share code, notes, and snippets.

View mastoj's full-sized avatar

Tomas Jansson mastoj

View GitHub Profile
/// Discriminated union representing two messages - one for sending
/// numbers to the agent and one for resetting the state of the agent
type CounterMessage =
| Update of float
| Reset
// Simple agent that calculates average and can be reset
let counter = MailboxProcessor.Start(fun agent ->
let rec loop sum count = async {
@mastoj
mastoj / fowler_fsm.fsx
Last active February 12, 2016 23:18
Simple FSM in F#. The fowler_fsm.fsx is an F# implementation of Miss Grant's controller from Fowler's DSL book: http://www.informit.com/articles/article.aspx?p=1592379&seqNum=2
module FSM =
type FSM<'TState, 'TEvent, 'TCommand when 'TEvent : comparison and 'TState : comparison> =
{
Transitions: Map<'TState, Map<'TEvent, 'TState>>
CurrentState: 'TState
InitState: 'TState
Commands: Map<'TState, 'TCommand list>
ResetEvents: 'TEvent list
CommandChannel: 'TCommand -> unit
}
@mastoj
mastoj / Error.txt
Last active January 27, 2016 15:16
Unwind sample
System.Threading.Tasks.TaskCanceledException: {"A task was canceled."}
at Neo4jClient.GraphClient.Neo4jClient.IRawGraphClient.ExecuteCypher(CypherQuery query) in D:\temp\8815004\Neo4jClient\GraphClient.cs:line 1053
at Neo4jClient.Cypher.CypherFluentQuery.ExecuteWithoutResults() in D:\temp\8815004\Neo4jClient\Cypher\CypherFluentQuery.cs:line 388
at [email protected](FSharpList`1 data) in C:\dev\git\Sfag\src\Sfag.GraphView\WriteToNeo4j.fs:line 37
at Microsoft.FSharp.Primitives.Basics.List.iter[T](FSharpFunc`2 f, FSharpList`1 x)
at Microsoft.FSharp.Collections.ListModule.Iterate[T](FSharpFunc`2 action, FSharpList`1 list)
at WriteToNeo4j.mergeNodes[T](GraphClient client, NodeMetaData nodeMeta, IEnumerable`1 nodes) in C:\dev\git\Sfag\src\Sfag.GraphView\WriteToNeo4j.fs:line 33
at [email protected](IEnumerable`1 nodes) in C:\dev\git\Sfag\src\Sfag.GraphView\WriteToNeo4j.fs:line 107
at IndexingImpl.saveState(GraphClient client, IndexingState state) in C:\de
module CachingAgent
open System
type AgentCacheMessage<'T> = AsyncReplyChannel<'T>
type Agent<'T> = MailboxProcessor<'T>
let CreateCache<'T> timespan (funcGen:Func<'T>) = Agent.Start(fun inbox ->
let rec loop expire (cached:Lazy<'T>) = async {
let! (message:AgentCacheMessage<'T>) = inbox.Receive()
let renew = expire <= DateTime.Now
let cached' = if renew then lazy (funcGen.Invoke()) else cached
Stacktrace:
at <unknown> <0xffffffff>
at (wrapper managed-to-native) System.MonoCustomAttrs.GetCustomAttributesInternal (System.Reflection.ICustomAttributeProvider,System.Type,bool) <0xffffffff>
at System.MonoCustomAttrs.GetCustomAttributesBase (System.Reflection.ICustomAttributeProvider,System.Type,bool) <0x00053>
at System.MonoCustomAttrs.GetCustomAttributes (System.Reflection.ICustomAttributeProvider,System.Type,bool) <0x0005f>
at System.MonoCustomAttrs.RetrieveAttributeUsageNoCache (System.Type) <0x00063>
at System.MonoCustomAttrs.RetrieveAttributeUsage (System.Type) <0x000ab>
at System.MonoCustomAttrs.IsDefined (System.Reflection.ICustomAttributeProvider,System.Type,bool) <0x000ff>
at System.MonoType.IsDefined (System.Type,bool) <0x0001f>
open System
open System.Drawing
open System.Windows.Forms
// Create a form to display the graphics
let width, height = 1000, 1000
let form = new Form(Width = width, Height = height)
let box = new PictureBox(BackColor = Color.White, Dock = DockStyle.Fill)
let image = new Bitmap(width, height)
public class SokEnhet : ISokEnhet<ElasticSearchKonsesjon>
{
public Guid Id { get; set; }
public EnhetNummer Enhetsnummer { get; set; }
[JsonProperty(TypeNameHandling = TypeNameHandling.Objects)]
public IEnhetNavn Navn { get; set; }
public string IndeksertNavn => Navn != null ? Navn.LagKortversjon() : "";
public EnhetType Enhetstype { get; set; }
public string KortAdresse { get; set; }
public List<ElasticSearchKonsesjon> Konsesjoner { get; set; } = new List<ElasticSearchKonsesjon>();
@mastoj
mastoj / Car.fs
Last active September 30, 2015 13:44
WhyNoFancyPrint
module Car
type Make = Make of string
type Color = Color of string
type Year = Year of int
type Car = private {Make: Make; Year: Year; Color: Color}
with
member a.PrintYear() = printfn "%A" a.Year
let print (a:Car) = printfn "%A" a // Car+Car
open System
let digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";;
let ex = "101010101011101010101011101010101011101010101011";;
let convert (chars:string) (ul) =
let baseLength = (int64 (chars.Length))
let rec convert' (acc,rest) =
if rest = 0L then acc |> List.rev
else convert' (chars.[int (rest % baseLength)]::acc,(rest/baseLength))
convert' ([],ul)
open System
[<AutoOpen>]
module FileHelper =
type FileCheck =
{
FileName: string
Size: int64
}