Skip to content

Instantly share code, notes, and snippets.

View nobeans's full-sized avatar

Yasuharu Nakano nobeans

  • Yokohama, Japan
View GitHub Profile
@nobeans
nobeans / overflow.groovy
Created March 31, 2018 05:12
intがoverflowする時にソート順序が入れ替わる
int i = 2147483647
def l = [a:i-1, b:i, c:i+1]
assert l == [a:2147483646, b:2147483647, c:-2147483648]
assert l.sort { it.value }*.key == ['c', 'a', 'b']
assert l.collectEntries { [it.key, "lock-" + it.value] }.sort { it.value }*.key == ['c', 'a', 'b']
@nobeans
nobeans / trait_with_compilestatic.md
Last active March 24, 2018 06:30
GroovyのtraitとCompileStaticとの関係

GroovyのtraitとCompileStaticとの関係

Groovyのtraitでimplementsする側に@CompileStaticをつけた場合に、trait由来のコードに@CompileStaticの効果が出るのか気になったので、groovyConsoleのFinalizationのバイトコードを確認してみた。

結論として、CallSiteとか使われなくなるし、「効率的なバイトコードになってる」ようなんだけど、ちょっとまだよくわかってない。

trait側のint型へのキャストを無くしたバージョンでMyTrait@CompileStaticをつけると、型チェック違反でコンパイルエラーになるんだけど(期待通り)、 MyTraitじゃなくてMyClass側に@CompileStaticをつけた場合、型チェック違反にはならなかった。 なのに、効率的なコードになるというのどういうことなのかしら。

@nobeans
nobeans / use_custom_schema_for_grails3.md
Last active March 14, 2018 07:20
Grails3で独自のスキーマを使う方法 (with database-migration plugin)

DB側でスキーマを生成しておく。

> CREATE SCHEMA my_schema;
or
> CREATE SCHEMA my_schema AUTHORIZATION my_user;

application.ymlに以下の設定を追加する。

import groovy.json.*
def test(jsonText) {
new JsonSlurper().parseText(jsonText)
}
def jsonText = '{"a": 1, "b": [2,3,{"c": 999}]}'
assert test(jsonText) == [a:1, b:[2, 3, [c:999]]]
assert test('[]') == []
const fs = require('fs');
console.log(fs.existsSync('/tmp/NOT_FOUND.txt'));
try {
console.log(fs.statSync('/tmp/NOT_FOUND.txt'));
} catch (err) {
console.error("ERROR: ", err);
}
console.log("-------------------------------");
const resolver = new Promise((resolve, reject) => {
console.log("resolve"); // この瞬間同期的に実行されている
resolve("resolved");
});
const rejector = new Promise((resolve, reject) => {
console.log("rejector"); // この瞬間同期的に実行されている
reject("rejected");
});
const catcher = (err) => (console.log(err))
function test(pattern, text, expected) {
const regexp = new RegExp(pattern)
const matched = regexp.test(text);
console.log(pattern, text, matched, (matched === expected) ? "OK" : "NG");
}
test("a", "aaa", true);
test(".a.", "aaa", true);
test("x", "aaa", false);
test("^/a/b/c", "/a/b/c/d/e", true);
new Promise(() => {
console.log("begin");
try {
throw Error("HOGE: 1"); // new Promise()で明示rejectせずに例外をスローしてもreject扱いになる
} finally {
console.log("end");
}
}).catch((err) => { // ここが非同期実行
console.log("Catched error", err);
})
function test(array) {
return Array.isArray(array) ? array.shift() : undefined
}
console.log(test([1]));
console.log("" + test([1]));
console.log(test([1, 2, 3]));
console.log("" + test([1, 2, 3]));
console.log(test([]));
// http://jmatsuzaki.com/archives/16866
var l1 = [1, 2, 3];
console.log(l1);
var l2 = [1, 2, 3];
console.log(l2);
console.log(l1 == l2); // false
console.log(l1 === l2); // false