fix up matches
there was a bug i ran into when matching on the empty list
This commit is contained in:
parent
798245aa0f
commit
17463c839f
1 changed files with 5 additions and 3 deletions
|
@ -502,6 +502,7 @@ type Matches<'src> = HashMap<Symbol, &'src Sexp>;
|
||||||
|
|
||||||
// DONE?: there. can. be. at most. one. match.
|
// DONE?: there. can. be. at most. one. match.
|
||||||
// i'm happy that this is faster, but it might be worth going back to the per-variable thing
|
// i'm happy that this is faster, but it might be worth going back to the per-variable thing
|
||||||
|
#[must_use]
|
||||||
pub fn matches<'src>(vars: &[Symbol], lhs: &Sexp, expr: &'src Sexp) -> Option<Matches<'src>> {
|
pub fn matches<'src>(vars: &[Symbol], lhs: &Sexp, expr: &'src Sexp) -> Option<Matches<'src>> {
|
||||||
match (lhs, expr) {
|
match (lhs, expr) {
|
||||||
(Sexp::Atom(a), Sexp::Atom(b)) => {
|
(Sexp::Atom(a), Sexp::Atom(b)) => {
|
||||||
|
@ -526,13 +527,14 @@ pub fn matches<'src>(vars: &[Symbol], lhs: &Sexp, expr: &'src Sexp) -> Option<Ma
|
||||||
}
|
}
|
||||||
(Sexp::List(_), Sexp::Atom(_)) => None,
|
(Sexp::List(_), Sexp::Atom(_)) => None,
|
||||||
(Sexp::List(xs), Sexp::List(ys)) => {
|
(Sexp::List(xs), Sexp::List(ys)) => {
|
||||||
if xs.len() == ys.len() {
|
if xs == ys {
|
||||||
|
Some(HashMap::with_capacity(0))
|
||||||
|
} else if xs.len() == ys.len() {
|
||||||
xs.iter()
|
xs.iter()
|
||||||
.zip(ys)
|
.zip(ys)
|
||||||
.map(|(lhs, expr)| matches(vars, lhs, expr))
|
.map(|(lhs, expr)| matches(vars, lhs, expr))
|
||||||
.reduce(|a, b| match (a, b) {
|
.reduce(|a, b| match (a, b) {
|
||||||
(None, _) => None,
|
(None, _) | (_, None) => None,
|
||||||
(_, None) => None,
|
|
||||||
(Some(a), Some(b)) => merge_matches(vars, a, b),
|
(Some(a), Some(b)) => merge_matches(vars, a, b),
|
||||||
})
|
})
|
||||||
.unwrap_or(None)
|
.unwrap_or(None)
|
||||||
|
|
Loading…
Reference in a new issue