Skip to content

Instantly share code, notes, and snippets.

@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
@bpatra
bpatra / startfile
Created August 5, 2013 19:12
startfile for conemu
/BufferHeight 1000 powershell
@bpatra
bpatra / gist:8b3cf6a1568a9ae580c2
Last active January 28, 2016 10:58
Comments on validate issuer
TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
{
// instead of using the default validation (validating against a single issuer value, as we do in line of business apps (single tenant apps)),
// we turn off validation
//
// NOTE:
// * In a multitenant scenario you can never validate against a fixed issuer string, as every tenant will send a different one.
// * If you don’t care about validating tenants, as is the case for apps giving access to 1st party resources, you just turn off validation.
// * If you do care about validating tenants, think of the case in which your app sells access to premium content and you want to limit access only to the tenant that paid a fee,
// you still need to turn off the default validation but you do need to add logic that compares the incoming issuer to a list of tenants that paid you,
@bpatra
bpatra / gist:d10ecab36f75f46ae62913051380899e
Created December 13, 2016 13:52
Powershell functions to load VS2015 VCVARS in Powershell (you may want to add it to you powsershell profile)
function Invoke-BatchFile
{
param([string]$Path, [string]$Parameters)
$tempFile = [IO.Path]::GetTempFileName()
## Store the output of cmd.exe. We also ask cmd.exe to output
## the environment table after the batch file completes
cmd.exe /c " `"$Path`" $Parameters && set > `"$tempFile`" "