9 people like it.

The "dir" function of Python

This snippet likes the "dir" function of Python. ( http://docs.python.org/2/library/functions.html#dir ) The "dir" function is useful on FSI. You can take a look at members of the object quickly.

Definition

 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: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
open System.Reflection

let printw width =
    let formatCode = sprintf "%%%ds" -width
    printf <| Printf.TextWriterFormat<string -> unit, unit>(formatCode)
    
let printMembers header column (ms:string list) =
    let printItems f =
        let hasValue = not << System.String.IsNullOrWhiteSpace
        if hasValue header then printfn "[%s]" header

        List.iteri f ms
        printf "\n\n"

    match ms with
    | [] -> ()
    | _ when ms.Length <= column -> printItems (fun _ -> printf "%s ")

    | otherwize ->
        let longest = ms |> List.maxBy (fun s -> s.Length)
        let width = longest.Length + 1
        printItems (fun i item ->
            if i % column = 0 && i <> 0 then printf "\n" ;
            printw width item)

let collectNames (ms:seq<#MemberInfo>) =
    ms
    |> Seq.map (fun m -> m.Name)
    |> Set.ofSeq
    |> Set.toList
    |> List.sort

let dirbase x =
    let t = x.GetType()
    t.GetMembers() |> collectNames

let dir x =
    let print header = collectNames >> printMembers header 3
    let t = x.GetType()
    t.GetMethods()    |> print "Methods"
    t.GetProperties() |> print "Properties"
    t.GetFields()     |> print "Fields"

Usage

 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: 
26: 
27: 
// > dir 1;;
// [Methods]
// CompareTo   Equals      GetHashCode 
// GetType     GetTypeCode Parse       
// ToString    TryParse    
//
// [Fields]
// MaxValue MinValue 
//
// val it : unit = ()
//
// > dir [] ;;
// [Methods]
// CompareTo         Cons              Equals            
// GetHashCode       GetType           ToString          
// get_Empty         get_Head          get_HeadOrDefault 
// get_IsCons        get_IsEmpty       get_Item          
// get_Length        get_Tag           get_Tail          
// get_TailOrNull    
//
// [Properties]
// Empty         Head          HeadOrDefault 
// IsCons        IsEmpty       Item          
// Length        Tag           Tail          
// TailOrNull    
//
// val it : unit = ()
namespace System
namespace System.Reflection
val printw : width:int -> (string -> unit)

Full name: Script.printw
val width : int
val formatCode : string
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
module Printf

from Microsoft.FSharp.Core
type TextWriterFormat<'T,'Result> = Format<'T,System.IO.TextWriter,unit,'Result>

Full name: Microsoft.FSharp.Core.PrintfModule.TextWriterFormat<_,_>
Multiple items
val string : value:'T -> string

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

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
val printMembers : header:string -> column:int -> ms:string list -> unit

Full name: Script.printMembers
val header : string
val column : int
val ms : string list
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val printItems : ((int -> string -> unit) -> unit)
val f : (int -> string -> unit)
val hasValue : (string -> bool)
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
System.String(value: nativeptr<char>) : unit
System.String(value: nativeptr<sbyte>) : unit
System.String(value: char []) : unit
System.String(c: char, count: int) : unit
System.String(value: nativeptr<char>, startIndex: int, length: int) : unit
System.String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
System.String(value: char [], startIndex: int, length: int) : unit
System.String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: System.Text.Encoding) : unit
System.String.IsNullOrWhiteSpace(value: string) : bool
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val iteri : action:(int -> 'T -> unit) -> list:'T list -> unit

Full name: Microsoft.FSharp.Collections.List.iteri
property List.Length: int
val otherwize : string list
val longest : string
val maxBy : projection:('T -> 'U) -> list:'T list -> 'T (requires comparison)

Full name: Microsoft.FSharp.Collections.List.maxBy
val s : string
property System.String.Length: int
val i : int
val item : string
val collectNames : ms:seq<#MemberInfo> -> string list

Full name: Script.collectNames
val ms : seq<#MemberInfo>
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 MemberInfo =
  member DeclaringType : Type
  member Equals : obj:obj -> bool
  member GetCustomAttributes : inherit:bool -> obj[] + 1 overload
  member GetCustomAttributesData : unit -> IList<CustomAttributeData>
  member GetHashCode : unit -> int
  member IsDefined : attributeType:Type * inherit:bool -> bool
  member MemberType : MemberTypes
  member MetadataToken : int
  member Module : Module
  member Name : string
  ...

Full name: System.Reflection.MemberInfo
module Seq

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val m : #MemberInfo
property MemberInfo.Name: string
Multiple items
module Set

from Microsoft.FSharp.Collections

--------------------
type Set<'T (requires comparison)> =
  interface IComparable
  interface IEnumerable
  interface IEnumerable<'T>
  interface ICollection<'T>
  new : elements:seq<'T> -> Set<'T>
  member Add : value:'T -> Set<'T>
  member Contains : value:'T -> bool
  override Equals : obj -> bool
  member IsProperSubsetOf : otherSet:Set<'T> -> bool
  member IsProperSupersetOf : otherSet:Set<'T> -> bool
  ...

Full name: Microsoft.FSharp.Collections.Set<_>

--------------------
new : elements:seq<'T> -> Set<'T>
val ofSeq : elements:seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Collections.Set.ofSeq
val toList : set:Set<'T> -> 'T list (requires comparison)

Full name: Microsoft.FSharp.Collections.Set.toList
val sort : list:'T list -> 'T list (requires comparison)

Full name: Microsoft.FSharp.Collections.List.sort
val dirbase : x:'a -> string list

Full name: Script.dirbase
val x : 'a
val t : System.Type
System.Object.GetType() : System.Type
System.Type.GetMembers() : MemberInfo []
System.Type.GetMembers(bindingAttr: BindingFlags) : MemberInfo []
val dir : x:'a -> unit

Full name: Script.dir
val print : (string -> seq<#MemberInfo> -> unit)
System.Type.GetMethods() : MethodInfo []
System.Type.GetMethods(bindingAttr: BindingFlags) : MethodInfo []
System.Type.GetProperties() : PropertyInfo []
System.Type.GetProperties(bindingAttr: BindingFlags) : PropertyInfo []
System.Type.GetFields() : FieldInfo []
System.Type.GetFields(bindingAttr: BindingFlags) : FieldInfo []
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/hm
Posted:11 years ago
Author:Nobuhisa
Tags: python , printf