Skip to content

Instantly share code, notes, and snippets.

View Thorium's full-sized avatar

Tuomas Hietanen Thorium

View GitHub Profile
@Thorium
Thorium / MemoizeWithTimeout.fs
Created May 3, 2016 12:04
This is variant of https://gist.github.com/Thorium/df46659348b0e2146cf6 that is having a time-out. You may want to use this if you e.g. cache a database query result or other mutable data source.
open System
open System.Collections.Concurrent
let cache = ConcurrentDictionary<(string * obj),Lazy<obj>>()
let cacheTimes = ConcurrentDictionary<string,DateTime>()
let cacheTimeSeconds = 30.
let memoizeConcurrent (caller:string) (f: ('a -> 'b)) = fun x ->
match cacheTimes.TryGetValue caller with
| true, time when time < DateTime.UtcNow.AddSeconds(-cacheTimeSeconds)
-> cache.TryRemove((caller, x|>box)) |> ignore
| _ -> ()
@Thorium
Thorium / gist:eef69eb53cb6cb7746d985119228a787
Created April 13, 2016 15:02
Calculate distance between two GPS latitude-longitude points.
open System
let ``calculate distance`` (p1Latitude,p1Longitude) (p2Latitude,p2Longitude) =
let r = 6371.0; // km
let dLat = (p2Latitude - p1Latitude) * Math.PI / 180.0
let dLon = (p2Longitude - p1Longitude) * Math.PI / 180.0
let lat1 = p1Latitude * Math.PI / 180.0
let lat2 = p2Latitude * Math.PI / 180.0
let a = Math.Sin(dLat/2.0) * Math.Sin(dLat/2.0) +
@Thorium
Thorium / gist:17184252d0284b78b63ef65e692d670b
Created April 13, 2016 14:57
Get local IPv4-address of the computer
open System;
open System.Net;
open System.Net.NetworkInformation;
let GetLocalIpv4Address() =
let interfaces = NetworkInterface.GetAllNetworkInterfaces()
let unicastAddresses =
interfaces |> Seq.map (fun i -> i.GetIPProperties().UnicastAddresses) |> Seq.concat |> Seq.toList
let item =
unicastAddresses |> Seq.find (fun a -> a.IPv4Mask.ToString() <> "0.0.0.0" && a.IPv4Mask.ToString() <> "255.0.0.0")
@Thorium
Thorium / phpmd5.fs
Created April 13, 2016 14:46
Calculate MD5 hash that can be used when interacting with a PHP-website. Note: MD5 has been cracked and shouldn't be used in new systems.
let phpMd5 (dataToHash:string) =
use md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
let encodedBytes = dataToHash |> System.Text.ASCIIEncoding.Default.GetBytes |> md5.ComputeHash
System.Text.RegularExpressions.Regex.Replace(System.BitConverter.ToString(encodedBytes), "-", "").ToLower()
@Thorium
Thorium / server.fs
Created April 8, 2016 18:05
Simple OWIN Self-Host web-server with static files support and request deflate/gzip compression.
// Nuget packages: Owin, Microsoft.Owin, Microsoft.Owin.Hosting, Microsoft.Owin.Host.HttpListener, Owin.Compression, Microsoft.Owin.StaticFiles, Microsoft.Owin.FileSystems
#if INTERACTIVE
#I @"./packages/Owin/lib/net40"
#I @"./packages/Owin.Compression/lib/net45"
#I @"./packages/Microsoft.Owin/lib/net45"
#I @"./packages/Microsoft.Owin.Hosting/lib/net45"
#I @"./packages/Microsoft.Owin.Host.HttpListener/lib/net45"
#I @"./packages/Microsoft.Owin.StaticFiles/lib/net45"
#I @"./packages/Microsoft.Owin.FileSystems/lib/net45"
@Thorium
Thorium / GoogleAnalytics.ts
Last active March 16, 2022 22:09
Google Analytics Typescript
/// <reference path="./../../paket-files/borisyankov/DefinitelyTyped/google.analytics/ga.d.ts" />
export var gaNewElem : any = {};
export var gaElems : any = {};
function gaInit(){
var currdate : any = new Date();
/* tslint:disable:no-string-literal */
/* tslint:disable:semicolon */
@Thorium
Thorium / logary-text-writer.fs
Last active December 19, 2015 10:42
Simple example for Logary docs
open System
open System.IO
open Logary
open Logary.Configuration
open Logary.Targets
open Logary.Metrics
#if INTERACTIVE
#else
[<EntryPoint>]
@Thorium
Thorium / lambda.fs
Last active November 27, 2015 13:44
F# lambda (quotation) to C# Expression<Func<_>> or Expression<Action<_>>
open Microsoft.FSharp.Linq.RuntimeHelpers
open System
open System.Linq.Expressions
module Lambda =
let toExpression (``f# lambda`` : Quotations.Expr<'a>) =
``f# lambda``
|> LeafExpressionConverter.QuotationToExpression
|> unbox<Expression<'a>>
@Thorium
Thorium / Memoize.fs
Last active September 30, 2015 16:42
Concurrent Memoization
module Memoize
//More generic variant of http://www.fssnip.net/c4
open System.Collections.Concurrent
let cache = ConcurrentDictionary<(string * obj) option,Lazy<obj>>()
let memoizeConcurrent (caller:string) (f: ('a -> 'b)) =
fun (x :'a) ->
(cache.GetOrAdd(Some (caller, x|>box), lazy ((f x)|>box)).Force() |> unbox) : 'b
@Thorium
Thorium / Program.fs
Created July 16, 2015 13:29
Simple CRUD -application. What you will need: 1) a Mac, Windows or Linux computer with FSharp (F#) installed. 2) MariaDB or MySQL installed. 3) Paket package manager. Run "paket.exe install" or "mono paket.exe install". 4) If on Mono, you have to add Program.fs to a project to compile it (or SignalR won't work).
// In MONO, won't work in interactive, you need to make a project and compile a dll.
#if INTERACTIVE
#r @"./packages/FSharp.Data/lib/net40/FSharp.Data.dll"
#r @"./packages/SQLProvider/lib/net40/FSharp.Data.SqlProvider.dll"
// OWIN and SignalR-packages:
#I @"./packages/Microsoft.AspNet.SignalR.Core/lib/net45"
#r @"./packages/Microsoft.AspNet.SignalR.Core/lib/net45/Microsoft.AspNet.SignalR.Core.dll"
#I @"./packages/Microsoft.Owin/lib/net45"