list.fnl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. ;;; Lispy-Fennel (LF) --- The Functional Fennel Library
  2. ;;;
  3. ;;; Copyright (C) 2020 Kevin "The Nuclear" Bloom <nuclearkev@dragora.org>
  4. ;;;
  5. ;;; This file is part of LF.
  6. ;;;
  7. ;;; LF is free software: you can redistribute it and/or modify
  8. ;;; it under the terms of the MIT License.
  9. ;;;
  10. ;;; LF is distributed in the hope that it will be useful,
  11. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;;; MIT License for more details.
  14. ;;;
  15. ;;; You should have received a copy of the MIT License
  16. ;;; along with LF. If not, see <https://opensource.org/licenses/MIT>.
  17. (local view (include :fennelview))
  18. (local list {})
  19. (fn empty? [l]
  20. (not (next l)))
  21. (fn list.empty? [l]
  22. (= 0 (length l)))
  23. (fn list.range [str end]
  24. (var x str)
  25. (var l [])
  26. (while (not (= x end))
  27. (table.insert l x)
  28. (set x (+ 1 x)))
  29. list)
  30. (fn list.nth [ind l]
  31. (. l ind))
  32. (fn list.car [l]
  33. (. l 1))
  34. (fn list.cdr [l]
  35. (var ls l)
  36. (table.remove ls 1)
  37. ls)
  38. (fn list.cadr [l]
  39. (list.car
  40. (list.cdr
  41. l)))
  42. (fn list.cddr [l]
  43. (list.cdr
  44. (list.cdr
  45. l)))
  46. (fn list.append [l1 l2]
  47. (var total-list l1)
  48. (each
  49. [k v (ipairs l2)]
  50. (table.insert total-list v))
  51. total-list)
  52. (fn list.last [l]
  53. (let [last-idx (length l)]
  54. (. l last-idx)))
  55. (fn list.reverse [l]
  56. (var x [])
  57. (each
  58. [k v (pairs l)]
  59. (set x (list.append [v] x)))
  60. x)
  61. (fn list.flatten [l]
  62. (var x [])
  63. (each [k v (ipairs l)]
  64. (if (and (= (type v) "table")
  65. (empty? v))
  66. nil
  67. (and (= (type v) "table")
  68. (. v 1)) ;make sure it's a list not just a table
  69. (set x (list.append v x))
  70. (table.insert x v)))
  71. x)
  72. (fn list.split [l n]
  73. (var idx 1)
  74. (var top [])
  75. (var rest l)
  76. (while (not (= idx n))
  77. (let [car (. rest 1)]
  78. (table.remove rest 1)
  79. (table.insert top car)
  80. (set idx (+ 1 idx))))
  81. [top rest])
  82. (fn list.insert [l idx val]
  83. (let [split-list (list.split l idx)]
  84. (var top (. split-list 1))
  85. (var rest (. split-list 2))
  86. (table.insert top val)
  87. (list.append top rest)))
  88. (fn list.split-when [f l]
  89. ;; (var idx 1)
  90. (var top [])
  91. (var rest l)
  92. (var done false)
  93. (while (not done)
  94. (let [car (. rest 1)]
  95. (if (list.empty? rest)
  96. (set done true)
  97. (f car)
  98. (set done true)
  99. (do
  100. (table.remove rest 1)
  101. (table.insert top car)))))
  102. [top rest])
  103. (fn list.find [f l]
  104. (var val nil)
  105. (each [k v (ipairs l)]
  106. (let [found? (f v)]
  107. (when found?
  108. (set val v))))
  109. val)
  110. (fn list.unique [l]
  111. (var uni [])
  112. (each [k v (ipairs l)]
  113. (when (not (list.find (fn [x] (= x v)) uni))
  114. (table.insert uni v))) uni)
  115. list