Your code seems a little too complicated for me, for what you're trying to do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
/// reader that directly generates a sequence of floats and strings
let reader filename =
  seq 
    { use reader = new StreamReader(File.OpenRead(filename))
      while not reader.EndOfStream do
        let next = reader.nextByte() 
        if numeric next then
          let num = read_till_^or~ reader
          yield (float_of_string num)


        elif next = '^' or next = '~' then
          // skip
        else
          let string = read_till_^or~ reader
          yeld string
    }

Note: I did not define the functions numeric, or read_till_^or~, but I hope you get the point ;)
Also I'm sure there's a couple of typing errors in there... (but you can easily return all strings or box the values)

By on 7/9/2008 7:13 AM ()

You could use a regular expression like in the sample below. Whether that is easier to understand than a simple imperative nested loop is debatable...

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
#light

open System
open System.IO
open System.Text.RegularExpressions

type Value = VString of string
           | VFloat of float

let regex = new Regex("^(?:(?:\^|(?<=^))([^^\r\n]*))*$", RegexOptions.Multiline)

let reader filename =
    let isBeginOfFloat c = (c >= '0' && c <= '9') || c = '.' || c = '-'
    
    use reader = new StreamReader(File.OpenRead(filename))
    let str = reader.ReadToEnd()    
    let matches = regex.Matches(str)
            
    [| for m in matches do
        -> [| for c in m.Groups.[1].Captures do
                  -> let i, n = c.Index, c.Length
                     if n = 0 then VFloat 0.0
                     else
                         let i, n = if str.[ i] = '~' then i + 1, n - 2 else i, n
                         if n = 0 then VFloat 0.0
                         elif isBeginOfFloat str.[ i] then 
                             VFloat (Float.of_string (str.Substring(i, n)))
                         else
                             VString (str.Substring(i, n)) 
           |]
    |]
By on 7/9/2008 7:54 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