Permalink
Browse files

Added a macro for slice.

Array.prototype.slice is used often in Sibilant (and JavaScript) so it
makes sense that it has its own macro rather than rewriting the same code
many times.
  • Loading branch information...
1 parent e816429 commit 2b0ce2b33a919fe6e2acff1368afcc670311bf7b @matthewp matthewp committed Oct 20, 2012
Showing with 9 additions and 1 deletion.
  1. +6 −1 include/macros.sibilant
  2. +3 −0 test/slice.sibilant
View
7 include/macros.sibilant
@@ -361,4 +361,9 @@
(concat ") {" (indent (translate body)) "}"))))
(defmacro instance-of? (item type)
- (concat "(" (translate item) " instanceof " (translate type) ")"))
+ (concat "(" (translate item) " instanceof " (translate type) ")"))
+
+(defmacro slice (list &optional begin &optional end)
+ (concat "Array.prototype.slice.call(" (translate list)
+ ", " (or (translate begin) 0) (if (defined? end) (concat ", "
+ (translate end) ")") ")")))
View
3 test/slice.sibilant
@@ -0,0 +1,3 @@
+(defvar sliced (slice '(1 2 3 4)))
+
+(defvar other (slice '(1 2 3 4) 0 2))

0 comments on commit 2b0ce2b

Please sign in to comment.