ä»æ¥ã¯11æ¥ã§ãã
11ã¯ç´ æ°ã ããä»æ¥ã¯ç´ æµãªæ¥ã§ãã
Javaã§ãã£ã¦ã¿ã
public class Main { static int value = 11; public static void main(String[] args) { for (int i = 2; i < value; i++) { if (value % i == 0) { System.out.println(false); return; } } System.out.println(true); } }
ï¼ããã¡ããé¢åã ã¨æã£ãã®ã«â¦ï¼
Groovyã§ãã£ã¦ã¿ã
ããããã®ã¯Javaã§ããã¨é¢åãªã®ã§ãGroovyï¼åå¼·ä¸ï¼ã§ãã£ã¦ã¿ããã
def value = 11 println value <= 2 || (2..value-1).every { value % it != 0 }
â¦ãªãããã£ã¡ããããã
1è¡ã«ãã¦ã¿ã
ç¡çç¢ç1è¡ã§æ¸ãã¦ããã¾ãå¬ãããªãã
def value = 11;println value <= 2 || (2..value-1).every{value % it != 0}
Object (Groovy JDK)çºãã¦ãã with ã£ã¦ã®ãããã£ã½ããã㨠..< 使ã£ã¦ã0ã¯falseã ããçç¥åºæ¥ãã
println 11.with{i -> (2..<i).every{i % it}}
ãããªããããªï¼
æè§ãªã®ã§ã³ãã³ãã©ã¤ã³ããå®è¡
PS> groovy -e "println 11.with{i -> (2..<i).every{i % it}}"
æå®ããããã¡ã¤ã«ãè¦ã¤ããã¾ããã
æå®ããããã¡ã¤ã«ãè¦ã¤ããã¾ããã
ã³ãã³ãã®æ§æã誤ã£ã¦ãã¾ãã
â¦ãªãã ãããï¼ãã¡ã¤ã«ã«ä¿åãããå®è¡ã§ãããã©ãããããå¬ãããªãããã¼ãã
ãã¿æ¸ãã«ãã³ã¯ã«ã¤ã®ã§ãã©ã¡ã¼ã¿ãåããããã«ãã
println ((args[0] as int).with{i -> (2..<i).every{i % it}})
ããã£ãã¨ã
ä¸çªå¤å´ã®ã«ãã³ãä»ããªãã¨åããªããã§ãããã¨ã©ã¼ãã¤ãã¤ãç解ã§ããªãã¦ãã°ããé¦æ»ã£ã¦ã¾ãããprintln(args[0] as int) ã®æ»ã(voidã ããnullã£ã½ãï¼)ã« with å¼ãã§ããããå
¥ã£ã¦ãã i 㨠2 ãæ¯è¼ãããã¨ããã¨ããã§ã³ã±ãæãã§ãã
ãã®é¨åã ãåãåºãã¦groovyshã§å®è¡ããã¨ãããªæãã
groovy:000> null.with{it <= 2} ERROR groovy.lang.GroovyRuntimeException: Cannot compare org.codehaus.groovy.runtime.NullObject with value 'null' and java.lang.Integer with value '2' at groovysh_evaluate$_run_closure1.doCall (groovysh_evaluate:2) at groovysh_evaluate.run (groovysh_evaluate:2)
NullPointerExceptionã«ã¯ãªããªããã§ããã