Fabulous ! Operator

From ZCubes Wiki
Jump to navigation Jump to search

Fabulous ! Operator

! is called the Fabulous Operator in z^3 languate. In the basic form, it is meant to compute the fantastic factorial function. However, based on the arguments provided, it can simply create powerful functions with the simple notation of ! to a string, regular expression, etc. This dramatically simplifies notation to create functions, and makes for powerful and fabulously expressive code.

! Operator can have multiple meanings, when used as a suffix. of course, as a prefix, ! represents NOT as in most C like languages.
! can be used to (1) Compute factorial of numbers (including complex numbers etc), (2) Create functions out of strings, (3) Create predicate functions and search functions out of simple notations with regular expressions, strings, etc.

Converting A String To A Function

A simple string can be converted to a function with ! operator, and parameters are detected automatically using z^3 rules.


	"x^2"!
	("x^2"!)(3)
		// could make ! binary with similar behavior too.
	1..100.$("x^2"!)
	1..100.$("SIN(x)"!)
	("SIN(x)"!)(3,4)
	
	s="x^5";
	g=(s!);
	g(34);

	s="x^5+y^4";
	g=(s!);
	g(1,3);

	("x^5+y^4+z^5"!)(2,3,4);
	
	MS(12).$$("SUM(x)"!)
	MS(1..12).$$("SUM(x)"!)
	
	a="x^4";
	foured=(a!)#;
	foured(1..34)

	(["x^3","x^4"]!)@1..3
	
	// did not work
	("x^"|.+|1..10)!@(1..10)
	//this did not parse
	("x^"|x+y|1..10)!@1..10
	// also did not parse
	("x+"|x+y|1..10)!@1..10

Simple predicate functions (functions that do simple boolean true/false checks) can be created with shortcut notations such as below:

	
	"<1"!

	
	("x<3"!)(2)
	("x<3&&y>4"!)(2,5)
	"<'absc'"!
	"<=3"!
	">=3"!
	"!=3"!
	("x<3&&x>4"!)+""
	("SIN(x)"!)(35deg)
	"==(44)"!
	"==(44!)"!
	
	f1="FEQ(44!,x)"!;
	f1(43!)
	

The following shows the use of these simple function creations, with generated arrays to create a series of functions. For example, ((1..10|.+|"x")!) creates 10 functions from x+1, x+2, etc. until x+10. Below is a case where these array series of functions are applied to an array to the left of @ operator.

	

	..5@((1..10|.+|"x+")!)
	..50@((1..10|.+|"x^")!)
	1..100.F(">50"!)  // .F stands for .filter
	1..100.F("x>5&&x<25"!)


See Also

The Powerful # Hash Operator