cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar

Add function which can be universally used to check if object/variable is empty/missing

What inspired this wish list request? 

JMP has multiple different ways to indicate if values are empty, missing or null and functions/operations might return different types when they have no values when compared to when they do have (and in Python the "pythonic" way would be just to check value in If-statement as the objects will be false Built-in Types/Truth Value Testing).

 

What is the improvement you would like to see?

More universal JSL function to check if value is empty/missing/null/has no value. Below is one example for the function (hopefully others have additional ideas):

Names Default To Here(1);

Is None = function({obj, keep_zero = 1}, {Default Local},
	/*"""Attempt to check if object is None (Empty(), ., 0, "", {}, [], [=>], [](0,1)...
	
	Args:
		obj: object to check
	
	Returns:
		int: 0 if non-zero and 1 if zero
		
	Examples:
		>> show(Is None([1 => 2]));
			Is None([1 => 2]) = 0;
		>> show(Is None(Associative Array()));
			Is None(Associative Array()) = 1;
	"""*/
	cur_type = Type(obj);
	return(
		If(Contains({"List", "Matrix", "Associative Array"}, cur_type), // check items in list, matrix and associative array
			!N Items(obj);
		, cur_type == "String", 
			!Length(obj); // check string length, Is Missing() should also work
		, Contains({"Integer", "Number"}, cur_type),
			If(IsMissing(obj),
				1
			, keep_zero & obj == 0,
				1
			,
				0
			);
		, cur_type == "Empty",
			1;
		,
			0;
		);
	);	
);


Print("Zeros:");
show(Is None([1 => 2]));
show(Is None({"a"}));
show(Is None([1]));
show(Is None(1)); 
show(Is None("a"));
Write("\!N");
Print("Ones:");
show(Is None(Associative Array()));
show(Is None({}));
show(Is None([]));
show(Is None(0));
show(Is None(""));
show(Is None(.));
show(Is None(Empty()));

 

Why is this idea important?

Makes it easier to build comparisons without guessing which check should be implemented this time (IsMissing(), Is Empty(), N Items() > 0, Zero Or Missing()...)

1 Comment
Craige_Hales
Super User