Advertisement
Guest User

Untitled

a guest
May 26th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. // holds list of already existing entities ('a list)
  2. // Allows to check if new entities already exist in list.
  3. // if entity already exists - add it's id into idsList
  4. // if entity doesn't exists - add to existing, add to list of notExistedEntities, add it's id into idsList
  5.  
  6. type EntityHolder(initialEntities: 'a seq, idGetter: 'a -> string, entityComparator: 'a -> 'a -> bool) =
  7.  
  8. let mutable existingEntities : 'a list = initialEntities |> Seq.toList
  9.  
  10. member this.Add (entities: 'a seq) : 'a list * string list =
  11. let notExistedEntities, ids =
  12. entities
  13. |> Seq.fold
  14. (
  15. fun (entitiesToSave: 'a list, idsList: string list) currentEntity ->
  16. match List.tryFind (entityComparator currentEntity) existingEntities with
  17. | Some alreadyExitingEntity ->
  18. let id = idGetter alreadyExitingEntity
  19. (entitiesToSave, (id :: idsList))
  20. | None ->
  21. existingEntities <- currentEntity :: existingEntities
  22. let id = idGetter currentEntity
  23. (currentEntity :: entitiesToSave, id :: idsList)
  24. )
  25. ([], [])
  26. notExistedEntities, ids
  27.  
  28. type EntityHolderMap(initialEntities: 'a seq, idGetter: 'a -> string, hashGetter: 'a -> string) =
  29.  
  30. let mutable existingEntities : Map<string, 'a> = initialEntities |> Seq.map (fun entity -> (hashGetter entity), entity) |> Map.ofSeq
  31.  
  32. member this.Add (entities: 'a seq) : 'a list * string list =
  33. let notExistedEntities, ids =
  34. entities
  35. |> Seq.fold
  36. (
  37. fun (entitiesToSave: 'a list, idsList: string list) currentEntity ->
  38. let currentHash = hashGetter currentEntity
  39. match existingEntities.TryFind currentHash with
  40. | Some alreadyExitingEntity ->
  41. let id = idGetter alreadyExitingEntity
  42. (entitiesToSave, (id :: idsList))
  43. | None ->
  44. existingEntities <- existingEntities.Add (currentHash, currentEntity)
  45. let id = idGetter currentEntity
  46. (currentEntity :: entitiesToSave, id :: idsList)
  47. )
  48. ([], [])
  49. notExistedEntities, ids
  50.  
  51. let AddEntities
  52. (idGetter: 'a -> string)
  53. (entityComparator: 'a -> 'a -> bool)
  54. (initialEntities: 'a list)
  55. (addingEntities: 'a list) : 'a list * 'a list * string list =
  56.  
  57. addingEntities
  58. |> Seq.fold
  59. (
  60. fun (existingEntities: 'a list, entitiesToSave: 'a list , idsList: string list) currentEntity ->
  61. match List.tryFind (entityComparator currentEntity) existingEntities with
  62. | Some alreadyExitingEntity ->
  63. let id = idGetter alreadyExitingEntity
  64. (existingEntities, entitiesToSave, (id :: idsList))
  65. | None ->
  66. let id = idGetter currentEntity
  67. (currentEntity :: existingEntities, currentEntity :: entitiesToSave, id :: idsList)
  68. )
  69. (initialEntities, [], [])
  70.  
  71. let AddEntitiesMap
  72. (idGetter: 'a -> string)
  73. (hashGetter: 'a -> string)
  74. (initialEntities: Map<string, 'a>)
  75. (addingEntities: 'a list) : Map<string, 'a> * 'a list * string list =
  76.  
  77. addingEntities
  78. |> Seq.fold
  79. (
  80. fun (existingEntities: Map<string, 'a>, entitiesToSave: 'a list , idsList: string list) currentEntity ->
  81. let currentHash = hashGetter currentEntity
  82. match existingEntities.TryFind currentHash with
  83. | Some alreadyExitingEntity ->
  84. let id = idGetter alreadyExitingEntity
  85. (existingEntities, entitiesToSave, (id :: idsList))
  86. | None ->
  87. let id = idGetter currentEntity
  88. (existingEntities.Add (currentHash, currentEntity), currentEntity :: entitiesToSave, id :: idsList)
  89. )
  90. (initialEntities, [], [])
  91.  
  92. // Example
  93. let AddInts = AddEntities (fun i -> i.ToString()) (fun (i: int) (j: int) -> i = j)
  94. val existingInts : int list = [1; 2; 3]
  95.  
  96. let existingInts, adding, ids = AddInts existingInts [5; 10]
  97.  
  98. // Output
  99. val ids : string list = ["10"; "5"]
  100. val existingInts : int list = [10; 5; 1; 2; 3]
  101. val adding : int list = [10; 5]
  102.  
  103. // Example
  104. let AddIntsMap = AddEntitiesMap (fun i -> i.ToString()) (fun (i: int) -> i.ToString())
  105. let existingIntsMap = Map.ofList [("1", 1); ("2", 2); ("3", 3)]
  106.  
  107. let existingIntsMap, adding, ids = AddIntsMap existingIntsMap [8; 13]
  108.  
  109. // Output
  110. val ids : string list = ["13"; "8"]
  111. val existingIntsMap : Map<string,int> = map [("1", 1); ("13", 13); ("2", 2); ("3", 3); ("8", 8)]
  112. val adding : int list = [13; 8]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement