Skip to content

Instantly share code, notes, and snippets.

[<System.Runtime.CompilerServices.Extension>]
type ExtensionMethods() =
[<System.Runtime.CompilerServices.Extension>]
static member inline GetOption< ^a,'k,'v when 'a : (member TryGetValue : 'k * ('v byref) -> bool)>(this : ^a, key : 'k) =
let mutable v = Unchecked.defaultof<'v>
let scc = ( ^a : (member TryGetValue : 'k * ('v byref) -> bool) this, key, &v)
if scc then
Some v
else
None
@kevmal
kevmal / overload.fsx
Last active November 24, 2015 14:07
Return type overloading to overload F# function
type A = A with static member Instance = A
type B = B with static member Instance = B
type C = C with static member Instance = C
type D = D with static member Instance = D
type E = E with static member Instance = E
let inline chooseType< ^a when ^a : (static member Instance : ^a) > = (^a : (static member Instance : ^a) ())
type F = F with
static member inline (?<-)(F,A,a) : int = a
open System
open System.Collections.Generic
open System.IO
open CNTK
let dataFolder = @"E:\Temp\CNTK\Tests\EndToEndTests\Text\SequenceClassification\Data"
let inputDim = 2000
open System
type Temp(d,fmt,build) =
member val Build = build with get,set
member x.Dictionary : System.Collections.Generic.Dictionary<string, obj> = d
member x.Formatters : ResizeArray<obj -> string option> = fmt
member x.Content() = x.Build x
new() = Temp(System.Collections.Generic.Dictionary<string, obj>(), ResizeArray(), fun _ -> Some "")
#r @"..\packages\NETStandard.Library.NETFramework.2.0.0-preview2-25405-01\build\net461\lib\netstandard.dll"
#r @"..\packages\Google.Protobuf.3.5.1\lib\net45\Google.Protobuf.dll" //Google.Protobuf
#r @"..\packages\Microsoft.ML.0.1.0\lib\netstandard2.0\Microsoft.ML.dll" //Microsoft.ML
#r @"..\packages\Microsoft.ML.0.1.0\lib\netstandard2.0\Microsoft.ML.Api.dll" //Microsoft.ML.Api
#r @"..\packages\Microsoft.ML.0.1.0\lib\netstandard2.0\Microsoft.ML.Core.dll" //Microsoft.ML.Core
#r @"..\packages\Microsoft.ML.0.1.0\lib\netstandard2.0\Microsoft.ML.CpuMath.dll" //Microsoft.ML.CpuMath
#r @"..\packages\Microsoft.ML.0.1.0\lib\netstandard2.0\Microsoft.ML.Data.dll" //Microsoft.ML.Data
open Microsoft.FSharp.Quotations
open System
type TypeTemplate() =
static member create ([<ReflectedDefinition(false)>] f : Expr<'a -> 'b>) : Type list -> ('a -> 'b) =
let rec extractCall e =
match e with
| Patterns.Lambda(_,body) -> extractCall body
| Patterns.Call(_o,minfo,_args) -> minfo
| _ -> failwithf "Expression not of expected form %A" e
#time
let a : byte[] = Array.zeroCreate 1000000
let b : int[] = Array.zeroCreate (1000000/4)
open System.Runtime.InteropServices
open System
open System
open FSharp.Reflection
open FSharp.Quotations
open System.Reflection
// Need packages:
// System.Reactive
// Trill
open Microsoft.StreamProcessing
open System
open System.Reactive.Linq
type ContextSwitch =
let makeExpr x =
let rec func (m : Expression) ps =
match m with
| :? MethodCallExpression as x ->
let lambda = x.Arguments.[0] :?> LambdaExpression
func lambda.Body (lambda.Parameters.[0] :: ps)
| _ -> Expression.Lambda(m, ps |> List.rev)
func (FSharp.Linq.RuntimeHelpers.LeafExpressionConverter.QuotationToExpression(x)) [] :?> Expression<_>
let (|F0|) (f : Expr<unit -> 'a>) : Expression<Func<'a>> = makeExpr f