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.
 
 
 
 
 
 

188 regels
6.0 KiB

  1. (import "cxr.slime")
  2. ;; alist:
  3. ;; [ |/]
  4. ;; |
  5. ;; V
  6. ;; [ | ]------------->[ | ]-------------> ...
  7. ;; | |
  8. ;; V V
  9. ;; [ | ]->value1 [ | ]->value2
  10. ;; | |
  11. ;; V V
  12. ;; key1 key2
  13. ;;
  14. ;; '(((key1 . value1) (key2 . value2)))
  15. ;; plist:
  16. ;; [ |/]
  17. ;; |
  18. ;; V
  19. ;; [ | ]------------->[ | ]-------------> ...
  20. ;; | |
  21. ;; V V
  22. ;; :key1 value1
  23. ;;
  24. ;; '((:key1 value1 :key2 value2))
  25. (define key-not-found-index -1)
  26. (define (make-alist)
  27. (set-type
  28. '(())
  29. :alist))
  30. (define (make-plist)
  31. (set-type
  32. '(())
  33. :plist))
  34. (define (pprint-alist alist)
  35. (let ((associations (first alist)))
  36. (define (pprint-intern associations)
  37. (when associations
  38. (printf " "
  39. (caar associations) "->"
  40. (cdar associations))
  41. (pprint-intern (rest associations))))
  42. (print "(")
  43. (when associations
  44. (print "\n")
  45. (pprint-intern associations))
  46. (print ")\n")))
  47. (define (pprint-plist plist)
  48. (let ((props (first plist)))
  49. (define (pprint-intern props)
  50. (when props
  51. (printf " "
  52. (car props) "->"
  53. (cadr props))
  54. (pprint-intern (cddr props))))
  55. (print "(")
  56. (when props
  57. (print "\n")
  58. (pprint-intern props))
  59. (print ")\n")))
  60. (define (alist-get alist key)
  61. (let ((associations (first alist)))
  62. (define (alist-get-intern associations key)
  63. (cond ((null? associations)
  64. (error "key was not found in alist"))
  65. ((= (caar associations) key)
  66. (cdar associations))
  67. (else (alist-get-intern (rest associations) key))))
  68. (alist-get-intern associations key)))
  69. (define (alist-find alist key)
  70. (let ((associations (first alist)))
  71. (define (alist-find-intern associations key current-index)
  72. (cond ((null? associations) key-not-found-index)
  73. ((= (caar associations) key) current-index)
  74. (else (alist-find-intern (rest associations)
  75. key
  76. (+ 1 current-index)))))
  77. (alist-find-intern associations key 0)))
  78. (define (alist-key-exists? alist key)
  79. (not (= (alist-find alist key)
  80. key-not-found-index)))
  81. (define (alist-remove! alist key)
  82. (let ((index (alist-find alist key)))
  83. (define (alist-remove!-internal asociations index)
  84. ;; reminder: we only get called if we are not replacing the
  85. ;; first element in the alist
  86. ;; reminder2: we know that the key exists
  87. (if (= index 1)
  88. ;; we want to remove the next one, so we set our
  89. ;; cdr to the next next one
  90. (mutate associations (pair (first associations)
  91. (rest (rest associations))))
  92. ;; else cdr-recurse
  93. (alist-remove!-internal (rest asociations) (- index 1))))
  94. (cond ((= index key-not-found-index) (error "key to remove was not found"))
  95. ((= index 0) (mutate alist (pair (cdar alist) ())))
  96. (else (alist-remove!-internal alist index))))
  97. alist)
  98. (define (alist-set! alist key value)
  99. (mutate alist (pair (pair (pair key value)
  100. (car alist))
  101. ())))
  102. (define (alist-set-overwrite! alist key value)
  103. (let ((associations (first alist)))
  104. (define (alist-set-overwrite-intern associations key value)
  105. (cond ((= (caar associations) key)
  106. (mutate (car associations) (pair key value)))
  107. ((null? associations) (alist-set! alist key value))
  108. (else (alist-set-overwrite-intern
  109. (rest associations) key value))))
  110. (alist-set-overwrite-intern associations key value))
  111. alist)
  112. (define (plist-get plist prop)
  113. (let ((props (first plist)))
  114. (define (plist-get-intern props prop)
  115. (cond ((null? props)
  116. (error "property was not found in plist"))
  117. ((= (car props) prop)
  118. (cadr props))
  119. (else (plist-get-intern (cddr props) prop))))
  120. (plist-get-intern props prop)))
  121. (define (plist-set! plist prop value)
  122. (mutate plist (pair (pair prop (pair value (first plist))) ())))
  123. (define (plist-set-overwrite! plist prop value)
  124. (let ((props (first plist)))
  125. (define (plist-set-overwrite-intern props prop value)
  126. (cond ((= (car props) prop)
  127. (mutate (cdr props) (pair value (cddr props))))
  128. ((null? props) (plist-set! plist prop value))
  129. (else (plist-set-overwrite-intern
  130. (cddr props) prop value))))
  131. (plist-set-overwrite-intern props prop value))
  132. plist)
  133. (define (plist-find plist prop)
  134. (let ((props (first plist)))
  135. (define (plist-find-intern props prop current-index)
  136. (cond ((null? props) key-not-found-index)
  137. ((= (car props) prop) current-index)
  138. (else (plist-find-intern (cddr props) prop
  139. (+ 1 current-index)))))
  140. (plist-find-intern props prop 0)))
  141. (define (plist-prop-exists? plist prop)
  142. (not (= (plist-find plist prop)
  143. key-not-found-index)))
  144. (define (plist-remove! plist prop)
  145. (let ((index (plist-find plist prop)))
  146. (define (plist-remove!-internal props index)
  147. ;; reminder: we only get called if we are not replacing the
  148. ;; first element in the alist
  149. ;; reminder2: we know that the key exists
  150. (if (= index 1)
  151. ;; we want to remove the next one, so we set our
  152. ;; cdr to the next next one
  153. (mutate (cdar props) (pair (cadar props) ;; xD nice meme dude!!!
  154. (cdr (cdr (cdr (cdar props))))))
  155. ;; else cdr-recurse
  156. (plist-remove!-internal (cddr props) (- index 1))))
  157. (cond ((= index key-not-found-index) (error "prop to remove was not found"))
  158. ((= index 0) (mutate plist (pair (cddar plist) ())))
  159. (else (plist-remove!-internal plist index))))
  160. plist)