RosettaCodeData/Task/Find-common-directory-path/Groovy/find-common-directory-path-1.groovy

9 lines
379 B
Groovy
Raw Permalink Normal View History

2013-04-10 21:29:02 -07:00
def commonPath = { delim, Object[] paths ->
def pathParts = paths.collect { it.split(delim) }
pathParts.transpose().inject([match:true, commonParts:[]]) { aggregator, part ->
aggregator.match = aggregator.match && part.every { it == part [0] }
if (aggregator.match) { aggregator.commonParts << part[0] }
aggregator
}.commonParts.join(delim)
}