Conor O'Brien
Conor O'Brien

Reputation: 1027

Making a range verb in J

J is a fantastic programming language that I have decided to learn. As a simple exercise, I decided to emulate a range. There is a builtin function i. that creates a range 0..i-1. Using some math, I have that this is the range between a and b:

 a + i. (b - a - 1)

Success! I thought. Now, the "simple" task of converting to a verb is set before me. This is my problem now. I have a (the left argument) being called on both sides of the verb +. I thought of using evoke, but I am not sure as how to make it work.

So my question stands: How do I convert an expression of the form a f (a g b) or, more specifically, a f (a g b h c) (and similar forms) to a pure verb? I don't want to use explicit arguments, for what's the fun in that? ;)

EDIT My solution is as thus:

range =: [(+i.)>:@-~

Upvotes: 4

Views: 166

Answers (1)

Eelvex
Eelvex

Reputation: 9143

To convert an expression of the form x f (x g y) to a form of x h y you can use a dyadic fork:

x (F G H) y = (x F y) G (x H y)

and the identity verb: x [ y = x, making:

x ([ f g) y  = (x [ y) f (x g y) = x f (x g y)

So the verb you are looking for is h =: [ f g

Upvotes: 5

Related Questions