Skip to content

Instantly share code, notes, and snippets.

@lfranchi
Created December 5, 2012 20:04
Show Gist options
  • Save lfranchi/4219014 to your computer and use it in GitHub Desktop.
Save lfranchi/4219014 to your computer and use it in GitHub Desktop.
(defn zipper
[]
[() ()])
(defn insert-at-cursor
[zipper newchar]
[(cons newchar (first zipper)) (second zipper)])
(defn get-at-cursor
[zipper]
(ffirst zipper))
(defn move-left
[zipper]
[(rest (first zipper)) (cons (ffirst zipper) (second zipper))])
(defn move-right
[zipper]
[(cons (first (second zipper)) (first zipper)) (rest (second zipper))])
(defn delete-at-cursor
[zipper]
[(rest (first zipper)) (second zipper)])
;; tests
(def z (zipper))
(def z (insert-at-cursor z "a"))
(def z (insert-at-cursor z "b"))
(def z (insert-at-cursor z "c"))
(def z (insert-at-cursor z "d"))
(get-at-cursor z)
(def z (move-left z))
(get-at-cursor z)
(def z (delete-at-cursor z))
(get-at-cursor z)
(def z (insert-at-cursor z "q"))
(get-at-cursor z)
(def z (move-right z))
(get-at-cursor z)
(def z (move-right z))
(get-at-cursor z)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment