This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> val v:Long = 3912657840L | |
v: Long = 3912657840 | |
scala> (1 to 9).map { v % _ } | |
res1: scala.collection.immutable.IndexedSeq[Long] = Vector(0, 0, 0, 0, 0, 0, 0, | |
0, 0) | |
scala> val sv = "3912657840" | |
sv: java.lang.String = 3912657840 | |
scala> sv.sliding(2).map { _.toInt }.map { v % _ }.toList | |
res2: List[Long] = List(0, 0, 0, 0, 0, 0, 0, 0, 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> def rotate[T](l: List[T], dist: Int) = { | |
| val _dist = (dist + (dist.abs / l.size + 1) * l.size) % l.size | |
| l.drop(_dist) ++ l.take(_dist) | |
| } | |
rotate: [T](l: List[T], dist: Int)List[T] | |
scala> rotate(List(1,2,3,4,5),0) | |
res13: List[Int] = List(1, 2, 3, 4, 5) | |
scala> rotate(List(1,2,3,4,5),1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> def hashMap[K,V](e:java.util.Map.Entry[K,V]*)={ | |
| e.foldLeft(new java.util.HashMap[K,V]()) { (map, e) => map.put(e.getKey, e.getValue); map } | |
| } | |
hashMap: [K, V](e: java.util.Map.Entry[K,V]*)java.util.HashMap[K,V] | |
scala> def $[K,V](k: K, v: V) = { new java.util.AbstractMap.SimpleEntry(k, v) } | |
$: [K, V](k: K, v: V)java.util.AbstractMap.SimpleEntry[K,V] | |
scala> hashMap($('a, 1), $('b, 2)) | |
res1: java.util.HashMap[Symbol,Int] = {'a=1, 'b=2} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovyx.gpars.GParsPool | |
GParsPool.withPool { | |
def array = ["+", "-", "*", "/"] | |
def combi = GroovyCollections.combinations((1..2).collectParallel{ array }) // arrayから2つ選びだす順列 | |
println combi //=> [[1, 1], [2, 1], [3, 1], [1, 2], [2, 2], [3, 2], [1, 3], [2, 3], [3, 3]] | |
combi.each { | |
def s = "1" | |
def i = 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
public class A { | |
protected int i; // ← コンストラクタ起動時にこいつにアクセスすると IllegalAccessError | |
public A(int i) { | |
this.i = i; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
public class A { | |
protected int i; // ← コンストラクタ起動時にこいつにアクセスすると IllegalAccessError | |
public A(int i) { | |
this.i = i; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FizzBuzz { | |
public static void main(String[] args) { | |
for (int i = 1; i <= 100; i++) { | |
if (!new Buzz(new Fizz(i)).apply()) { | |
System.out.print(i); | |
} | |
System.out.println(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FizzBuzz { | |
public static void main(String[] args) { | |
for (int i = 1; i <= 100; i++) { | |
if (i % 3 == 0) { | |
System.out.print("Fizz"); | |
} | |
if (i % 5 == 0) { | |
System.out.print("Buzz"); | |
} | |
if (i % 3 != 0 && i % 5 != 0) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package p | |
class RichString(str: String) { | |
def test = { | |
m(str) | |
} | |
} | |
// 追記 | |
// RichString をどこか別のパッケージ(たとえば p2)にして |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g100pon #18 単位換算 | |
// JScience を使用します - http://jscience.org/ | |
// 注: 次期バージョン(5.0)では配布方法が変わるため、以下の Grab 指定が変わる可能性があります。 | |
// また、本バージョン(4.x)も、公式には Maven リポジトリとして配布されていません。 | |
// 現在は以下に示すサイトで暫定的に取得できます。 | |
//@GrabResolver(name = 'jcurl', root = 'http://www.jcurl.org/m2/repo/') | |
//@GrabResolver(name = 'obiba', root = 'http://maven.obiba.org/maven2/') | |
@Grab('org.jscience:jscience:4.3.1') | |
import javax.measure.DecimalMeasure |