Skip to content

Instantly share code, notes, and snippets.

@bpatra
bpatra / gist:6074217
Last active Dec 20, 2015
Implementation of a "tree builder" using mutability of the data structure
View gist:6074217
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 Dec 20, 2015
Using the mutable tree and its mergeInto function introduced ingist:6074217
View gist:6074329
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 Dec 20, 2015
Labeled tree and the zipper pattern
View gist:6074448
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 Dec 20, 2015
Core algorithm for building a tree using the zippers
View gist:6074520
//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 Dec 20, 2015
Some material for abstracting the tree zipper and an example
View gist:6074560
//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 Dec 20, 2015
F#-ception, building a powershell script with inlined F# from F#
View gist:6098558
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 Dec 20, 2015
F#-ception, testing Fs2Ps with a very simple example
View gist:6098579
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 Jul 28, 2013
Powershell script in simpleExample.ps1 generated by FsToPs
View gist:6098589
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 Jul 28, 2013
F#-ception. ExecutePsScript
View gist:6098637
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 Dec 20, 2015
F#ception. test execution with factorial
View gist:6098656
[<TestClass>]
type FsCeptionTests () =
[<TestMethod>]
member this.WithFactorial () =
let myTest = "module dafuq
let rec factorial n =
match n with
| 0 -> 1
| 1 -> 1
You can’t perform that action at this time.