Skip to content

Commit d03feb8

Browse files
committed
Support Lookup threading over a list of associations
Lookup[{<|a->1|>, <|a->3|>}, a] now correctly returns {1, 3}. Previously it errored because a list of associations was not recognized as a valid first argument.
1 parent 5c6b146 commit d03feb8

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

src/functions/association_ast.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,18 @@ pub fn lookup_ast(args: &[Expr]) -> Result<Expr, InterpreterError> {
206206
args: vec![Expr::String("KeyAbsent".to_string()), args[1].clone()],
207207
})
208208
}
209+
Expr::List(items) => {
210+
// Thread Lookup over a list of associations
211+
let results: Result<Vec<Expr>, InterpreterError> = items
212+
.iter()
213+
.map(|item| {
214+
let mut new_args = vec![item.clone()];
215+
new_args.extend(args[1..].iter().cloned());
216+
lookup_ast(&new_args)
217+
})
218+
.collect();
219+
Ok(Expr::List(results?))
220+
}
209221
_ => Err(InterpreterError::EvaluationError(
210222
"Lookup expects an association as first argument".into(),
211223
)),

tests/interpreter_tests/association.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,3 +964,23 @@ mod association_thread_rule_form {
964964
);
965965
}
966966
}
967+
968+
mod lookup {
969+
use super::*;
970+
971+
#[test]
972+
fn lookup_threads_over_list_of_associations() {
973+
assert_eq!(
974+
interpret("Lookup[{<|a -> 1, b -> 2|>, <|a -> 3, c -> 4|>}, a]").unwrap(),
975+
"{1, 3}"
976+
);
977+
}
978+
979+
#[test]
980+
fn lookup_threads_with_default() {
981+
assert_eq!(
982+
interpret(r#"Lookup[{<|a -> 1|>, <|b -> 2|>}, a, "miss"]"#).unwrap(),
983+
"{1, miss}"
984+
);
985+
}
986+
}

0 commit comments

Comments
 (0)