Skip to content

Instantly share code, notes, and snippets.

Tuomas Hietanen Thorium

Block or report user

Report or block Thorium

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@Thorium
Thorium / BlockChain.fs
Last active Apr 25, 2018
Using NBitcoin to create private BlockChain with F# (FSharp)
View BlockChain.fs
// This is just an initial example / tech-demo.
#if INTERACTIVE
#I "./../packages/NBitcoin/lib/net45/"
#I "./../packages/Newtonsoft.Json/lib/net45"
#r "NBitcoin.dll"
#r "Newtonsoft.Json.dll"
#else
module BlockChain
#endif
@Thorium
Thorium / btc.fs
Last active Feb 2, 2017
BTC transfers with NBitcoin with FSharp
View btc.fs
//Add reference to NBitcoin
#if INTERACTIVE
#I "./../packages/NBitcoin.3.0.1.6/lib/net45/"
#I "./../packages/Newtonsoft.Json.9.0.1/lib/net45"
#r "NBitcoin.dll"
#r "Newtonsoft.Json.dll"
#endif
open System
open NBitcoin
@Thorium
Thorium / graphgen.fs
Created Dec 9, 2016
How you can programmatically use GraphViz from F#
View graphgen.fs
// 1. Install: http://www.graphviz.org/Download..php
//
// 2. Add <appSettings> under <configuration> to app.config/web.config
// Add Nuget / Paket: GraphViz.NET
//
// 3. Add under <configuration><appSettings>:
// <add key="graphVizLocation" value="C:\Program Files (x86)\Graphviz2.38\bin" />
//
// Add references to System.Configuration.dll and System.Drawing.dll
@Thorium
Thorium / rss.fs
Last active Sep 10, 2016
Generate RSS feed from a list of F# record items using Linq2Xml. To use e.g. to generate feeds from your web server.
View rss.fs
#if INTERACTIVE
#r "System.Xml.Linq.dll"
#else
module Rss
#endif
open System
open System.Xml.Linq
type NewsItem = {
@Thorium
Thorium / ecb.fs
Created Jun 7, 2016
Fetch EUR based currency rates of the day and parse currency conversion rates from European Central Bank
View ecb.fs
#if INTERACTIVE
#r "System.Xml.dll"
#r "System.Xml.Linq.dll"
#endif
open System
open System.Net
open System.IO
open System.Xml.Linq
@Thorium
Thorium / Rijndael.fsx
Created May 25, 2016
Encrypting a Rijndael string, counter part for https://gist.github.com/Thorium/1972253
View Rijndael.fsx
open System
open System.IO
open System.Security.Cryptography
open System.Text
open System.Diagnostics.Contracts
let EncryptStringWith (plain:string) (key:string) (iv:string) =
let enc = new ASCIIEncoding()
use encrypted = new MemoryStream()
use encode = new ToBase64Transform()
@Thorium
Thorium / post.fs
Created May 20, 2016
Send async HTTP POST request
View post.fs
open System.Net
open System.IO
let makePostRequest (url : string) (requestBody : string) =
let req = WebRequest.CreateHttp url
req.CookieContainer <- new CookieContainer()
req.Method <- "POST"
req.ProtocolVersion <- HttpVersion.Version10
let postBytes = requestBody |> System.Text.Encoding.ASCII.GetBytes
req.ContentLength <- postBytes.LongLength
@Thorium
Thorium / transactions.fs
Created May 20, 2016
Way to wrap methods to transactions
View transactions.fs
// 1) Create TransactionScope, on Mono and in .NET, and the later one supports TransactionScopeAsyncFlowOption.
// 2) Then, complete the transaction automatically if no exceptions has been thrown.
// If Async or Task, then automatically await the result before complete without blocking the thread.
#if INTERACTIVE
#r "System.Transactions.dll"
#endif
open System
open System.Threading
open System.Threading.Tasks
@Thorium
Thorium / MemoizeWithTimeout.fs
Created May 3, 2016
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.
View MemoizeWithTimeout.fs
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 Apr 13, 2016
Calculate distance between two GPS latitude-longitude points.
View gist:eef69eb53cb6cb7746d985119228a787
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) +
You can’t perform that action at this time.