Skip to content

Instantly share code, notes, and snippets.

@bpatra
bpatra / gist:6074217
Last active December 20, 2015 04:49
Implementation of a "tree builder" using mutability of the data structure
type Tree =
|Node of string*list<Tree ref>
|Empty
//create tree from a branch
let rec branchToTree (inputList:list<string>) =
match inputList with
| [] -> Tree.Empty
| head::tail -> Tree.Node (head, [ref (branchToTree tail)])
@bpatra
bpatra / gist:6074329
Last active December 20, 2015 04:58
Using the mutable tree and its mergeInto function introduced ingist:6074217
let tree = ref Tree.Empty
let branch1 = ["a";"b";"c"]
let branch2 = ["a";"b";"d"]
let branch3 = ["a";"f"]
do mergeInto tree branch1
do mergeInto tree branch2
do mergeInto tree branch3
//the following tree is created
// a
@bpatra
bpatra / gist:6074448
Last active December 20, 2015 04:59
Labeled tree and the zipper pattern
type Tree =
| TreeNode of string* Tree list
| Empty
member this.GetLabel =
match this with
| Empty -> failwith "cannot get the label of an empty tree"
| TreeNode(lbl,_) -> lbl
//transform the branch into a tree
let rec branchToTree (inputList:list<string>) =
@bpatra
bpatra / gist:6074520
Last active December 20, 2015 04:59
Core algorithm for building a tree using the zippers
//core algorithm that returns a new zipper with the branch appended
let rec appendToTreeZipper (Loc (t,p) as l) (branch:list<string>) =
let rightSiblings = match p with
| Top(_) -> []
| PathNode(_,_,_,rights) -> rights
let label = match t with
| Empty -> None
| TreeNode(lbl,_) -> Some(lbl)
@bpatra
bpatra / gist:6074560
Last active December 20, 2015 04:59
Some material for abstracting the tree zipper and an example
//Get the root tree from a zipper
let rec root (Loc (t, p) as l) =
match p with
| Top(_) -> t
| _ -> root (go_up l)
//creates zipper from a tree
let getZipper t =
match t with
| Empty -> Loc(Empty, Top(""))
@bpatra
bpatra / gist:6098558
Last active December 20, 2015 08:09
F#-ception, building a powershell script with inlined F# from F#
let PSUsableAssemblies =
AppDomain.CurrentDomain.GetAssemblies()
|> Array.filter (fun asm -> match asm.IsDynamic with
| true -> false
| false -> File.Exists asm.Location)
let FsToPs (codeDOMProviderPath:string) (fsCode:string) moduleName returnedVariableName =
let sb = new StringBuilder()
sb.AppendFormat("Add-Type -Path {0}",codeDOMProviderPath) |> ignore
sb.AppendLine("") |> ignore
@bpatra
bpatra / gist:6098579
Last active December 20, 2015 08:09
F#-ception, testing Fs2Ps with a very simple example
let myTest = "module dafuq
let x= 5
"
//disk path to FSharp code dom provider
let codDomPath = @"<myPath>\FSharp.Compiler.CodeDom.dll"
let psCode = FsToPs codDomPath myTest "dafuq" "x"
//write script to file to test it.
use streamWriter = File.CreateText("simpleExample.ps1")
streamWriter.Write(psCode)
@bpatra
bpatra / gist:6098589
Created July 28, 2013 13:31
Powershell script in simpleExample.ps1 generated by FsToPs
Add-Type -Path <myPath>\FSharp.Compiler.CodeDom.dll
$provider = New-Object Microsoft.FSharp.Compiler.CodeDom.FSharpCodeProvider
$fsharpCode = @"
module dafuq
let x= 5
"@
$fsharpType = Add-Type -TypeDefinition $fsharpCode -CodeDomProvider $provider
[dafuq]::x
@bpatra
bpatra / gist:6098637
Created July 28, 2013 13:48
F#-ception. ExecutePsScript
let ExecPsScript<'T> script =
let runspaceConfiguration = RunspaceConfiguration.Create()
let runspaceAssemblies = runspaceConfiguration.Assemblies
let assembliesConfiguration =
PSUsableAssemblies
|> Array.map (fun asm -> new AssemblyConfigurationEntry(asm.FullName, asm.Location))
assembliesConfiguration |> runspaceAssemblies.Append
assembliesConfiguration
|> Array.map (fun asm -> asm.FileName)
|> Array.map (fun path -> new TypeConfigurationEntry(path))
@bpatra
bpatra / gist:6098656
Last active December 20, 2015 08:09
F#ception. test execution with factorial
[<TestClass>]
type FsCeptionTests () =
[<TestMethod>]
member this.WithFactorial () =
let myTest = "module dafuq
let rec factorial n =
match n with
| 0 -> 1
| 1 -> 1