List.metaClass.joinWithDifferentLast { a, b ->
delegate.join(a).reverse().replaceFirst(a.reverse(),b.reverse()).reverse()
}
def a = ['one', 'two', 'three'];
println a.joinWithDifferentLast(',',' and ') //prints one,two and three
assert '' == [].joinWithDifferentLast(' , ', ' and ' )
assert '1' == [ 1 ].joinWithDifferentLast( ', ', ' and ' )
assert '1 and 2' == [ 1, 2 ].joinWithDifferentLast( ', ', ' and ' )
assert '1, 2 and 3' == [ 1, 2, 3 ].joinWithDifferentLast(', ', ' and ' )
assert '1 %ac 2 %ac 3 %ac 4 %ac 5 %bi 6' == [ 1, 2, 3, 4, 5, 6 ].joinWithDifferentLast(' %ac ', ' %bi ' )
私の最初のブルートフォースで素朴な(ただし修正された)推測になります:-D
List.metaClass.joinWithDifferentLast { a, b ->
def l = delegate.size()
delegate[0..l-2].join(a) + b + delegate[l-1]
}
「反転」は少し少ないですが、リストの size() に関するセキュリティ化が必要です