Created
July 26, 2017 00:15
-
-
Save remexre/6cacdafdec26db977c8058601a4c4883 to your computer and use it in GitHub Desktop.
oftlisp typeclasses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(deftypeclass (indexed 'a) | |
(fn ((get 'a) (this @) (index fixnum))) | |
(fn ((len fixnum) (this @)))) | |
; An example data structure. | |
(deftype (append-list 'a) | |
(list (list 'a))) | |
(instance (append-list 'a) (indexed 'a) | |
(defn (get this index) | |
...) | |
(defn (len this) | |
(-> this | |
(map len) | |
sum))) | |
(defn (main) | |
; I can't find a way to get rid of the need for a type annotation to | |
; transform a built-in data structure into a deftype'd one. The problem is: | |
(def x '((1 2 3) (4 5 6) (7 8 9))) | |
(def y (as (append-list fixnum) x)) | |
(assert-eq (len x) 3) | |
(assert-eq (len y) 9)) | |
; And as an example of a non-required function on a typeclass: | |
(defn ((last 'a) (x (indexed 'a))) | |
(get x (1- (len x)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment