fixed in_list bug

This commit is contained in:
Adrian Mariano 2020-07-21 19:55:05 -04:00
parent 1d03a6aa93
commit a73ca6368c
2 changed files with 10 additions and 2 deletions

View file

@ -91,7 +91,11 @@ function slice(arr,st,end) = let(
// in_list("bar", ["foo", "bar", "baz"]); // Returns true. // in_list("bar", ["foo", "bar", "baz"]); // Returns true.
// in_list("bee", ["foo", "bar", "baz"]); // Returns false. // in_list("bee", ["foo", "bar", "baz"]); // Returns false.
// in_list("bar", [[2,"foo"], [4,"bar"], [3,"baz"]], idx=1); // Returns true. // in_list("bar", [[2,"foo"], [4,"bar"], [3,"baz"]], idx=1); // Returns true.
function in_list(x,l,idx=undef) = search([x], l, num_returns_per_match=1, index_col_num=idx) != [[]]; function in_list(val,list,idx=undef) =
let( s = search([val], list, num_returns_per_match=1, index_col_num=idx)[0] )
s==[] ? false
: is_undef(idx) ? val==list[s]
: val==list[s][idx];
// Function: min_index() // Function: min_index()

View file

@ -1,6 +1,5 @@
include <BOSL2/std.scad> include <BOSL2/std.scad>
// List/Array Ops // List/Array Ops
module test_repeat() { module test_repeat() {
@ -16,6 +15,11 @@ module test_in_list() {
assert(in_list("bar", ["foo", "bar", "baz"])); assert(in_list("bar", ["foo", "bar", "baz"]));
assert(!in_list("bee", ["foo", "bar", "baz"])); assert(!in_list("bee", ["foo", "bar", "baz"]));
assert(in_list("bar", [[2,"foo"], [4,"bar"], [3,"baz"]], idx=1)); assert(in_list("bar", [[2,"foo"], [4,"bar"], [3,"baz"]], idx=1));
assert(!in_list(undef, [3,4,5]));
assert(in_list(undef,[3,4,undef,5]));
assert(!in_list(3,[]));
assert(!in_list(3,[4,5,[3]]));
} }
test_in_list(); test_in_list();