Skip to content

Instantly share code, notes, and snippets.

View Thorium's full-sized avatar

Tuomas Hietanen Thorium

View GitHub Profile
//Using Azure Table Storage with WindowsAzure.Storage
open Microsoft.WindowsAzure.Storage
open Microsoft.WindowsAzure.Storage.Table
open Microsoft.WindowsAzure.ServiceRuntime
type Person(partitionKey, rowKey, name) =
inherit TableEntity(partitionKey, rowKey)
new(name) = Person("defaultPartition", System.Guid.NewGuid().ToString(), name)
new() = Person("")
@Thorium
Thorium / gist:9752096
Created March 25, 2014 00:00
Generate random hex-string and calculate base58encode. I made these for some initial BTC-testing, but didn't test too much... seems to work, but would need some unit-tests... :-)
module BtcTesting
open System
open System.Security.Cryptography
/// https://en.bitcoin.it/wiki/Base58Check_encoding
let base58encode (hash:byte[]) =
let code_string = ['1'..'9']@['A'..'H']@['J'..'N']@['P'..'Z']@['a'..'k']@['m'..'z'] |> List.toArray
let data = hash |> Array.toList
@Thorium
Thorium / gist:8488563
Created January 18, 2014 10:21
Set timeout or cancel Using task based async, won't block the thread.
open System
open System.Threading
/// Async timer to perform actions
let timer interval scheduledAction = async {
do! interval |> Async.Sleep
scheduledAction()
}
/// Add action to timer, return cancellation-token to cancel the action
@Thorium
Thorium / gist:8488556
Created January 18, 2014 10:19
String.notNullOrEmpty extension to strings
module String = let notNullOrEmpty = not << System.String.IsNullOrEmpty
#if INTERACTIVE
#else
module LogReader
#endif
//Log file parser for log4Net files.
open System
open System.IO
type CollectData =
@Thorium
Thorium / mystate.fs
Created November 1, 2013 12:13
Actors with control actor. No mutable state.
open System
open System.Threading
open System.IO
open Microsoft.FSharp.Control.WebExtensions
type Agent<'T> = MailboxProcessor<'T>
type SingleAgent<'T> =
| Set of 'T
| Get of AsyncReplyChannel<List<'T>>
@Thorium
Thorium / example.txt
Last active December 13, 2017 22:40
File parsing, based on multiple lines, using recursive pattern matching with many :: (cons) operator
== MyItem 1 ==
...some content...
Total: 10
Success
== MyItem 2 ==
...some content...
Total: 2
Failed
@Thorium
Thorium / gist:5181342
Created March 17, 2013 12:40
Making QR-code image having contact information (VCard) with Google Chart API. If you scan this image with mobile phone, you can directly add new person to your contacts.
open System
open System.IO
open System.Net
open System.Drawing
let ImageSize = 300
let fetchImage (url : Uri) =
let req = WebRequest.Create (url) :?> HttpWebRequest
use stream = req.GetResponse().GetResponseStream()
@Thorium
Thorium / gist:5001162
Created February 21, 2013 01:21
Agent that can upgrade its functionality on the fly. (F# MailboxProcessor containing function in the loop...)
// Erlang-style message-passing:
// Agent that can upgrade its functionality on the fly.
// Note: static typing, this agent can't upgrade its state data type (so better to use obj or custom interface)...
// Also this runs only in localhost...
// So this is more a technical demo than something useful
open System
type Methods<'state, 'x, 'reply> =
| Upgrade of
@Thorium
Thorium / gist:4989024
Created February 19, 2013 19:28
Example of state of the art C#: EntityFramework done right... :-)
// This shows how you can:
// 1) Code from top-down, give high level picture first and then go to details
// 2) Define LINQ outside EF-context and still convert it to SQL:
// - Don't hammer the database!
// 3) Have small independend classes (/parts)
// 4) Have state-less code with no global/class-variables outside entities
// 5) Easy to test:
// - Integration test with EF-DbContext.
// - LINQ logics unit tests without DbContext (with unit tests InternalsVisibleToAttribute).