Generic functions in 45 lines
Clearly, there could be more features and it could be more efficient, but in terms of producing the simplest thing that works for me, I'm really happy with this: https://gist.github.com/eraserhd/5435505 Usage: (define-generic (length object)) (add-method (length (vector? v)) (vector-length s)) (add-method (length (string? s)) (string-length s)) (add-method (length (list? l)) (##length l)) (length "hello") => 5 (length '(1 2 3)) => 3 (length '#(a b)) => 2
Afficher les réponses par date
Neat! Now when on the topic, some thoughts on what would be a next level in this, generally: A nice way to deliver generics would be to make it control flow analysis-optimized at the level of the module system/code expander/expanded code. CFA2 might be something, Dimitris Vardoulakis' thesis may tell if it delivers for call/cc, and possibly Matt Might http://matt.might.net/ has something to say on the topic, search for abstract interpreter. (ref<http://brendaneich.com/2010/08/static-analysis-ftw/> ref <http://matt.might.net/articles/implementation-of-kcfa-and-0cfa/>) As long as that works well enough to optimize ~80-95% of uses to ordinary procedure calls you've got a generics system that really delivers for most purposes. Of course without that delivers for lots of purposes too. Feel free to share any reflections. Brgds 2013/4/22 Jason Felice <jason.m.felice@gmail.com>
Clearly, there could be more features and it could be more efficient, but in terms of producing the simplest thing that works for me, I'm really happy with this:
https://gist.github.com/eraserhd/5435505
Usage:
(define-generic (length object))
(add-method (length (vector? v)) (vector-length s)) (add-method (length (string? s)) (string-length s)) (add-method (length (list? l)) (##length l))
(length "hello") => 5 (length '(1 2 3)) => 3 (length '#(a b)) => 2
_______________________________________________ Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
participants (2)
-
Jason Felice -
Mikael