quartata

Chaining

Feb 18th, 2016
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. Tacit Chaining
  2.  
  3. Syntax:
  4.  
  5. initial_args~func1,func2,...->func3,func4,...->...
  6.  
  7. Tacit Chaining is an alternate way of chaining several fixed-arity functions together. Starting out, the initial args on the left side are splatted and appended to a special argument queue. Then, at each stage (delimited with ->), each function in the stage (delimited with commas) takes as many arguments as it needs from the front of the queue. At the end of the stage, the return values of each function are appended to the end of the queue in order, at which point the queue is passed on to the next stage. At the end, the resulting queue is then returned. If the queue contains only one element, that element is returned -- otherwise, the elements are returned as a list. If a non-function variable, literal or expression is found in a stage, it is appended to the queue as if it had been returned by a function.
  8.  
  9. Examples:
  10.  
  11. [1...n]~\product,n->\mod,n-1->\equals
  12.  
  13. A primality test using Wilson's Theorem, assuming n contains the number to be tested. To start, the exclusive range 1...n is wrapped in a list and appended to the argument queue (which now looks like [[1,2,3,...,n-1]]). In the first stage, \product pops [1,2,3,...,n-1] and takes the product of the list (let's call it x). This, along with n, is appended to the queue ([x,n]). In the next stage, \mod takes both x and n and puts the result on the queue, and n - 1 is also added to the queue ([x % n, n-1]). Finally, \equals takes x % n and n - 1 and compares them, putting either 1 or 0 on the queue. Since there is only one element on the queue, it is popped and returned.
  14.  
  15. This is the equivalent of \equals(\mod(\product(1...n),n),n-1).
  16.  
  17. [n*2,n]~\combinations,n+1->\divide
  18.  
  19. This calculates the nth Catalan Number. The list [n*2,n] is appended to the argument queue, then the first stage does \combinations[n*2,n] and appends it and n+1, then the final stage divides the two results.
  20.  
  21. This is the equivalent of \divide(\combinations(n*2,n),n+1).
Advertisement
Add Comment
Please, Sign In to add comment