A possible translation is the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
let s = "this is a string very long"
let splits = new ResizeArray<int>()
splits.Add(10)
splits.Add(15)

let Split (str:string) (bounds:ResizeArray<int>) =
let ch = str.ToCharArray()
let ret = 
    ResizeArray.mapi 
        (fun idx v -> 
            let prev = if idx = 0 then 0 else bounds.[idx - 1]
            new System.String(ch, prev, v - prev)
        ) 
        bounds
    ret.Add(str.Substring(bounds.[bounds.Count - 1]))
    ret

Split s splits;;

Cheers,

Antonio

By on 6/18/2008 5:03 AM ()

Thank you very much. This is a clever solution.

I was not thinking of mapi. I was trying to come up with a map accumulator. Your solution is much easier. ;)

By on 6/19/2008 10:11 AM ()

Hello, jonh.

There is yet simpler solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
open System.IO

let reader parser fname =
  seq {
    use reader = StreamReader(File.OpenRead(fname)) in
      while not reader.EndOfStream do
        let s = reader.ReadLine() 
        yield parser s
  }

let bounds = [(0,5);(7,10);(12,18)] //fixed bounds of records in string
let parser = List.map (fun (l,r) -> s.[l..r]) bounds


reader parser "f.txt"
By on 6/24/2008 9:16 PM ()
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