Groovy \"List Comprehension\" example


/ Published in: Groovy
Save to your folder(s)



Copy this code and paste it in your HTML
  1. def paramNames = request.getParameterNames().collect { it }
  2. def paramValues = request.getParameterNames().collect { request.getParameter(it) }
  3.  
  4. // -----------------------------------------------
  5. def getEvenNumbers(l) {
  6. l.findAll {it % 2 == 0}
  7. }
  8.  
  9. def getFilesGreaterThan(size, folder) {
  10. new File(folder).listFiles().findAll {it.isFile() && it.length() > size}
  11. }
  12.  
  13. def solveProblem() {
  14. (1..9).each { a->
  15. (0..9).each { b->
  16. (0..9).each { c->
  17. (0..9).each { d->
  18. if ((1000*a + 100*b + 10*c + d)*4 == 1000*d +100*c + 10*b + a) {
  19. println "$a, $b, $c, $d"
  20. }
  21. }
  22. }
  23. }
  24. }
  25. }

URL: https://www.ist-dresden.de/blog/?p=11

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.