开发者

Piping a list into the line in F#

开发者 https://www.devze.com 2022-12-21 09:24 出处:网络
Now I can add single value or tubles to the pipeline, my next question is can I add a list/array: filesUnderFolder

Now I can add single value or tubles to the pipeline, my next question is can I add a list/array:

filesUnderFolder
|> Seq.map FileInfo  

my problem is I can't process this with pipeline |开发者_开发问答>, or can I?

Assembly.GetExecutingAssembly.GetFiles()
|>Array.map (fun file -> file.ReadAllText().Contains(keyword))


I do not understand what is your question about, but correct version is:

open System.Reflection
open System.IO

Assembly.GetExecutingAssembly().GetFiles() 
|> Seq.map (fun file -> new StreamReader(file)) 
|> Seq.map (fun file -> file.ReadToEnd().Contains(keyword)) 
|> Seq.iter(printfn "%s")

First you need GetExecutingAssembly and you need its result. So (). Second GetFiles(), return array of Stream, not FileInfo as you may expect. Because of this you have to wrap Stream to StreamWriter.

Thats it.


Looking at your comment are you trying to do this?

Assembly.GetExecutingAssembly().GetFiles() 
|> Seq.map (fun file -> 
    let stream = new StreamReader(file)
    file, stream.ReadToEnd().Contains(keyword))
|> Seq.filter snd
|> Seq.map fst
|> Seq.iter (fun file -> printfn "%s" file.Name)

Imperative style can be cleaner.

for file in Assembly.GetExecutingAssembly().GetFiles() do
    let stream = new StreamReader(file)
    if stream.ReadToEnd().Contains(keyword) then
        printfn "%s" file.Name
0

精彩评论

暂无评论...
验证码 换一张
取 消