HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Summative Pattern

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
patternsummativestackoverflow

Problem

While playing around with J (link), I found that you can create a sequence of shape N0 N1 ... NK with i. taking a list as its parameters. Since i. creates a range 0 .. RHS and >: increments each value in a range, i. >: i. RHS creates a non-empty range. We can use , to flatten it and +/ to summate it. Given an RHS, this looks like:

+/ , i. >: i. RHS


One can trivially convert this to a tacit expression using an extended monadic fork and capping the left branch [:.

[: +/ [: , [: i. [: >: i.


However, I find this rather bulky and that it could be more concise. How might I make it more so?

I have found a more concise solution that I would also like feedback on. Observing that the last members in each of the lists (before being summated) is (n-1)!. Since , flattens the list, we can emulate this with i., and since the endpoint is not included, we have another solution:

+/ i. ! RHS


Converting this to a tacit expression in the same way as above yields:

[: +/ [: i. !


However, I do not know of a better method for converting a monadic sequence of operations to a tacit verb.

Moving some verbs into variables:

sum =: +/
cap =: [:
cap sum cap i. !


How can this code be improved?

Some example input/outputs (work for both functions).

sum =: +/
   cap =: [:
   sp =: cap sum cap i. !
   max =: 8
   (2 , max) $ (i.max) , (sp"0 i.max)
0 1 2  3   4    5      6        7
0 0 1 15 276 7140 258840 12698280


The first row is input to sp, and the second row is its output.

Solution

You're likely aware of this already, but just for posterity it's worth noting that you can always create an anonymous verb:

(3 :'+/ , i. >: i.y') 6


The extra 5 (or 7, if you count the parens, which are often optional) characters you spend pay for themselves after about 3 fork caps...

Code Snippets

(3 :'+/ , i. >: i.y') 6

Context

StackExchange Code Review Q#126454, answer score: 3

Revisions (0)

No revisions yet.