You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lib.Enumerable.asp 6.3KB

8 kuukautta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <%
  2. Class EnumerableHelper_Class
  3. Private m_list
  4. Public Sub Init(list)
  5. set m_list = list
  6. End Sub
  7. Public Sub Class_Terminate
  8. set m_list = Nothing
  9. End Sub
  10. Public Default Function Data()
  11. set Data = m_list
  12. End Function
  13. '---------------------------------------------------------------------------------------------------------------------
  14. ' Convenience wrappers
  15. '---------------------------------------------------------------------------------------------------------------------
  16. Public Function Count()
  17. Count = m_list.Count()
  18. End Function
  19. Public Function First()
  20. Assign First, m_list.Front()
  21. End Function
  22. Public Function Last()
  23. Assign Last, m_list.Back()
  24. End Function
  25. '---------------------------------------------------------------------------------------------------------------------
  26. ' Methods that return a single value
  27. '---------------------------------------------------------------------------------------------------------------------
  28. 'true if all elements of the list satisfy the condition
  29. Public Function All(condition)
  30. dim item_, all_matched : all_matched = true
  31. dim it : set it = m_list.Iterator
  32. Do While it.HasNext
  33. Assign item_, it.GetNext()
  34. If "String" = typename(condition) then
  35. If Not eval(condition) then
  36. all_matched = false
  37. End If
  38. Else
  39. If Not condition(item_) then
  40. all_matched = false
  41. End If
  42. End If
  43. If Not all_matched then Exit Do
  44. Loop
  45. All = all_matched
  46. End Function
  47. 'true if any element of the list satisfies the condition
  48. Public Function Any(condition)
  49. Any = Not All("Not " & condition)
  50. End Function
  51. Public Function Max(expr)
  52. dim V_, item_, maxval
  53. dim it : set it = m_list.Iterator
  54. If "String" = typename(expr) then
  55. While it.HasNext
  56. Assign item_, it.GetNext()
  57. Assign V_, eval(expr)
  58. If V_ > maxval then maxval = V_
  59. Wend
  60. Else
  61. While it.HasNext
  62. Assign item_, it.GetNext()
  63. Assign V_, expr(item_)
  64. If V_ > maxval then maxval = V_
  65. Wend
  66. End If
  67. Max = maxval
  68. End Function
  69. Public Function Min(expr)
  70. dim V_, item_, minval
  71. dim it : set it = m_list.Iterator
  72. If "String" = typename(expr) then
  73. While it.HasNext
  74. Assign item_, it.GetNext()
  75. If IsEmpty(minval) then ' empty is always less than everything so set it on first pass
  76. Assign minval, item_
  77. End If
  78. Assign V_, eval(expr)
  79. If V_ < minval then minval = V_
  80. Wend
  81. Else
  82. While it.HasNext
  83. Assign item_, it.GetNext()
  84. If IsEmpty(minval) then
  85. Assign minval, item_
  86. End If
  87. V_ = expr(item_)
  88. If V_ < minval then minval = V_
  89. Wend
  90. End If
  91. Min = minval
  92. End Function
  93. Public Function Sum(expr)
  94. dim V_, item_
  95. dim it : set it = m_list.Iterator
  96. While it.HasNext
  97. Assign item_, it.GetNext()
  98. execute "V_ = V_ + " & expr
  99. Wend
  100. Sum = V_
  101. End Function
  102. '---------------------------------------------------------------------------------------------------------------------
  103. ' Methods that return a new instance of this class
  104. '---------------------------------------------------------------------------------------------------------------------
  105. 'returns a list that results from running lambda_or_proc once for every element in the list
  106. Public Function Map(lambda_or_proc)
  107. dim list2 : set list2 = new LinkedList_Class
  108. dim it : set it = m_list.Iterator
  109. dim item_
  110. If "String" = typename(lambda_or_proc) then
  111. dim V_
  112. While it.HasNext
  113. Assign item_, it.GetNext()
  114. execute lambda_or_proc
  115. list2.Push V_
  116. Wend
  117. Else
  118. While it.HasNext
  119. Assign item_, it.GetNext()
  120. list2.Push lambda_or_proc(item_)
  121. Wend
  122. End If
  123. set Map = Enumerable(list2)
  124. End Function
  125. 'alias to match IEnumerable for convenience
  126. Public Function [Select](lambda_or_proc)
  127. set [Select] = Map(lambda_or_proc)
  128. End Function
  129. 'returns list containing first n items
  130. Public Function Take(n)
  131. dim list2 : set list2 = new LinkedList_Class
  132. dim it : set it = m_list.Iterator
  133. dim i : i = 1
  134. While it.HasNext And i <= n
  135. list2.Push it.GetNext()
  136. i = i + 1
  137. Wend
  138. set Take = Enumerable(list2)
  139. End Function
  140. 'returns list containing elements as long as the condition is true, and skips the remaining elements
  141. Public Function TakeWhile(condition)
  142. dim list2 : set list2 = new LinkedList_Class
  143. dim item_, V_, bln
  144. dim it : set it = m_list.Iterator
  145. Do While it.HasNext
  146. Assign item_, it.GetNext()
  147. If "String" = typename(condition) then
  148. 'execute condition
  149. If Not eval(condition) then Exit Do
  150. Else
  151. If Not condition(item_) then Exit Do
  152. End If
  153. list2.Push item_
  154. Loop
  155. set TakeWhile = Enumerable(list2)
  156. End Function
  157. 'returns a list containing only elements that satisfy the condition
  158. Public Function Where(condition)
  159. dim list2 : set list2 = new LinkedList_Class
  160. dim it : set it = m_list.Iterator
  161. dim item_
  162. While it.HasNext
  163. Assign item_, it.GetNext()
  164. If "String" = typename(condition) then
  165. If eval(condition) then list2.Push item_
  166. Else
  167. If condition(item_) then list2.Push item_
  168. End If
  169. Wend
  170. set Where = Enumerable(list2)
  171. End Function
  172. End Class
  173. Function Enumerable(list)
  174. dim E : set E = new EnumerableHelper_Class
  175. E.Init list
  176. set Enumerable = E
  177. End Function
  178. %>

Powered by TurnKey Linux.