Skip to content

Instantly share code, notes, and snippets.

View mastoj's full-sized avatar

Tomas Jansson mastoj

View GitHub Profile
@mastoj
mastoj / Person.cs
Last active August 29, 2015 14:21
Helps clearify things for this blog post: http://blog.bjartwolf.com/?p=4522
public class Person
{
private readonly PersonState _state;
public Person(string name)
{
_state = new PersonState(name);
}
public string GetName()
@mastoj
mastoj / money.cs
Created June 11, 2015 11:39
Simple implementation of money for C#
namespace Money
{
public class Money<TCurrency> where TCurrency : ICurrency
{
private readonly decimal _value;
public Money(decimal value)
{
_value = value;
}
@mastoj
mastoj / build.cmd
Last active August 29, 2015 14:23
FAKE question about parameters
@echo off
cls
"NuGet.exe" "Install" "FAKE" "-OutputDirectory" "packages" "-ExcludeVersion"
"packages\FAKE\tools\Fake.exe" build.fsx %*
pause
open System
[<AutoOpen>]
module FileHelper =
type FileCheck =
{
FileName: string
Size: int64
}
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)
@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
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>();
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)
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>
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