Skip to content

Instantly share code, notes, and snippets.

View jindraivanek's full-sized avatar

Jindřich Ivánek jindraivanek

View GitHub Profile
let lockObj = obj()
let printLock s = lock lockObj (fun () -> printfn "%s" s)
let memoizeAsyncAsTask f =
let cache = System.Collections.Concurrent.ConcurrentDictionary()
let f' x = Async.StartAsTask (f x)
fun x -> async { return cache.GetOrAdd(x, lazy f' x).Value.Result }
[<Struct>]
type MemoizeAsyncState<'a> = | Running of sem:System.Threading.SemaphoreSlim | Completed of 'a
@jindraivanek
jindraivanek / patternParser.fsx
Created April 2, 2024 14:39
Rec Active Pattern Parser
let (|TakeUntil|_|) x xs =
match List.takeWhile ((<>) x) xs with
| y when y = xs -> None
| y -> Some(y, List.skipWhile ((<>) x) xs)
let (|Split|_|) split xs =
match xs with
| TakeUntil split (x1, (_ :: x2)) -> Some(x1, x2)
| _ -> None
@jindraivanek
jindraivanek / AsyncAndBang.fsx
Created August 15, 2023 11:12
Async CE with and! parallelism
type Parallel() =
member this.Bind (a, f) = async.Bind
member this.Bind2 (a, b, f) = async {
let! [|x; y|] = Async.Parallel [a; b]
return! f (x, y)
}
member this.Bind3 (a, b, c, f) = async {
let! [|x; y; z|] = Async.Parallel [a; b; c]
return! f (x, y, z)
}
module FsCheckExample
open System.IO
open NUnit.Framework
open FsCheck.NUnit
open FsCheck.PropOperators
open FsCheck.GenBuilder
open FsCheck
[<Property(Verbose = true, Replay = "(1593021609, 297184309)")>]
module Bench
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Running
let containsByExists x xs = List.exists ((=) x) xs
let inline containsByExistsInlined x xs = List.exists ((=) x) xs
[<MemoryDiagnoser>]
type ListTests() =
#r "nuget: Flips"
type Cut = Cut of float
type Plan = Plan of Map<Cut, int>
module Cut =
/// Take a Cut and return the length as a float
let length (Cut length) =
length
@jindraivanek
jindraivanek / exodus.cs
Last active March 27, 2019 17:22
Sample of cs2fs output
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Example
{
@jindraivanek
jindraivanek / File_ordering_alg.md
Last active January 10, 2018 08:40
Mechanic: file ordering alg

Ordering of files alg. is splitted into 3 parts:

  1. Collecting symbols from AST
  2. From symbols, find dependencies between files
  3. Construct oriented graph from dependencies and solve topological order on it

Collecting symbols from AST

For each file, from untyped tree we collect three types of symbols:

  • symbol definition, fully qualified (let bindings, type declaration, DU case constructors, record fields, members)
#load ".paket/load/main.group.fsx"
//used snippets from https://fsharpforfunandprofit.com/posts/understanding-parser-combinators-4/
open FParsec
type NewLineStyle = | NLnever | NLwrap | NLalways
type FmtOptions = { ColumnWidth : int option; Spaces : bool; IndentSize : int; ObjectNewLine : NewLineStyle; ArrayNewLine : NewLineStyle }
#load ".paket/load/main.group.fsx"
open FParsec
type Json = JString of string
| JNumber of float
| JBool of bool
| JNull
| JList of Json list