Skip to content

Instantly share code, notes, and snippets.

View Thorium's full-sized avatar

Tuomas Hietanen Thorium

View GitHub Profile
@Thorium
Thorium / ParallelReduce.fs
Last active August 29, 2015 14:13
Idea from Guy L. Steele - Organizing Functional Code for Parallel Execution; or, foldl and foldr Considered Slightly Harmful - https://vimeo.com/6624203
// Reduce / Aggregate / Fold
// Usual way makes deep recursion and trusts tail-opt:
// (a,b,c,d,e,f,g,h) => (((((((a + b) + c) + d) + e) + f) + g) + h)
// This more is quicksort-style parallel:
// (a,b,c,d,e,f,g,h) => (((a + b) + (c + d)) + ((e + f) + (g + h)))
// No Haskell Kinds support for F# so List and Array are easiest to make as separate methods.
open System.Threading.Tasks
module List =
let reduceParallel<'a> f (ie :'a list) =
@Thorium
Thorium / gulpfile.js
Last active August 29, 2015 14:11
JavaScript minifier for FunScript
// Put this file to your FunScript output folder.
var gulp = require('gulp'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
sourcemaps = require('gulp-sourcemaps');
var jsfile = {
targetPath: '',
//This is your FunScript-generated .js-file:
sources: ['generated.js']
@Thorium
Thorium / music.fs
Last active August 29, 2015 14:11
Musical Note Frequencies: With the usual Western music it is common to use the Equal temperament: http://en.wikipedia.org/wiki/Equal_temperament http://hyperphysics.phy-astr.gsu.edu/hbase/music/et.html Then the note frequencies are calculated by this formula. For more information: http://en.wikipedia.org/wiki/Pitch_(music) http://en.wikipedia.or…
/// (Infinite) list of note-frequency -pairs
let tones =
let bass = 55.0
let octave = ["A"; "A#"; "B"; "C"; "C#"; "D"; "D#"; "E"; "F"; "F#"; "G"; "G#"]
let notes = seq { while true do yield! octave }
let frequency = bass |> Seq.unfold (fun x -> Some (x, x*System.Math.Pow(2.0, 1.0 / 12.0)))
Seq.zip notes frequency
//let ``guitar open A`` = tones |> Seq.nth 24 // val it : float * string = (220.0, "A")
@Thorium
Thorium / gist:b1dff8526d407ff43597
Last active August 29, 2015 14:11
Microsoft Kinect Body Basics with Kinect SDK 2.0 and F#
// Microsoft Kinect Body Basics with Kinect SDK 2.0 and F#
#if INTERACTIVE
#r @"..\packages\Microsoft.Kinect.2.0.1410.19000\lib\net45\Microsoft.Kinect.dll"
#else
module Kinect20
#endif
open Microsoft.Kinect
open System.Collections.Generic
let bodyFrameReader =
@Thorium
Thorium / gist:4ee6b0debdbcb9bddb7f
Last active August 29, 2015 14:11
Analyse word count from files. You can use it e.g. to create Tag Clouds
//From NuGet: Sparc.TagCloud
#if INTERACTIVE
#r @"..\packages\Sparc.TagCloud.0.0.1\lib\net40\Sparc.TagCloud.dll"
#else
module MyTagCloud
#endif
open System.IO
open Sparc.TagCloud
let analyzer = new TagCloudAnalyzer()
//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 =