7 people like it.

Enumerable extraction in query expressions

Adds "enumerable extraction" support to query expressions

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
[<AutoOpen>]
module EnToSeq =
    type Linq.QueryBuilder with
        member inline qb.Source(s) = 
            let e = (^t : (member GetEnumerator : unit -> ^e) s)
            Linq.QuerySource<_,System.Collections.IEnumerable>(
                seq { while (^e : (member MoveNext : unit -> bool) e) do
                        yield (^e : (member Current : ^v) e) })

[<AutoOpen>]
module EnToSeqWithItem =
    type Linq.QueryBuilder with
        member inline qb.Source(s) = 
            // constrain ^t to have an Item indexed property (which we don't actually invoke)
            let _ = fun x -> (^t : (member Item : int -> ^v with get) (x, 0))
            let e = (^t : (member GetEnumerator : unit -> ^e) s)
            Linq.QuerySource<_,System.Collections.IEnumerable>(
                seq { while (^e : (member MoveNext : unit -> bool) e) do
                        yield (^e : (member Current : obj) e) :?> ^v })

// demonstration usage
query { for m in System.Text.RegularExpressions.Regex.Matches("test", "t|e") do
        where m.Success
        select m.Value
        distinct }
Multiple items
type AutoOpenAttribute =
  inherit Attribute
  new : unit -> AutoOpenAttribute
  new : path:string -> AutoOpenAttribute
  member Path : string

Full name: Microsoft.FSharp.Core.AutoOpenAttribute

--------------------
new : unit -> AutoOpenAttribute
new : path:string -> AutoOpenAttribute
namespace Microsoft.FSharp.Linq
Multiple items
type QueryBuilder =
  new : unit -> QueryBuilder
  member All : source:QuerySource<'T,'Q> * predicate:('T -> bool) -> bool
  member AverageBy : source:QuerySource<'T,'Q> * projection:('T -> 'Value) -> 'Value (requires member ( + ) and member DivideByInt and member get_Zero)
  member AverageByNullable : source:QuerySource<'T,'Q> * projection:('T -> Nullable<'Value>) -> Nullable<'Value> (requires member ( + ) and member DivideByInt and member get_Zero and default constructor and value type and 'Value :> ValueType)
  member Contains : source:QuerySource<'T,'Q> * key:'T -> bool
  member Count : source:QuerySource<'T,'Q> -> int
  member Distinct : source:QuerySource<'T,'Q> -> QuerySource<'T,'Q> (requires equality)
  member ExactlyOne : source:QuerySource<'T,'Q> -> 'T
  member ExactlyOneOrDefault : source:QuerySource<'T,'Q> -> 'T
  member Exists : source:QuerySource<'T,'Q> * predicate:('T -> bool) -> bool
  ...

Full name: Microsoft.FSharp.Linq.QueryBuilder

--------------------
new : unit -> Linq.QueryBuilder
val qb : Linq.QueryBuilder
member Linq.QueryBuilder.Source : s:'t -> Linq.QuerySource<'v,System.Collections.IEnumerable> (requires member GetEnumerator and member MoveNext and member get_Current)

Full name: Script.EnToSeq.Source
val s : 't (requires member GetEnumerator and member MoveNext and member get_Current)
val e : 'e (requires member MoveNext and member get_Current)
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
Multiple items
type QuerySource<'T,'Q> =
  new : seq<'T> -> QuerySource<'T,'Q>
  member Source : seq<'T>

Full name: Microsoft.FSharp.Linq.QuerySource<_,_>

--------------------
new : seq<'T> -> Linq.QuerySource<'T,'Q>
namespace System
namespace System.Collections
type IEnumerable =
  member GetEnumerator : unit -> IEnumerator

Full name: System.Collections.IEnumerable
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
type bool = System.Boolean

Full name: Microsoft.FSharp.Core.bool
member Linq.QueryBuilder.Source : s:'t -> Linq.QuerySource<'v,System.Collections.IEnumerable> (requires member get_Item and member GetEnumerator and member MoveNext and member get_Current)

Full name: Script.EnToSeqWithItem.Source
val s : 't (requires member get_Item and member GetEnumerator and member MoveNext and member get_Current)
val x : 't (requires member get_Item and member GetEnumerator and member MoveNext and member get_Current)
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
type obj = System.Object

Full name: Microsoft.FSharp.Core.obj
val query : Linq.QueryBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.query
val m : System.Text.RegularExpressions.Match
namespace System.Text
namespace System.Text.RegularExpressions
Multiple items
type Regex =
  new : pattern:string -> Regex + 1 overload
  member GetGroupNames : unit -> string[]
  member GetGroupNumbers : unit -> int[]
  member GroupNameFromNumber : i:int -> string
  member GroupNumberFromName : name:string -> int
  member IsMatch : input:string -> bool + 1 overload
  member Match : input:string -> Match + 2 overloads
  member Matches : input:string -> MatchCollection + 1 overload
  member Options : RegexOptions
  member Replace : input:string * replacement:string -> string + 5 overloads
  ...

Full name: System.Text.RegularExpressions.Regex

--------------------
System.Text.RegularExpressions.Regex(pattern: string) : unit
System.Text.RegularExpressions.Regex(pattern: string, options: System.Text.RegularExpressions.RegexOptions) : unit
System.Text.RegularExpressions.Regex.Matches(input: string, pattern: string) : System.Text.RegularExpressions.MatchCollection
System.Text.RegularExpressions.Regex.Matches(input: string, pattern: string, options: System.Text.RegularExpressions.RegexOptions) : System.Text.RegularExpressions.MatchCollection
custom operation: where (bool)

Calls Linq.QueryBuilder.Where
property System.Text.RegularExpressions.Group.Success: bool
custom operation: select ('Result)

Calls Linq.QueryBuilder.Select
property System.Text.RegularExpressions.Capture.Value: string
custom operation: distinct

Calls Linq.QueryBuilder.Distinct
Raw view Test code New version

More information

Link:http://fssnip.net/ep
Posted:12 years ago
Author:kvb
Tags: query , sequence