首页 > 其他分享 >打动你朋友的11条Groovy超炫代码

打动你朋友的11条Groovy超炫代码

时间:2023-08-10 21:38:32浏览次数:31  
标签:11 Groovy 超炫 .. 10 tweet println new def

Dustin Marx在其博文中,跟读者分享了11条Groovy的超炫代码。


List中的每个元素乘2:

1	(1..10)*.multiply(2)
List求和:
1	//元素均为为数字
2	(1..1000).sum()
3	//元素含有字符
4	['a',3,'z'].sum()  //结果为字符串‘a3z’

List中是否含有某个字符串

1	def wordList = ['groovy', 'akka', 'grails framework', 'spock','typesafe']
2	def tweet = 'This is an example tweet talking about groovy and spock.'
3	wordList.any { word -> tweet.contains(word) }
01	//该方法同样适用于对象
02	class Person{
03     String name
04	}
05	def person1=new Person(name:'person1')
06	def person2=new Person(name:'person2')
07	def person3=new Person(name:'person3')
08	def wordList = [person1,person2]
09	def tweet = [person3]
10	wordList.any { it -> tweet.contains(it) }

上述代码结果为false,如果tweet = [person3,person1],结果就为true


文件内容读取,易如反掌:

1	//读取所有内容
2	new File("data.txt").text
3	//按行读取,返回List
4	new File("data.txt").readLines()

生日快乐!

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

按条件拆分List

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

获取和解析XML Web服务

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

找出List中最大最小值:

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

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

使用 GPars提供的直观、安全的方式控制Groovy的并行任务

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

找质数算法(Sieve of Eratosthenes筛法)

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

这个方法来自于Groovy Prime Numbers的评论。


有奖问答:FizzBuzz问题 - 打印1到100这些数字,遇到数字为3的倍数的时候,打印“Fizz”替代数字,5的倍数用“Buzz”代替,既是3的倍数又是5的倍数打印“FizzBuzz”。

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

另附两个有趣问题的解答:


现在手头有0.5美元、0.25美元、10美分、5美分、1美分,将1美元换成这些零钱,有多少种换法:


01	def count=0
02	101.times{ x1 -> 21.times{
03     x2 -> 11.times{
04         x3 -> 5.times{
05             x4 -> 3.times{
06                 x5 -> if(x1*1+x2*5+x3*10+x4*25+x5*50 == 100){
07                          count++
08                          println "$x1*1+$x2*5+$x3*10+$x4*25+$x5*50 == 100"
09                     }
10                 }
11             }
12         }
13     }
14	}
15	println count

汉诺塔问题:

01	def hanoita(n, a, b, c){
02     if(n==1){
03         println "$n : $a -> $c"
04     }else{
05         hanoita n-1, a, c, b
06         println "$n : $a -> $c"
07         hanoita n-1, b, a, c
08     }
09	}
10	hanoita 5, 'a', 'b', 'c'

奇妙吧?就是这么简单!对于上述代码,如果你有更好的提议,也可以分享给大家。


标签:11,Groovy,超炫,..,10,tweet,println,new,def
From: https://blog.51cto.com/u_6174294/7039867

相关文章

  • Windows 11下安装金蝶云星空超详细图文说明(K3 Cloud 8.1企业版)
    安装目录1、官网下载安装包并安装2、创建管理中心3、创建业务中心4、下载补丁包5、执行特定版本补丁包6、下载bos  金蝶云社区使用指南https://vip.kingdee.com/article/69145827754457344?channel_level=kdclub&utm_campaign=%E7%94%A8%E6%88%B7%E5%BC%95%E5%AF%BC&u......
  • 通过注册表关闭Windows 11的LE Audio功能 How to Disable LE Audio on Windows 11
    LEAudio是蓝牙音频的下一代技术,具有以下主要特点和优势:低能耗:LEAudio使用更高效的编解码器和低能量的蓝牙技术,从而降低了功耗,使设备的电池寿命更长。多设备连接:LEAudio支持多设备之间的同步连接,允许用户将多个蓝牙设备连接到同一音频源。广播音频:通过Auracast™广播音......
  • 8.11总结
    今天学习了堆排序模板inth[N],sz;//h[N]是有一维数组建立之后的堆voiddown(intu){ intt=u; if(u*2<=sz&&h[t]>h[u*2])t=u*2; if(u*2+1<=sz&&h[t]>h[u*2+1])t=u*2+1; if(u!=t) { swap(h[t],h[u]); down(t......
  • 字符设备驱动-11.mmap机制-实例分析
    1mmap驱动要做的事情确定物理地址确定属性:是否使用cache、buffer建立映射关系参考Linux驱动源文件代码:我们要验证mmap功能,在驱动程序中申请一个8K的buffer,让APP通过mmap能直接访问。2mmap驱动代码示例分析linux内核中常用的内存申请方式:函数名说明......
  • 火山引擎开发者社区一周资讯精选(8.5-8.11)
    ......
  • 外汇110提醒大家,入金前先查平台
    FX110网一直提醒大家,入金前先查平台,不要被平台方的一面之词蒙蔽了双眼,否则一不小心掉坑黑平台,要想把钱拿回来可就难了!近日,就有汇友向我站爆料,称其在CCFMarkets 平台投资梦变成了噩梦。在CCFMarkets投资梦变噩梦据汇友描述,他之所以选择在CCFMarkets平台投资,是因为平台承诺了超额......
  • Windows11安装python模块transformers报错Long Path处理
    Windows11安装python模块transformers报错,报错信息如下ERROR:CouldnotinstallpackagesduetoanOSError:[Errno2]Nosuchfileordirectory:'C:\\Users\\27467\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCac......
  • Microsoft SQL Server 2012 Updates / RTM (11.00.2100) / SP1 (11.0.3000.0 or 11.1.
    SQLSERVER2012SP4UpdateVersion:MSSQL2012SP4SECURITYUPDATE,Build:11.0.7507.2/11.4.7507.2,KB:KB4583465,ReleaseDate:January2021,Download:https://support.microsoft.com/en-us/topic/kb4583465-description-of-the-security-update-for-sql-serve......
  • 解决Avalonia 11.X版本的中文字体问题
    网上搜索的方法使用接口“IFontManagerImpl”这个方法目前只能用于Avalonia10.X版本,因为11版本后官方把这个接口的成员都设置成了非plubic,所以之前的版本解决办法用不上了,经过搜索github的官方那边的问题集锦,要解决解决Avalonia11.X版本的中文字体问题有2个思路:1.在程序里面嵌......
  • P9511 『STA - R3』大豆 题解--zhengjun
    妙妙题。题意给定\(F_0(x)=a_{(x-1)\bmodn+1}\)。\[F_k(x)=F_{k-1}(x)-\sum\limits_{i=2}^nF_k(\lfloor\frac{n}{i}\rfloor)\]求\(F_k(m)\)。\(1\len\le10^4,1\lem\le10^{10},1\lek\le3\)。直接数论分块求解的复杂度是\(O(m^{\frac{3}{4}}k)\),难以通过。如果像......