From ZCubes Wiki
Jump to navigation Jump to search

Combinators in z^3

Combinators are an advanced concept. But z^3 makes it simple.

For more details, please go through Combinators Theory.

Functional Languages often use patterns to combine function. These are called Combinators. For example, few of these patterns are given in Functional programming in JavaScript Part 2 - Combinators (such as Tap, Currying, Pipe/Compose, Fork, Alternation, Sequence, etc.).

In z^3, Combinators are accessible as a part of an array of functions, as well as in the Global Scope.

Take for example the B combinator, which is defined as B = a => b => c => a(b(c)), and takes functions and apply them one atop the other.

In z^3, [SUM,RECIPROCAL].$combinators("B") gives a function that can be called with arguments.


gives 2.9289682539682538 (which is SUM(RECIPROCAL(1..10))).

From the Global Scope, the call could be λB([SUM,RECIPROCAL])(1..10) . All combinators are named such as λCOMBINATOR with a λ prefix at the global scope. The parameters are an array of functions, and the resulting function can be used later with parameters to provide results.

Code Keyboard Helper contains a panel to aid the use of combinators.

ZCubes Code Mode Combinator Quick Panel

This is WIP. Below are some of the code to try. The Quick Panel shows all the combinators that can be tried in z^3.

Array Combinators

z^3 Array Combinators can be used to represent and complex programming logic, along with the powerful Array Member Function called $fnx, which can take a complex array structure and convert it into a full fledged programming logic, fully availing the power of Combinators.

$fnx is the core function that is utilized by other member functions such as $combinators, $atop, $tap, $pipe, $compose, $sequence, $fork, $alternation, etc. All popular combinators are named with the letters as well as bird names, and in several cases APL Glyphs. The use of functions stored in Arrays being invoked into patterns represented by combinators is an interesting twist that could change the way we think about programs in the future.

For advanced users, this approach suggest as a technique to generate effectively curried functions, that are partially evaluated and held for future use.

Notes and Code to Try

// combinators
// function trains



[[SIN,TAN],SUM].$atop()(22..45) // more like the way to operate






[DIVIDE,SUM,Length].$fork()([5, 3, 2, 8, 4, 2])


[DIVIDE,SUM,Length].$fork()([5, 3, 2, 8, 4, 2])

	// does not have to return values. just need to run them in sequence, no array result expected.
	// if needed, could have a wrap functionality to fnx with an array member doing that.

	// done: with itself call in parallel.
	// (function(a){return(ITSELF(TAN(COS(SIN(a)))))})
	// how to reflect ITSELF a in this. Maybe another indicator to capture and reflect?
	// and how do we wrap a set into an array?






[SIN,COS].$combinators("K")(3) // gives SIN as it ignore COS etc. constant


// errors on this one because the com.

// not working. as a is not generating a function.


	//add by default

 //can still work	

[DIVIDE,SUM,Length].$combinators("B")(1..10) // gives error