Skip to content

Instantly share code, notes, and snippets.

@palladin
palladin / gist:def871a13f9d26c6c388
Created July 7, 2014 20:20
Clojure meetup final
;; Anything you type in here will be executed
;; immediately with the results shown on the
;; right.
(def data '({:name "nick" :age 20} {:name "george", :age 30}
{:name "giannis", :age 40} {:name "dimitris", :age 50}))
@palladin
palladin / gist:c5fca07b74da782832cb
Last active September 25, 2021 17:52
Parallel Streams Test
#time
#r "bin/Release/Streams.Core.dll"
open Nessos.Streams.Core
let data = [|1..10000000|] |> Array.map int64
#r "../../packages/FSharp.Collections.ParallelSeq.1.0/lib/net40/FSharp.Collections.ParallelSeq.dll"
@palladin
palladin / gist:c34a3a761f755b57244c
Created July 27, 2014 15:05
ParStream.sortBy performance test
#time
#r "bin/Release/Streams.Core.dll"
open Nessos.Streams.Core
let rnd = new System.Random()
let data = [|1..10000000|] |> Array.map (fun _ -> int64 <| rnd.Next(1000000))
#r "../../packages/FSharp.Collections.ParallelSeq.1.0/lib/net40/FSharp.Collections.ParallelSeq.dll"
open FSharp.Collections.ParallelSeq
@palladin
palladin / gist:66d93ff825402f1af15e
Created August 5, 2014 15:51
ParStream.groupBy performance test
#time
#r "bin/Release/Streams.Core.dll"
open Nessos.Streams.Core
let data = [|1..10000000|] |> Array.map (fun i -> int64 <| (i % 1000000))
#r "../../packages/FSharp.Collections.ParallelSeq.1.0/lib/net40/FSharp.Collections.ParallelSeq.dll"
open FSharp.Collections.ParallelSeq
type Grouping<'T> = { IndexRef : int ref; ArrayRef : 'T [] ref }
let rec groupingAdd (value : 'T) (grouping : Grouping<'T>) =
let indexRef = grouping.IndexRef
let arrayRef = grouping.ArrayRef
let array = !arrayRef
if Object.ReferenceEquals(array, null) then
groupingAdd value grouping
else
let index = Interlocked.Increment(indexRef)
@palladin
palladin / gist:aac92fe83aba3027969c
Last active September 25, 2021 17:51
Func<...> vs FSharpFunc<...> (2-arg functions)
#time
let sum (f : 'T -> int -> int) (array : 'T[]) =
let mutable sum = 0
for i = 0 to array.Length - 1 do
sum <- sum + f array.[i] i
sum
@palladin
palladin / gist:6fac782a0ba4aa6b88b5
Last active September 25, 2021 17:51
Push/Pull Streams
type Stream<'T> = ('T -> unit) -> unit -> bool
let inline ofArray (source : 'T []) : Stream<'T> =
fun iterf ->
let i = ref 0
fun () ->
let flag = !i < source.Length
if not flag then
false
@palladin
palladin / gist:bc278fc010e4d244ef7a
Last active September 25, 2021 17:52
Experiment
#time "on"
let inline readAllHashes nSkip nSentences =
sentencesFile
|> File.ReadLines
|> Stream.ofSeq
|> Stream.skip nSkip
|> Stream.take nSentences
|> Stream.map(fun line ->
let separatorIndex = line.IndexOf(' ')
@palladin
palladin / gist:671b514df45fed287e5f
Created January 24, 2015 15:21
A type-level SAT solver in F#
type Bool = interface end
and True = True with
interface Bool
and False = False with
interface Bool
type And = And with
static member inline (?<-) (True, And, True) = True
static member inline (?<-) (True, And, False) = False
#include <iostream>
using namespace std;
int foo()
{
auto flatMap = [](auto f, auto s) { return [=](auto k) { return s([=](auto x) { f(x)(k); }); }; };
auto map = [](auto f, auto s) { return [=](auto k) { s([=](auto x) { k(f(x)); }); }; };
auto filter = [](auto p, auto s) { return [=](auto k) { s([=](auto x) { if(p(x)) k(x); }); }; };
auto sum = [](auto k) { int sum = 0; k([&](int x) { sum += x; }); return sum; };