开发者

How to create a lookup table in Groovy?

开发者 https://www.devze.com 2023-04-13 00:02 出处:网络
I want to create a lookup table in Groovy, given a size (in this case the size is of 4): RGGG RRGG RRRG RRRR

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

精彩评论

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

关注公众号