-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from vic1707/HOF
Hof
- Loading branch information
Showing
14 changed files
with
248 additions
and
40 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
macro_rules! assert_f64_eq { | ||
($left:expr, $right:expr) => { | ||
assert!(($left - $right).abs() < f64::EPSILON); | ||
}; | ||
($left:expr, $right:expr, $($arg:tt)+) => { | ||
assert!(($left - $right).abs() < f64::EPSILON, $($arg)+); | ||
}; | ||
} | ||
|
||
pub(crate) use assert_f64_eq; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/* Modules */ | ||
mod issues; | ||
mod macros; | ||
mod parser; | ||
mod thread_safety; | ||
mod xprs; |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* Crate imports */ | ||
use super::super::macros::assert_f64_eq; | ||
use crate::{xprs_fn, Context, Parser, Xprs}; | ||
|
||
#[test] | ||
fn test_higher_order_functions() { | ||
let xprs_hof = Xprs::try_from("2x + y").unwrap(); | ||
let fn_hof = xprs_hof.bind2("x", "y").unwrap(); | ||
let hof = xprs_fn!("hof", dyn fn_hof, 2); | ||
let ctx = Context::default().with_fn(hof); | ||
let parser = Parser::new_with_ctx(ctx); | ||
|
||
let bare_use = parser.parse("hof(2, 3)").unwrap(); | ||
assert_f64_eq!(bare_use.eval_unchecked(&[].into()), 7.0_f64); | ||
|
||
let invalid_use = parser.parse("hof(2)"); | ||
assert!( | ||
invalid_use.is_err(), | ||
"Expected error for invalid use of hof" | ||
); | ||
|
||
let complex_use = parser.parse("hof(2, 3) + 3 * hof(4, 5)").unwrap(); | ||
assert_f64_eq!(complex_use.eval_unchecked(&[].into()), 46.0_f64); | ||
|
||
let nested_var_use = parser.parse("hof(x, hof(2, 3))").unwrap(); | ||
assert_f64_eq!( | ||
nested_var_use.eval_unchecked(&[("x", 42.0_f64)].into()), | ||
91.0_f64 | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/* Modules */ | ||
mod eval; | ||
mod hof; | ||
mod simplify; |
Oops, something went wrong.