理解F#seqs

时间:2014-02-04 08:47:43

标签: f# f#-fake

我正在尝试使用FAKE来自动化我的项目构建。在这个过程中,我编写了以下F#代码 -

open System
open System.IO

module FileUtils =

    type BuildConfiguration = {Name:string; Directory:string}
    let DebugBuildConfiguration = {Name = "Debug"; Directory = @"\bin\Debug"}
    let ReleaseBuildConfiguration = {Name = "Release"; Directory = @"\bin\Release"}
    let StagingBuildConfiguration = {Name = "Staging"; Directory = @"\bin\Staging"}

    let codeDir = @"C:\source-control\s4sold\src"
    let sourceDirs = [| "co"; "lo"; "mo"; "po"; "pr"; "pro"; "re"; "ro" |]

    let GetAllBuildConfigurationDirs buildConfiguration =
        let allSourceDirs = sourceDirs
                                |> Seq.map (fun i -> Path.Combine(codeDir, i))
                                |> Seq.map (fun d -> Directory.GetDirectories(d))
                                |> Array.concat
        allSourceDirs |> printf "%A"
        List.ofArray allSourceDirs
            |> List.map (fun i -> Path.Combine(i, buildConfiguration.Directory))
            // |> Seq.toArray

现在我遇到的问题是,当我打印allSourceDirs时,它正确打印my / src文件夹下的目录。但是当我运行GetAllBuildConfigurationDirs时,我得到的是一个“\ bin \ Debug”数组,这意味着它不会考虑allSourceDirs的输出。

我真的很难理解这里发生了什么。我是F#新手,但知道一些Clojure。另外我如何在一系列seqs上使用Seq.map(这样我可以避免使用Array.concat)?

1 个答案:

答案 0 :(得分:4)

问题在于您组合路径的方式,您必须在开头删除\,只需写@"bin\Debug"

此外,您可以将两张地图合并为一张,因为map f |> map g相当于map (f |> g)而不是map + concat,您可以使用collect。< / p>

以下是包含这些更正的原始代码:

    open System
    open System.IO

module FileUtils =

    type BuildConfiguration = {Name:string; Directory:string}
    let DebugBuildConfiguration = {Name = "Debug"; Directory = @"bin\Debug"}
    let ReleaseBuildConfiguration = {Name = "Release"; Directory = @"bin\Release"}
    let StagingBuildConfiguration = {Name = "Staging"; Directory = @"bin\Staging"}

    let codeDir = @"C:\source-control\s4sold\src"
    let sourceDirs = [| "co"; "lo"; "mo"; "po"; "pr"; "pro"; "re"; "ro" |]

    let allSourceDirs = sourceDirs
                            |> Array.collect (fun i -> Path.Combine(codeDir, i) |> Directory.GetDirectories)
        allSourceDirs |> printf "%A"
        List.ofArray allSourceDirs
            |> List.map (fun i -> Path.Combine(i, buildConfiguration.Directory))
            // |> Seq.toArray