This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>) = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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("")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[<TestClass>] | |
type FsCeptionTests () = | |
[<TestMethod>] | |
member this.WithFactorial () = | |
let myTest = "module dafuq | |
let rec factorial n = | |
match n with | |
| 0 -> 1 | |
| 1 -> 1 |
OlderNewer