I prefer to use sequence comprehensions to read lines from a file:

1
2
3
4
5
6
7
8
9
10
#light
open Systemopen System.IO
let fileLines path =
    seq {
        use fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite );
        use bs = new BufferedStream(fs);
        use ts = new StreamReader(bs);
        while not ts.EndOfStream do
            yield ts.ReadLine()
    } fileLines "hello.txt"|> Seq.iter (fun line -> printfn "%s" line)

This way, a pipeline can be built up to process the file's elements:

1
2
3
fileLines "hello.txt"|> Seq.map
    (fun line -> makeObject(line))|> Seq.filter (fun o -> o.isValid)|> Seq.iter
   (fun o -> o.doSomething())
By on 10/22/2007 6:14 PM ()

Hmmm thanks for that. I think I need to use pipelines more often. I'm still writing my code backwards like, e.g.,

1
2
3
4
5
6
let patternStats = 
    List.fold_left
        (fun map (morse, word) -> Map.add morse (createInfo morse word map) map)
        Map.empty

        [for word in File.ReadAllLines(wordsPath) when target.IndexOf(convert word) <> -1 -> (convert word, word)];;

Incidentally, how do I use the nice syntax highlighting when posting code?

By on 10/25/2007 3:42 AM ()

To use code highlight you need to round the code in code tages (like xml tags but with square brackets) and give the name of the language as property you want to use i.e.:

1
let foo = "bar"

If you press reply to this message you should see the tags, as obviously they are hidden when code colouring is preformed. The code colouring works best when there are no other formatting tags so I usally copy my code to notepad and then into the reply box to stip any formatting tags visual studio and other text editors can add.

By on 10/25/2007 4:06 AM ()

To use code highlight you need to round the code in code tages (like xml tags but with square brackets) and give the name of the language as property you want to use i.e.:

1
let foo = "bar"

If you press reply to this message you should see the tags, as obviously they are hidden when code colouring is preformed. The code colouring works best when there are no other formatting tags so I usally copy my code to notepad and then into the reply box to stip any formatting tags visual studio and other text editors can add.

Ah brilliant, thank you... although I get the feeling that jhugard is using the CopyAsHTML VS plugin, because when I quote his posts, I get a preformed and preformatted area that I can't modify. Ah well.

By on 10/25/2007 5:18 AM ()

printf is actually a special case, you can only use a string as the first argument if the compiler knows what it is at compile time so that it can convert it to a Printf.textwriter_format (the actual type of the first argument to printf). If you where allowed to pass a string created at runtime then you could pass arguments with a different type to that specified in the format string which is a Bad Thing(tm). To make the example work you need to do printf "%s" (sr.ReadLine()).

In the second example when myStream.ReadLine() returns null the exception will be thrown and the application will terminate because it wasn't caught, raise is prety much the same as throw in C# and exceptions behave simlarly for the most part.

By on 10/20/2007 5:19 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper