зеркало из https://github.com/dotnet/spark.git
Make F# example more idiomatic (#46)
This commit is contained in:
Родитель
a10b0ac27d
Коммит
1589d646ac
|
@ -5,23 +5,18 @@
|
|||
module Microsoft.Spark.Examples.Main
|
||||
|
||||
open System
|
||||
open System.Collections.Generic
|
||||
open System.Linq
|
||||
open System.Reflection
|
||||
open System.Runtime.InteropServices
|
||||
|
||||
let printUsage (examples : IEnumerable<string>) =
|
||||
let printUsage (examples : string seq) =
|
||||
let assemblyName = Assembly.GetExecutingAssembly().GetName().Name
|
||||
printfn "Usage: %s <example> <example args>" assemblyName
|
||||
|
||||
if examples.Any() then
|
||||
if not (Seq.isEmpty examples) then
|
||||
printfn "Examples:\n\t*%s" (examples |> String.concat "\n\t*")
|
||||
printfn "\n'%s <example>' to get the usage info of each example." assemblyName
|
||||
|
||||
let tryFindExample (examples: IEnumerable<string>, search: string, [<Out>] found : string byref) =
|
||||
found <- examples.FirstOrDefault(fun e ->
|
||||
e.Equals(search, StringComparison.InvariantCultureIgnoreCase))
|
||||
not (String.IsNullOrWhiteSpace(found))
|
||||
let tryFindExample search (examples: string seq) =
|
||||
examples |> Seq.tryFind (fun e -> e.Equals(search, StringComparison.InvariantCultureIgnoreCase))
|
||||
|
||||
[<EntryPoint>]
|
||||
let main args =
|
||||
|
@ -32,29 +27,29 @@ let main args =
|
|||
// name of the type after the rootNamespace.
|
||||
let examples =
|
||||
Assembly.GetExecutingAssembly().GetTypes()
|
||||
.Where(fun t ->
|
||||
|> Seq.filter (fun t ->
|
||||
typeof<IExample>.IsAssignableFrom(t) &&
|
||||
not t.IsInterface &&
|
||||
not t.IsAbstract &&
|
||||
t.Namespace.StartsWith(rootNamespace) &&
|
||||
((t.Namespace.Length = rootNamespace.Length) ||
|
||||
(t.Namespace.[rootNamespace.Length] = '.')))
|
||||
.Select(fun t -> t.FullName.Substring(rootNamespace.Length + 1))
|
||||
|> Seq.map (fun t -> t.FullName.Substring(rootNamespace.Length + 1))
|
||||
|
||||
match args with
|
||||
| [||] ->
|
||||
printUsage(examples)
|
||||
printUsage examples
|
||||
1
|
||||
| _ ->
|
||||
let mutable exampleName = String.Empty
|
||||
if not (tryFindExample(examples, args.[0], &exampleName)) then
|
||||
printUsage(examples)
|
||||
match examples |> tryFindExample (args |> Array.head) with
|
||||
| None ->
|
||||
printUsage examples
|
||||
1
|
||||
else
|
||||
let exampleArgs = args.Skip(1).ToArray()
|
||||
| Some exampleName ->
|
||||
let exampleArgs = args |> Array.skip 1
|
||||
let exampleType =
|
||||
Assembly.GetExecutingAssembly()
|
||||
.GetType(sprintf "%s.%s" rootNamespace exampleName)
|
||||
let instance = Activator.CreateInstance(exampleType)
|
||||
let method = exampleType.GetMethod("Run")
|
||||
method.Invoke(instance, [|exampleArgs|]) :?> int
|
||||
method.Invoke(instance, [|exampleArgs|]) :?> int
|
Загрузка…
Ссылка в новой задаче