Last active
December 10, 2020 18:36
-
-
Save Joxebus/91497574816d553ee2c3acf43cac958f to your computer and use it in GitHub Desktop.
Closure examples - Here you can see some samples about how to use closures in Groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Closure square = { num -> | |
num * num | |
} | |
Closure pow = { num, index -> | |
num.power(index) | |
} | |
List numbers = [2,5,20,10] | |
println square(11) | |
println square.call(22) | |
println numbers.collect(square) | |
println numbers.collect{pow(it,2)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String regex = /http[s]?:\/\/[-A-Za-z0-9.\/_-]*[-A-Za-z0-9._-]/ | |
Closure isUrl = { text -> | |
text ==~ regex | |
} | |
Closure collectUrls = { text -> | |
(text =~ regex).collect() | |
} | |
String sampleText = """Lorem ipsum dolor sit amet, consectetur adipiscing elit http://google.com.mx Integer | |
pharetra elit a elit molestie vehicula. Sed in eros mi. Aliquam massa elit, iaculis eget velit non, | |
malesuada elementum nisl. Cras urna sem, ornare quis turpis id, https://youtube.com facilisis ultrices massa. Duis sit amet | |
pretium ligula. Curabitur in libero vehicula, facilisis diam eget, maximus velit. Vestibulum ut orci at | |
velit vulputate placerat https://github.com Nunc varius a arcu nec lacinia. Vestibulum et magna http://facebook.com et metus | |
venenatis suscipit.""" | |
List urls = collectUrls(sampleText) | |
int urlsCount = sampleText.split(' ').count(isUrl) | |
assert urls.size() == urlsCount | |
println """ | |
URLs count: $urlsCount | |
URLs: $urls | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String downloadFolder = System.getProperty("user.home") + File.separator + "Downloads" | |
URL source = "https://github.com/Joxebus/Joxebus/blob/master/images/joxebus_banner_github.png?raw=true".toURL() | |
File destination = new File(downloadFolder , "banner.png") | |
Closure downloadFile = { url, file -> | |
url.withInputStream {is-> | |
println "start download" | |
file.withOutputStream {os-> | |
def bs = new BufferedOutputStream( os ) | |
bs << is | |
} | |
println "finish downlad the file $file" | |
} | |
} | |
downloadFile(source, destination) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def srcImageRegex = /assets\/images[-A-Za-z0-9.\/_-]*[-A-Za-z0-9._-]/ | |
def webpage = "https://joxebus.github.io" | |
String downloadFolder = System.getProperty("user.home") + File.separator + "Downloads" | |
Closure collectImageAssets = { text -> | |
(text =~ srcImageRegex).collect() | |
} | |
Closure toFullUrl = { path -> | |
webpage.concat('/').concat(path) | |
} | |
Closure downloadFile = { url, fileName -> | |
File destination = new File(downloadFolder , fileName) | |
url.withInputStream {is-> | |
println "start download ${fileName}" | |
destination.withOutputStream {os-> | |
def bs = new BufferedOutputStream( os ) | |
bs << is | |
} | |
println "finish downlad the file $fileName" | |
} | |
} | |
List images = collectImageAssets(webpage.toURL().text).collect(toFullUrl) | |
images.each{ fullUrl -> | |
String fileName = fullUrl.substring(fullUrl.lastIndexOf('/')+1) | |
downloadFile(fullUrl.toURL(), fileName) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment