Guest User

Untitled

a guest
Oct 23rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.39 KB | None | 0 0
  1. /**
  2. Represent pure container. Allow work only with existing keys and only if you know keys.
  3. Real life examples: any shared storages
  4. */
  5. fix Pure = class(Key: Fn, Value: Fn)
  6. {
  7. /** class of Key */
  8. fix Key = Key
  9.  
  10. /** class of Value */
  11. fix Value = Value
  12.  
  13. /** -> value by {k} or null if {k} is not in container */
  14. method fix _get += #(k: Key) -> ?Value { abstract }
  15.  
  16. /** allow check {k} in container or not */
  17. method fix _has += #(k: Key) -> Bool { -> @_get(k) != null }
  18.  
  19. /**
  20. set {v} by existing {k}.
  21. If {k} doesn't exist - @throws {RangeError}.
  22. -> is container changed or not
  23. */
  24. method _overwrite += #(k: Key, v: Value) -> Bool { abstract }
  25.  
  26. /** @wrapper of {@_get} */
  27. operator fix 'get [x]' += #(k: Key) -> ?Value { -> @_get(k) }
  28.  
  29. /** @wrapper of {@_overwrite} */
  30. operator 'set [x]' += #(k: Key, v: Value) { @_overwrite(k, v) }
  31. }
  32.  
  33. /**
  34. add {@_each} functionality which allow user to know all keys of container
  35. that + stadard (@_copy) allows great ability to implemet {@_map}, {@_reduce} etc but you still can not implemet {@_filter} which requires {@_del} or other way
  36. */
  37. fix Enumerable = class(Key: Fn, Value: Fn) extends Pure(Key, Value)
  38. {
  39. /**
  40. breakable iterate through all container. breaks when user's {_fn} -> not null. -> return of {_fn}
  41. @match checks {_fn} semantic {_fn(value, key, container)}
  42. */
  43. method fix _each += #(_fn: Fn), match { -> _fn._lazyMatch([@Key, @Value, Enumerable]) } { abstract }
  44.  
  45. /** checks if container is empty or not */
  46. method fix _isEmpty += #() -> Bool { -> !(@_each(#{ -> yes }) || no) }
  47.  
  48. /**
  49. stadard functional map, but w/o 2nd {dest} arg
  50. -> transformed copy {@} applying {_fn} to each value of {@}
  51. */
  52. /*
  53. method fix _map += #(_fn: Fn, out: Fillable), match { -> _fn._lazyMatch([@Key, @Value, Enumerable]) }
  54. {
  55. a._each(#@(v, k){ a._overwrite(k, _fn._lazyCall(v, k, @)) })
  56. -> a
  57. }
  58. */
  59.  
  60. /**
  61. compact container to one value applying {r = _fn(r, v, i, c)} recursivaly
  62. if initial {r} is null => r = 'first' value
  63. */
  64. method fix _reduce += #(_fn: Fn, r: ?Auto), match { -> _fn._lazyMatchArgs([r.Class, @Key, @Value, Enumerable]) }
  65. {
  66. if(r == null)
  67. {
  68. var isFirst = yes
  69. @_each(#@(v, k){
  70. if(isFirst)
  71. {
  72. r = v
  73. isFirst = no
  74. }
  75. else
  76. {
  77. r = _fn._lazyCall(r, v, k, @)
  78. }
  79. })
  80. }
  81. else
  82. {
  83. @_each(#@(v, k){ r = _fn._lazyCall(r, v, k, @) })
  84. }
  85. -> r
  86. }
  87. }
  88.  
  89. /**
  90. Represents fillable class which allows you abstract fill container.
  91. Requires key save semantic of {@_add} method
  92. Useful for generic {_grep}, may be {_sort} etc
  93. */
  94. fix Fillable = class(Key: Fn, Value: Fn)
  95. {
  96. /**
  97. add new {v} to container.
  98. -> key of added {v}
  99. */
  100. method _add += #(v: Value) -> Key { abstract }
  101.  
  102. /**
  103. filter {@}, testing {_filter(v, k, @)} and if yes - add to {dest} container
  104. if {@} == {dest} @throw {UnsupportedError} because this feature requires {@_del}
  105. */
  106. /*
  107. method fix _grep += #(_fn: Fn, dest: ?&Fillable = @Class()){
  108. if(@ == dest)
  109. throw UnsupportedError()
  110. src._each(#@(v, k){
  111. if(_fn._lazyCall(v, k, @))
  112. dest._add(v)
  113. })
  114. }*/
  115. }
  116.  
  117. /**
  118. Add {@_set} and {@_del} with key save semantic i.e. deleting key do not change other keys
  119. Alsa {@_set} automatically create new keys if key did not exist in container unlike {@_overwrite}'s behavior from {Pure}
  120. {@_del} allow implemet missed functionality in {Enumerable} as {@_filter} and {@_merge}.
  121. */
  122. fix Map = class(Key: Fn, Value: Fn) extends Enumerable(Key, Value)
  123. {
  124. /**
  125. set or create {v} by {k}.
  126. -> container is changed or not
  127. */
  128. method _set += #(k: Key, v: Value) -> Bool { abstract }
  129.  
  130. /** @wrapper of {@_set} */
  131. operator 'set [x]' += #(k: Key, v: Value) { @_set(k, v) }
  132.  
  133. /**
  134. delete {k}
  135. -> container is changed or not
  136. */
  137. method _del += #(k: Key) -> Bool { abstract }
  138. }
  139.  
  140. /**
  141. Set is just Map Value -> Value i.e. Key = Value but has {Fillable} semantic
  142. */
  143. fix Set = class(Value: Fn) extends Map(Value, Value), Fillable(Key, Value)
  144. {
  145. /** add {v} to container. -> Key */
  146. method _add += #(v: Value) -> Value { -> @_set(v, v) }
  147. }
  148.  
  149. fix Iter = class(Container: Fn) {
  150. fix Container = Container
  151. fix get container += #() -> Container { abstract }
  152. fix operator "++x" += #{ abstract }
  153. fix operator "--x" += #{ abstract }
  154. fix operator "x < y" += #(y) -> Bool { abstract }
  155. }
  156.  
  157. /**
  158. keys of container are ordered i.e. there is first key, end key, and you can traverse back and front using {@_prev} and {@_next}
  159. Traverse is strict i.e. throws RangeError if you try to {@_prev(@begin)} or {@_next(@end)}
  160. */
  161. fix Ordered = class(Key: Fn, Value: Fn) extends Enumerable(Key, Value)
  162. {
  163. /**
  164. set {v} by existing {k}.
  165. If {k} doesn't exist - @throws RangeError.
  166. -> is container changed or not
  167. */
  168. method _set += #(k: Key, v: Value) -> Bool { @_overwrite(k, v) }
  169.  
  170. /** first key of container */
  171. abstract get fix begin += #() -> Key {}
  172.  
  173. /** end key of container */
  174. abstract get fix end += #() -> Key {}
  175.  
  176. /**
  177. get key after({n} times) {k}.
  178. @throws RangeError if {k == @end}
  179. */
  180. abstract fix _next += #(k: Key, n: ?Num = 1) -> Key {}
  181.  
  182. /**
  183. get key before({n} times) {k}.
  184. @throws RangeError if {k == @begin}
  185. */
  186. abstract fix _prev += #(k: Key, n: ?Num = 1) -> Key {}
  187.  
  188.  
  189. method fix _diff += #(k1: Key, k2: Key)
  190. /**
  191. ordered keys allows implemet {@_each}
  192. */
  193. method fix _each += #(_fn: Fn){
  194. for(var k = @begin; k != @end; k = @_next(k))
  195. {
  196. fix ret = _fn._lazyCall(k, @_get(k), @)
  197. if(ret != null)
  198. -> ret
  199. }
  200. }
  201.  
  202. method _sort = #(_less){
  203. fix _part = #(b, e){
  204. if()
  205. }
  206. }
  207. }
  208.  
  209. /**
  210. Allow add/remove last item with key save semantic, Fillable
  211. */
  212. fix Stack = class(Key: Fn, Value: Fn) extends Ordered(Key, Value), Fillable(Key, Value)
  213. {
  214. /**
  215. add {v} to end of container
  216. -> key of {v}
  217. */
  218. method _pushBack += #(v: Value) -> Key { abstract }
  219.  
  220. /**
  221. -> and remove last item of container
  222. -> null if container already empty
  223. */
  224. method _popBack += #() -> ?Value { abstract }
  225.  
  226. /**
  227. @wrapper of {@_pushBack} for {Fillable}
  228. */
  229. method _add = #(v: Value) -> Key { -> @_pushBack(v) }
  230. }
  231.  
  232. /**
  233. Allow add/remove first item with key save semantic (which is different from Array)
  234. */
  235. fix Deque = class(Key: Fn, Value: Fn) extends Stack(Key, Value)
  236. {
  237. /**
  238. insert {v} at begin of container
  239. -> key of {v}
  240. */
  241. method _pushFront = #(v: Value) -> Key { abstract }
  242.  
  243. /**
  244. -> and remove first item of container
  245. -> null if container already empty
  246. */
  247. method _popFront =+ #() -> ?Value { abstract }
  248.  
  249. /**
  250. rotate deque left
  251. */
  252. method _rol =+ #(n: Natural = 1){
  253. if(@_isEmpty())
  254. ->
  255. while(--n >= 0)
  256. @_pushBack(@_popFront())
  257. }
  258.  
  259. /**
  260. rotate deque right
  261. */
  262. method _ror =+ #(n: Natural = 1){
  263. if(@_isEmpty())
  264. ->
  265. while(--n >= 0)
  266. @_pushFront(@_popBack())
  267. }
  268. }
  269.  
  270. /**
  271. Add ability to delete any item from container with key save semantic
  272. */
  273. fix FlatList = class(Key: Fn, Value: Fn) extends Deque(Key, Value)
  274. {
  275. /**
  276. delete value by {k}
  277. -> yes is {k} was in {@}
  278. */
  279. method _del =+ #(k: Key) -> Bool { abstract }
  280. }
  281.  
  282. /**
  283. Add ability to insert value at not only at front or back with key save semantic
  284. */
  285. fix List = class(Key: Fn, Value: Fn) extends FlatList(Key, Value)
  286. {
  287. /**
  288.  
  289. */
  290. method _insertBefore =+ #(k: Key, v: Value) -> Key { abstract }
  291. method _insertAfter =+ #(k: Key, v: Value) -> Key { -> @_insertBefore(@_next(k), v) }
  292. }
  293.  
  294. fix Array = class(Key: Fn, Value: Fn) extends Stack(Key, Value)
  295. {
  296. method _insertBefore =+ #(k: Key, v: Value) -> Key { abstract }
  297. method _del =+ #(k: Key) -> Bool { abstract }
  298. method _insertAfter =+ #(k: Key, v: Value) -> Key { -> @_insertBefore(@_next(k), v) }
  299. method _pushFront =+ #(v: Value) -> Key { -> @_insertBefore(@begin, v) }
  300. method _popFront =+ #() -> ?Value { fix k = @begin, v = @[k]; @_del(k); -> v }
  301. method _pushBack =+ #(v: Value) -> Key { -> @_insertBefore(@end, v) }
  302. method _popBack =+ #() -> ?Value { fix k = @_prev(@end), v = @[k]; @_del(k); -> v }
  303. }
  304.  
  305. fix RangeError = class extends Error
  306. {
  307.  
  308. }
Add Comment
Please, Sign In to add comment