Link:http://arturoherrero.com/2011/06/04/10-groovy-one-liners-to-impress-your-friends/

I find that comparing programming languages is a worthwhile exercise mainly because of the different techniques and styles that you are exposed to.

After 10 Scala / CoffeeScript / Ruby / Python / Haskell / Clojure / C# / F# one liners to impress your friends, here are the Groovy one liners.

1. Multiple Each Item in a List by 2

(1..10)*.multiply(2)

2. Sum a List of Numbers

(1..1000).sum()

3. Verify if Exists in a String

def wordList = ['groovy', 'akka', 'grails framework', 'spock', 'typesafe']
def tweet = 'This is an example tweet talking about groovy and spock.'
wordList.any { word -> tweet.contains(word) }

4. Read in a File

def fileText = new File('data.txt').text
def fileLines = new File('data.txt').readLines()

5. Happy Birthday to You!

(1..4).each { println 'Happy Birthday ' + ((it == 3) ? 'dear Arturo' : 'to You') }

6. Filter list of numbers

def (passed, failed) = [49, 58, 76, 82, 88, 90].split{ it > 60 }

7. Fetch and Parse an XML web service

def results = new XmlSlurper().parse('http://search.twitter.com/search.atom?&q=groovy')

8. Find minimum (or maximum) in a List

[14, 35, -7, 46, 98].min()
[14, 35, -7, 46, 98].max()

9. Parallel Processing

import groovyx.gpars.*
GParsPool.withPool { def result = dataList.collectParallel { processItem(it) } }

Using Gpars that offers intuitive and safe ways to handle Groovy tasks concurrently.

10. Sieve of Eratosthenes

def t = 2..100
(2..Math.sqrt(t.last())).each { n -> t -= ((2*n)..(t.last())).step(n) }
println t

I found this solution in the comments of this post, Groovy Prime Numbers.

11. Bonus: FizzBuzz

for (i in 1..100) { println "${i%3?'':'Fizz'}${i%5?'':'Buzz'}" ?: i }

[转]Groovy One Liners to Impress Your Friends的更多相关文章

  1. Impress.js上手 - 抛开PPT、制作Web 3D幻灯片放映

    前言: 如果你已经厌倦了使用PPT设置路径.设置时间.设置动画方式来制作动画特效.那么Impress.js将是你一个非常好的选择. 用它制作的PPT将更加直观.效果也是嗷嗷美观的. 当然,如果用它来装 ...

  2. Java——搭建自己的RESTful API服务器(SpringBoot、Groovy)

    这又是一篇JavaWeb相关的博客,内容涉及: SpringBoot:微框架,提供快速构建服务的功能 SpringMVC:Struts的替代者 MyBatis:数据库操作库 Groovy:能与Java ...

  3. 用Groovy构建java脚本

    我是做工作流项目的,工作流中各个模板引擎都需要要执行一个动态业务,这些动态业务有多种实现方式,最常用的就是用户自己写一段脚本文件,然后工作流引擎执行到这里的时候,运行这个脚本文件. 这个运行脚本文件的 ...

  4. Groovy学习--基本语法了解

    x项目用到gradle,学习gradle之前准备先过一遍Groovy的语法.这里参考:Groovy入门. 该博客没有系统的讲解Groovy的语法和原理,仅仅只是罗列了使用Groovy的常规方法.我照着 ...

  5. How to use groovy script on jenkins

    1. Install groovy plugin 2. Add a step of groovy. (normal & systerm) 3. Execute groovy script im ...

  6. Java8-Function使用及Groovy闭包的代码示例

    导航 定位 概述 代码示例 Java-Function Groovy闭包 定位 本文适用于想要了解Java8 Function接口编程及闭包表达式的筒鞋. 概述 在实际开发中,常常遇到使用模板模式的场 ...

  7. Groovy中文教程(链接收藏)

    学习Gradle前,需要有一个Groovy语言的基础,以免被Groovy的语法困扰,反而忽略了Gradle的知识.这里有一个Groovy的简明中文教程文档,可以快速学习Groovy的一些语法:http ...

  8. Groovy入门经典 随书重点

    1 数值和表达式 1.1数值 整数是Integer类的实例 有小数部分的数值是BigDecimal类的实例 不同于java,没有基础数据类型 一切皆对象的概念重于java 1.2表达式 两个整数的除法 ...

  9. Groovy解析xml并且注入Project,TestSuite,TestCase级别的custom properties

    import com.eviware.soapui.support.GroovyUtils import groovy.util.XmlParser def groovyUtils = new Gro ...

随机推荐

  1. javascipt——基础知识——基本数据类型和逻辑运算

    进制转换:http://www.topthink.com/topic/504.html javascript的数据类型分为基本数据类型和非基本数据类型(对象) 一.基本数据类型: 包括以下部分:数字. ...

  2. http协议基础教程

    引言 HTTP 是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和 扩展.目前在WWW中使用的是HTTP/ ...

  3. 一些API的用法

    //1.init初始化 NSString * str1 = [[NSString alloc] init]; NSLog(@"str1 = %@",str1); //2.initW ...

  4. 修改 linux 时区时间和 php 时区

    问题:客户美国服务器时间不对第一步,先修改硬件时区. vim /etc/sysconfig/clock将 ZONE="America/New_York" 注释,加多一行 ZONE= ...

  5. day70-oracle PLSQL_02光标

    涨工资之前员工的工资. 如果PLSQL程序没有commit的话,命令行这边的客户端是无法读到的.这是oracle数据库的隔离级别. 为什么在PLSQL程序中commit之后还是不行呢? PLSQL程序 ...

  6. php 关于锁的一些看法

    背景:在一个项目中,需要一次对数据很复杂的计算,其中一次计算需要花费大概30秒钟时间,大概需要查询一个比较大的表300次左右,然后还需要进行查询7-8次数据库,然后进行组合排序等功能,完成最终结果.对 ...

  7. cocos2dx中替代goto的用法:do{}while(0)和CC_BREAK_IF

    我们时常会调用某个函数来创建一个对象,但由于内存不足或其他异常情况发生时对象可能会创建失败,创建失败我们就要结束当前程序转到错误处理地方去处理错误或释放已生成的对象. int* p1 = new in ...

  8. iOS 地图(添加大头针)

    首先在工程中导入MapKit.framework库文件 #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <U ...

  9. 【摘自张宴的"实战:Nginx"】try_files指令

    语法:try_files param1 [param2...paramN] fallback 默认值:none 使用环境: location 该指令用于告诉nginx测试每个文件是否存在,并且使用首先 ...

  10. Centos7安装mysql缺乏yum源怎么安装

    找到mysql5.6的centos的repo源,终于解决mysql的安装问题: 1.确保centos安装了wget,没有的话安装wget   1 yum install wget 2.下载mysql的 ...