Skip to content

Instantly share code, notes, and snippets.

View Thorium's full-sized avatar

Tuomas Hietanen Thorium

View GitHub Profile
@Thorium
Thorium / myfile.fsx
Last active August 29, 2015 14:23
One script to create MS-SQL-database and host OWIN Web-server with SignalR-clients
#if INTERACTIVE
open System
open System.IO
Console.WriteLine "Downloading components, etc. Will take a while."
// Run as admin, developer command prompt or F# Interactive. e.g. "fsi myfile.fsx"
[<Literal>]
let connectionString = "Data Source=localhost; Initial Catalog=myDatabase; Integrated Security=True;"
let serverDir = __SOURCE_DIRECTORY__ + @"/frontend"
@Thorium
Thorium / .gitignore
Last active August 29, 2015 14:24
How to use Paket also in JavaScript references: How to get Bower-components without npm and Node.js
lib/
paket-files/
[Pp]ackages/
@Thorium
Thorium / Dockerfile
Last active August 29, 2015 14:24 — forked from junosuarez/Dockerfile
Dockerfile to start F# Owin project
FROM ubuntu:14.04
RUN apt-get -y update
# Install Mono (and fsharp)
RUN apt-get -y install mono-devel autoconf pkg-config make git libtool
RUN git clone https://github.com/fsharp/fsharp
RUN cd fsharp && ./autogen.sh --prefix /usr && make && make install
# Add some files
@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"
@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 / Rijndael.fs
Created March 4, 2012 10:39
Decrypting a Rijndael string
open System.IO
open System.Security.Cryptography
open System.Text
open System.Diagnostics.Contracts
let DeCryptStringWith (crypted:string) (key:string) (iv:string) =
let enc = new ASCIIEncoding()
let algo = Rijndael.Create()
if(crypted.Length < 5) then
failwith "Crypted string length has to be over 5 chars."
@Thorium
Thorium / gist:1972331
Created March 4, 2012 10:41
Getting a key from Windows registry
let REGISTRYSOFTWARE = "Software";
let REGISTRYMYPATH = "MySoftware";
let internal GetRegistryValue key =
use path1 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(REGISTRYSOFTWARE)
match path1 with
| null -> failwith("Access failed to registry: hklm\\"+REGISTRYSOFTWARE)
| keyhklmsw ->
use path2 = keyhklmsw.OpenSubKey(REGISTRYMYPATH)
match path2 with
@Thorium
Thorium / gist:1972368
Created March 4, 2012 10:48
Silverlight asynchronous WebService call with UI-thread syncronization dispatcher
//#r "System.Runtime.Serialization"
//#r "FSharp.PowerPack"
open Microsoft.FSharp.Control.WebExtensions
open System
open System.Runtime.Serialization
open System.Runtime.Serialization.Json
open System.Net
open System.IO
@Thorium
Thorium / gist:1972225
Created March 4, 2012 10:36
Convert an object to json, and json to object
#r "System.Runtime.Serialization" // for interactive
// Reference to assembly System.Runtime.Serialization and System.Xml
open System.IO
open System.Runtime.Serialization.Json
open System.Xml
open System.Text
/// Object to Json
let internal json<'t> (myObj:'t) =
@Thorium
Thorium / gist:1972308
Created March 4, 2012 10:40
Get Stock Quote Data and Historical Stock Prices from Yahoo Finance
open System
open System.IO
open System.Xml
open System.Text
open System.Net
open System.Globalization
let makeUrl symbol (dfrom:DateTime) (dto:DateTime) =
//Uses the not-so-known chart-data:
let monthfix (d:DateTime)= (d.Month-1).ToString()