开发者

How to create a lookup table in Groovy?

I want to create a lookup table in Groovy, given a size (in this case the size is of 4):

    RGGG
    RRGG
    RRRG
    RRRR

That is in first i开发者_JS百科teration only one R should be there and size-1 times of G should be there. As per the iteration value increases R should grow and G should decrease as well. So for size 4 I will have 4 lookup values.

How one could do this in Groovy?


You mean like this:

def lut( width, a='R', b='G' ) {
  (1..width).collect { n ->
    ( a * n ) + ( b * ( width - n ) )
  }
}

def table = lut( 4 )

table.each { println it }

prints:

RGGG
RRGG
RRRG
RRRR

Your question doesn't really say what sort of data you are expecting out? This code gives a List of Strings

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜