Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.64 KB | None | 0 0
  1. // Code from Hansen and Rischel: Functional Programming using F#     16/12 2012
  2. // Chapter 10: Asynchronous and parallel computations.
  3. // Appendix B: The TextProcessing module: implementation file
  4.  
  5. module TextProcessing
  6.  
  7. open System.Text.RegularExpressions ;;
  8.  
  9. let captureSingle (ma:Match) (n:int) =
  10.    ma.Groups.[n].Captures.[0].Value ;;
  11.  
  12. let captureList (ma:Match) (n:int) =
  13.   let capt = ma.Groups.[n].Captures
  14.   let m = capt.Count - 1
  15.   [for i in 0..m -> capt.[i].Value] ;;
  16.  
  17. let captureCount (ma:Match) (n:int) =
  18.   ma.Groups.[n].Captures.Count ;;
  19.  
  20. let captureCountList (ma:Match) =
  21.   let m = ma.Groups.Count - 1
  22.   [for n in 0..m -> ma.Groups.[n].Captures.Count] ;;
  23.  
  24. open System ;;
  25. open System.IO ;;
  26.  
  27. let fileXfold f e0 path =
  28.   use s = File.OpenText path
  29.   let rec fld e =  
  30.     if s.EndOfStream then e
  31.     else fld (f e s)
  32.   let res = fld e0
  33.   s.Close()
  34.   res ;;
  35.  
  36. let fileXiter g path =
  37.   use s = File.OpenText path
  38.   while not(s.EndOfStream)
  39.     do g s
  40.   s.Close() ;;
  41.  
  42. let fileFold f e s =
  43.   fileXfold (fun e s -> f e (s.ReadLine())) e s ;;
  44.  
  45. let fileIter g s =
  46.   fileXiter (fun s -> g (s.ReadLine())) s ;;
  47.  
  48. open System.IO ;;
  49. open System.Runtime.Serialization.Formatters.Binary ;;  
  50.  
  51. let saveValue v path =
  52.   let fsOut = new FileStream(path,FileMode.Create)
  53.   let formatter = new BinaryFormatter()
  54.   formatter.Serialize(fsOut,box v)
  55.   fsOut.Close() ;;
  56.  
  57. let restoreValue path =  
  58.   let fsIn = new FileStream(path,FileMode.Open)
  59.   let formatter = new BinaryFormatter()
  60.   let res = formatter.Deserialize(fsIn)
  61.   fsIn.Close()
  62.   unbox res ;;
  63.  
  64. open System.Globalization ;;
  65. open System ;;
  66.  
  67. exception StringOrderingMismatch ;;
  68.  
  69. [<CustomEquality;CustomComparison>]
  70. type orderString =
  71.   {Str: string; Cult: string; Cmp: string->string->int}
  72.   override s.ToString() = s.Str
  73.   interface System.IComparable with
  74.     member s1.CompareTo sobj =
  75.       match sobj with
  76.       | :? orderString as s2 ->
  77.         if s1.Cult <> s2.Cult then raise StringOrderingMismatch
  78.         else
  79.         match s1.Cmp s1.Str s2.Str with
  80.         | 0 -> compare s1.Str s2.Str
  81.         | z -> z
  82.       | _ ->
  83.         invalidArg "sobj"
  84.                    "cannot compare values with different types"
  85.   override s1.Equals sobj =
  86.     match sobj with
  87.     | :? orderString as s2 -> s1 =  s2
  88.     | _                    -> false
  89.   override s.GetHashCode() = hash(s.Str) ;;
  90.  
  91. let orderString (cult: string) =
  92.   let culInfo = CultureInfo cult
  93.   let comp s1 s2 =
  94.     String.Compare(s1,s2,culInfo,CompareOptions.None)
  95.   fun s -> {Str = s; Cult = cult; Cmp = comp}: orderString ;;
  96.  
  97. let orderCulture s = s.Cult ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement