Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. let depth = 1000000
  2.  
  3. module Test =
  4. let check basis createItem =
  5. let rec createList n l =
  6. if n = 0 then l
  7. else createList (n-1) (createItem (int64 n, int64 n) l)
  8.  
  9. let a0 = createList depth basis
  10. let a1 = createList depth basis
  11.  
  12. (a0 = a1)
  13. && (a0 <= a1)
  14. && (a0 >= a1)
  15. && not (a0 < a1)
  16. && not (a0 > a1)
  17. && not (a0 <> a1)
  18.  
  19. let inline run basis createItem =
  20. let createItemType = string (createItem.GetType ())
  21. let sw = System.Diagnostics.Stopwatch.StartNew ()
  22. if not (check basis createItem) then
  23. failwith (sprintf "failed. (%A)" createItemType)
  24. printf "%d\t\t%s\n" sw.ElapsedMilliseconds createItemType
  25.  
  26. module UnionViaTuple =
  27. type XList<'T> = Nil | Cons of 'T * XList<'T>
  28. let createItem n l = (Cons (n, l))
  29.  
  30. module UnionViaRecord =
  31. type xListData<'T> = { N : 'T; L : xList<'T> }
  32. and xList<'T> = Nil | Cons of xListData<'T>
  33. let createItem n l = (Cons { N = n; L = l })
  34.  
  35. module UnionViaUnion =
  36. type xListData<'T> = Data of 'T * XList<'T>
  37. and XList<'T> = Nil | Cons of xListData<'T>
  38. let createItem n l = (Cons (Data (n, l)))
  39.  
  40. module UnionViaGeneicType =
  41. type Pair<'T,'U> = Pair of 'T * 'U
  42. type XList<'T> = Nil | Cons of Pair<'T, XList<'T>>
  43. let createItem n l = (Cons (Pair(n, l)))
  44.  
  45. module UnionViaStruct =
  46. [<Struct>]
  47. type xListData<'T>(n:'T,l:XList<'T>) =
  48. member __.N = n
  49. member __.L = l
  50. and XList<'T> = Nil | Cons of xListData<'T>
  51. let createItem n l = (Cons (xListData (n, l)))
  52.  
  53. module UnionViaGenericTypeStruct =
  54. [<Struct>]
  55. type xListData<'T, 'U>(n:'T,l:'U) =
  56. member __.N = n
  57. member __.L = l
  58. type XList<'T> = Nil | Cons of xListData<'T, XList<'T>>
  59. let createItem n l = (Cons (xListData (n, l)))
  60.  
  61. module RecordViaOption =
  62. type XList<'T> = { N : 'T; L : option<XList<'T>> }
  63. let createItem n l = Some { N = n; L = l }
  64.  
  65. Test.run UnionViaTuple.Nil UnionViaTuple.createItem
  66. Test.run UnionViaRecord.Nil UnionViaRecord.createItem
  67. Test.run UnionViaUnion.Nil UnionViaUnion.createItem
  68. Test.run UnionViaGeneicType.Nil UnionViaGeneicType.createItem
  69. #if DOESNT_WORK_ANYWHERE
  70. Test.run UnionViaStruct.Nil UnionViaStruct.createItem
  71. Test.run UnionViaGenericTypeStruct.Nil UnionViaGenericTypeStruct.createItem
  72. #endif
  73. Test.run None RecordViaOption.createItem
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement