Combinators

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.

[SUM,RECIPROCAL].$combinators("B")(1..10)

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

// https://thecodest.co/blog/power-of-functional-programming-in-javascript-part-2-combinators/
// combinators
// function trains

//eval([COS,SIN,TAN,SUM].$fnx())(45)
[COS,SIN,TAN,SUM].$fnx()
[COS,SIN,TAN,SUM].$fnx()(45)
[COS,SIN,TAN,SUM].$atop()(45)

[COS,SIN].$atop()


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

[[SIN,TAN],SUM].$fnx()(22..45) 
([[SIN,TAN],SUM].$fnx())+""

([[SIN,TAN],SUM].$fnx())(45..90)

([[[SIN],[TAN]],SUM].$fnx())(45..90)
([[[SIN],[TAN]],SUM].$fnx())(49,39)
([[[SIN],[TAN]],SUM].$fnx())(49,34)

([[[SIN],TAN],SUM].$fnx())(49,34)
([[[SIN],TAN],SUM].$fnx())+""
([[[SIN],TAN,[COS]],SUM].$fnx())+""
([[[SIN],[TAN],[COS]],SUM].$fnx())+""
([[SIN,TAN,[COS]],SUM].$fnx())+""

[DIVIDE,SUM,Length].$fork()

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

$Fork([DIVIDE,Length,SUM])([2,3,4,5])

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

[SIN,COS,TAN].$sequence()(1..10)
	// 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.
	
[SIN,COS,TAN].$pipe()(1..10)
[SIN,COS,TAN].$compose()(1..10)	

[SIN,COS,TAN].$tap()(1..10)	
	// 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,TAN].$tap()(10)

[x=>x>4?1,x=>x>8?2,x=>x>14?3].$alternation()(10)

[x=>x>4?1,x=>x>8?2,x=>x>14?3].$alternation()(10)
 
[x=>x>4?1,x=>x>8?2,x=>x>14?3].$alternation()+""
 
[x=>x<4?1,x=>x<8?2,x=>x<14?3].$alternation()(10)


[].$combinators()



[SIN,COS].$combinators("B")(3)
[SIN,COS].$fnx("B")(3)


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


[DIVIDE,SUM,Length].$combinators("Φ")(1..10)

// errors on this one because the com.
[DIVIDE,SUM].$combinators("Σ")(1..10)

// not working. as a is not generating a function.
//[SIN,COS].$combinators("Σ")(1..10)

[SIN,COS].$fnx("⊣⊢")(1..10)

[].$combinators() 
	//add by default

window['λ⊣⊢']
 //can still work	
 
 
$combinators([SIN,COS],"B")(1..20)

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