ombre: Implement `pop` on vectors
b61f11f294d923d59df8c55f1ffa66d9687238b6
1 parent
fb4a054b
ombre/src/lisp/evaluator.rs
+13 -0
| 1140 | 1140 | env.intrinsic( |
|
| 1141 | 1141 | "push", |
|
| 1142 | 1142 | "Push elements to the end of a vector", |
|
| 1143 | 1143 | intrinsic::push, |
|
| 1144 | 1144 | ); |
|
| 1145 | + | env.intrinsic( |
|
| 1146 | + | "pop", |
|
| 1147 | + | "Pop an element from the end of a vector", |
|
| 1148 | + | intrinsic::pop, |
|
| 1149 | + | ); |
|
| 1145 | 1150 | env.intrinsic("reverse", "Reverse a tuple or vector", intrinsic::reverse); |
|
| 1146 | 1151 | env.intrinsic( |
|
| 1147 | 1152 | "range", |
|
| 1148 | 1153 | "Create a vector of numbers in a specified range", |
|
| 1149 | 1154 | intrinsic::range, |
| 1765 | 1770 | v.borrow_mut().extend(args); |
|
| 1766 | 1771 | ||
| 1767 | 1772 | Ok(Expr::Nil) |
|
| 1768 | 1773 | } |
|
| 1769 | 1774 | ||
| 1775 | + | /// Pop an element from the end of a vector. |
|
| 1776 | + | pub fn pop(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> { |
|
| 1777 | + | let (vec,) = <(Mut<Vec<Expr>>,)>::try_from_args(args)?; |
|
| 1778 | + | let value = vec.borrow_mut().pop().unwrap_or_default(); |
|
| 1779 | + | ||
| 1780 | + | Ok(value) |
|
| 1781 | + | } |
|
| 1782 | + | ||
| 1770 | 1783 | /// Reverse a vector. |
|
| 1771 | 1784 | pub fn reverse(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> { |
|
| 1772 | 1785 | let (v,) = <(Mut<Vec<Expr>>,)>::try_from_args(args)?; |
|
| 1773 | 1786 | v.borrow_mut().reverse(); |
|
| 1774 | 1787 |