Skip to content

Commit

Permalink
Expose TulispContext::eval_and_then (#53)
Browse files Browse the repository at this point in the history
Also update `cxr` and `cxr_and_then` methods to use the more lenient
`FnOnce` signature.
  • Loading branch information
shsms authored Nov 18, 2023
2 parents defea4e + b84b3f7 commit 29e21e6
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/builtin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ These functions need to be organized into categories. They are grouped here for
| `concat` | ☑️ | for strings |
| `expt` | ☑️ | |
| `load` | ☑️ | |
| `intern` | ☑️ | no optional obarray param, always uses default. |
## Other predicates
Expand Down
12 changes: 11 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{collections::HashMap, fs};
use crate::{
builtin,
error::Error,
eval::{eval, eval_basic, funcall, DummyEval},
eval::{eval, eval_and_then, eval_basic, funcall, DummyEval},
list,
parse::parse,
TulispObject,
Expand Down Expand Up @@ -85,6 +85,16 @@ impl TulispContext {
eval(self, value)
}

/// Evaluates the given value, run the given function on the result of the
/// evaluation, and returns the result of the function.
pub fn eval_and_then<T>(
&mut self,
expr: &TulispObject,
f: impl FnOnce(&TulispObject) -> Result<T, Error>,
) -> Result<T, Error> {
eval_and_then(self, expr, f)
}

/// Calls the given function with the given arguments, and returns the
/// result.
pub fn funcall(
Expand Down
2 changes: 1 addition & 1 deletion src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ pub(crate) fn eval_check_null(ctx: &mut TulispContext, expr: &TulispObject) -> R
pub(crate) fn eval_and_then<T>(
ctx: &mut TulispContext,
expr: &TulispObject,
func: fn(&TulispObject) -> Result<T, Error>,
func: impl FnOnce(&TulispObject) -> Result<T, Error>,
) -> Result<T, Error> {
let mut result = None;
eval_basic(ctx, expr, &mut result)?;
Expand Down
4 changes: 2 additions & 2 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ macro_rules! extractor_cxr_and_then_fn {
)]
pub fn $name<Out: Default>(
&self,
f: impl FnMut(&TulispObject) -> Result<Out, Error>,
f: impl FnOnce(&TulispObject) -> Result<Out, Error>,
) -> Result<Out, Error> {
self.rc
.borrow()
Expand All @@ -594,7 +594,7 @@ macro_rules! extractor_cxr_and_then_fn {
#[doc(hidden)]
pub fn $name<Out: Default>(
&self,
f: impl FnMut(&TulispObject) -> Result<Out, Error>,
f: impl FnOnce(&TulispObject) -> Result<Out, Error>,
) -> Result<Out, Error> {
self.rc
.borrow()
Expand Down
6 changes: 3 additions & 3 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ macro_rules! make_cxr_and_then {
($name:ident, $($step:tt)+) => {
pub fn $name<Out: Default>(
&self,
func: impl FnMut(&TulispObject) -> Result<Out, Error>,
func: impl FnOnce(&TulispObject) -> Result<Out, Error>,
) -> Result<Out, Error> {
match self {
TulispValue::List { cons, .. } => cons.$($step)+(func),
Expand Down Expand Up @@ -789,7 +789,7 @@ impl TulispValue {

pub fn car_and_then<Out: Default>(
&self,
mut func: impl FnMut(&TulispObject) -> Result<Out, Error>,
func: impl FnOnce(&TulispObject) -> Result<Out, Error>,
) -> Result<Out, Error> {
match self {
TulispValue::List { cons, .. } => func(cons.car()),
Expand All @@ -803,7 +803,7 @@ impl TulispValue {

pub fn cdr_and_then<Out: Default>(
&self,
mut func: impl FnMut(&TulispObject) -> Result<Out, Error>,
func: impl FnOnce(&TulispObject) -> Result<Out, Error>,
) -> Result<Out, Error> {
match self {
TulispValue::List { cons, .. } => func(cons.cdr()),
Expand Down

0 comments on commit 29e21e6

Please sign in to comment.