groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Edinson E. Padrón Urdaneta <edinson.padron.urdan...@gmail.com>
Subject Re: [Question] Variable Argument Lists, Named Arguments, Optional Parameters and Multiple assignment
Date Fri, 14 Aug 2015 19:22:32 GMT
‚ÄčThank you, David. I assume that there's no way to achieve the behavior I
described. The following is a very naive work around I just wrote:

def multiple_assignment(int assignments, List values) {
    assert assignments >= 0

    assignments = Math.min(values.size() - 1, assignments - 1)
    values.take(assignments) << values.takeRight(values.size() -
assignments)
}

def (a, b, c) = [1, 2, 3, 4]
assert a == 1 && b == 2 && c == 3

(a, b, c) = multiple_assignment(1, [1, 2, 3, 4])
assert a == [1, 2, 3, 4] && b == null && c == null

(a, b, c) = multiple_assignment(2, [1, 2, 3, 4])
assert a == 1 && b == [2, 3, 4] && c == null

(a, b, c) = multiple_assignment(3, [1, 2, 3, 4])
assert a == 1 && b == 2 && c == [3, 4]

(a, b, c) = multiple_assignment(4, [1, 2, 3, 4])
assert a == 1 && b == 2 && c == 3

(a, b, c) = multiple_assignment(5, [1, 2, 3, 4])
assert a == 1 && b == 2 && c == 3

Mime
View raw message