[转]Groovy One Liners to Impress Your Friends
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的更多相关文章
- Impress.js上手 - 抛开PPT、制作Web 3D幻灯片放映
前言: 如果你已经厌倦了使用PPT设置路径.设置时间.设置动画方式来制作动画特效.那么Impress.js将是你一个非常好的选择. 用它制作的PPT将更加直观.效果也是嗷嗷美观的. 当然,如果用它来装 ...
- Java——搭建自己的RESTful API服务器(SpringBoot、Groovy)
这又是一篇JavaWeb相关的博客,内容涉及: SpringBoot:微框架,提供快速构建服务的功能 SpringMVC:Struts的替代者 MyBatis:数据库操作库 Groovy:能与Java ...
- 用Groovy构建java脚本
我是做工作流项目的,工作流中各个模板引擎都需要要执行一个动态业务,这些动态业务有多种实现方式,最常用的就是用户自己写一段脚本文件,然后工作流引擎执行到这里的时候,运行这个脚本文件. 这个运行脚本文件的 ...
- Groovy学习--基本语法了解
x项目用到gradle,学习gradle之前准备先过一遍Groovy的语法.这里参考:Groovy入门. 该博客没有系统的讲解Groovy的语法和原理,仅仅只是罗列了使用Groovy的常规方法.我照着 ...
- How to use groovy script on jenkins
1. Install groovy plugin 2. Add a step of groovy. (normal & systerm) 3. Execute groovy script im ...
- Java8-Function使用及Groovy闭包的代码示例
导航 定位 概述 代码示例 Java-Function Groovy闭包 定位 本文适用于想要了解Java8 Function接口编程及闭包表达式的筒鞋. 概述 在实际开发中,常常遇到使用模板模式的场 ...
- Groovy中文教程(链接收藏)
学习Gradle前,需要有一个Groovy语言的基础,以免被Groovy的语法困扰,反而忽略了Gradle的知识.这里有一个Groovy的简明中文教程文档,可以快速学习Groovy的一些语法:http ...
- Groovy入门经典 随书重点
1 数值和表达式 1.1数值 整数是Integer类的实例 有小数部分的数值是BigDecimal类的实例 不同于java,没有基础数据类型 一切皆对象的概念重于java 1.2表达式 两个整数的除法 ...
- Groovy解析xml并且注入Project,TestSuite,TestCase级别的custom properties
import com.eviware.soapui.support.GroovyUtils import groovy.util.XmlParser def groovyUtils = new Gro ...
随机推荐
- hadoop job -kill 与 yarn application -kii(作业卡了或作业重复提交或MapReduce任务运行到running job卡住)
问题详情 解决办法 [hadoop@master ~]$ hadoop job -kill job_1493782088693_0001 DEPRECATED: Use of this script ...
- eclipse怎么查看class文件(eclipse安装反编译插件)
本人eclipse版本: Eclipse Java EE IDE for Web Developers. Version: Mars.2 Release (4.5.2) 步骤1:下载两个我们需要的东西 ...
- js面试题知识点全解(一作用域)
问题: 1.说一下对变量提升的理解 2.说明this几种不同的使用场景 3.如何理解作用域 4.实际开发中闭包的应用 知识点: js没有块级作用域只有函数和全局作用域,如下代码: if(true){ ...
- Codeforces #505(div1+div2) C Plasticine zebra
题意:给你一段字符串,可以选择任意多的位置,每个位置会反转两边的字符串,问交错的字符串最长是多长? 思路:找规律,仔细分析样例1.假设位置为 1 2 3 4 5 6 7 8 9,反转之后会发现答案是7 ...
- 高并发压力测试工具Locust(蝗虫)
What is Locust? Locust is an easy-to-use, distributed, user load testing tool. It is intended for lo ...
- opencv 基本数据结构
转自:http://www.cnblogs.com/guoqiaojin/p/3176692.html opencv 基本数据结构 DataType : 将C++数据类型转换为对应的opencv数 ...
- HiveServer2的配置使用
HiveServer2的配置和使用 hive-site.xml配置 hiveserver2的配置 <property> <name>hive.support.concurren ...
- 数字图像处理实验(1):PROJECT 02-01, Image Printing Program Based on Halftoning 标签: 图像处理MATLAB 2017-04-2
实验要求: Image Printing Program Based on Halftoning Objective: To know in principle what is "halft ...
- hdu6331 Problem M. Walking Plan
传送门 题目大意 给你一个n点m条边的有向图,q次询问,给定s,t,k,求由s到t至少经过k条边的最短路. 分析 我们设dp[i][j][k]为从i到j至少经过k条边的最短路,sp[i][j]意为从i ...
- hdu5521 Meeting
传送门 题目 Bessie and her friend Elsie decide to have a meeting. However, after Farmer John decorated hi ...