closure.glisp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. (defn foldl [lst fun acc]
  2. (cond
  3. (empty? lst) acc
  4. (foldl (cdr lst) fun (fun (car lst) acc))
  5. ))
  6. (defn filter [lst fun]
  7. (foldl lst
  8. (fn [x l]
  9. (cond
  10. (fun x) (append l x)
  11. l))
  12. [])
  13. )
  14. (defn g-map [fun lst]
  15. (foldl lst (fn [x l] (fun x)) []))
  16. (defn even? [x]
  17. (cond
  18. (number? x)
  19. (cond
  20. (float? x) false
  21. (= (bit-and x 1) 0))
  22. false
  23. ))
  24. (defn newStore [items]
  25. (let* [
  26. idx [-1]
  27. fun (fn []
  28. (cond
  29. (< (+ (aget idx 0) 1) (len items))
  30. (begin
  31. (aset! idx 0 (+ (aget idx 0) 1))
  32. (aget items (aget idx 0))
  33. )
  34. ()))
  35. ] fun))
  36. (def evens (newStore [2 4 6 8 10]))
  37. (map (fn [x]
  38. (assert (= x (evens))))
  39. (filter [1 2 3 4 5 6 7 8 9 10] even?)
  40. )
  41. (def s (newStore [10 9 8 7 6 5 4 3 2 1]))
  42. (g-map (fn [x] (assert (= x (s)))) [10 9 8 7 6 5 4 3 2 1])
  43. (def s (newStore [10 9 8 7 6 5 4 3 2 1]))
  44. (map (fn [x] (assert (= x (s)))) [10 9 8 7 6 5 4 3 2 1])
  45. (def decending (newStore [10 9 8 7 6 5 4 3 2 1]))
  46. (def s (newStore [10 9 8 7 6 5 4 3 2 1]))
  47. (defn drainStore []
  48. (let [ v (s) ]
  49. (cond
  50. (empty? v) (assert (= (decending) ()))
  51. ((begin
  52. (assert (= (decending) v))
  53. (drainStore)))))
  54. )
  55. (drainStore)
  56. (def a 1)
  57. (defn enclosure []
  58. (let [func1 (fn [thunk] (let [a 2] (thunk)))
  59. func2 (fn [] (assert (= a 1)))]
  60. (func1 func2)))
  61. (enclosure)