Groovyで特定文字(空白類文字)を含まない部分の長さを調べる その3(完結編)

Groovyで特定文字(空白類文字)を含まない部分の長さを調べる
Groovyで特定文字(空白類文字)を含まない部分の長さを調べる その2
で悩み、試行錯誤した結果、以下となった。
Groovyでdef関数からdef関数が呼び出せないのか?

groovyshで実行するにはクラス定義をするか、もしくは、あまり現実的ではないが1行で書く必要が
ある。
これは仕様なのか?ちょっとgoovyshのdefの扱いが変な気がする。

groovyshでクラス宣言で実行

ソース

class test{
    def matching_length(str, regexp){
        def result = 0
        def m = (regexp).matcher(str)
        m.find()
        m.each{ chunk ->
            result += chunk.size()
        }
        return result
    }

    def unmatching_length(str, regexp){
        return str.length() - matching_length(str, regexp)
    }
}
t = new test()
/* 空白類文字以外は何文字か? */
println t.unmatching_length("abc d eee ff\n", ~/\s/)


/* 空白類文字は何文字か? */
println t.matching_length("abc d eee ff\n", ~/\s/)
println t.matching_length("abc d eee ff", ~/\s/)
println t.matching_length("", ~/\s/)
// ※空白が12個書いてある
println t.matching_length("            \n", ~/\s/)
println t.matching_length("            ", ~/\s/)

/* 「も」は何文字か? */
println t.matching_length("すももももももももものうち", ~/も+/)


/* 空白類文字以外は何文字か? */
println t.unmatching_length("abc d eee ff\n", ~/\s/)
println t.unmatching_length("abc d eee ff", ~/\s/)
println t.unmatching_length("", ~/\s/)
// ※空白が12個書いてある
println t.unmatching_length("            \n", ~/\s/)
println t.unmatching_length("            ", ~/\s/)

/* 「も」以外は何文字か? */
println t.unmatching_length("すももももももももものうち", ~/も+/)

結果

groovy:000> load 044_4_特定の文字を含む部分の長さを調べる.groovy
===> true
===> test@1f52460
===> true
9
===> null
===> true
4
===> null
3
===> null
0
===> null
===> true
13
===> null
12
===> null
===> true
9
===> null
===> true
9
===> null
9
===> null
0
===> null
===> true
0
===> null
0
===> null
===> true
4
===> null

groovyで実行 クラス宣言なし

ソース
def matching_length(str, regexp){
    def result = 0
    def m = (regexp).matcher(str)
    m.find()
    m.each{ chunk ->
       result += chunk.size()
    }
    return result
}

def unmatching_length(str, regexp){
    return str.length() - matching_length(str, regexp)
}

/* 空白類文字以外は何文字か? */
println unmatching_length("abc d eee ff\n", ~/\s/)


/* 空白類文字は何文字か? */
println matching_length("abc d eee ff\n", ~/\s/)
println matching_length("abc d eee ff", ~/\s/)
println matching_length("", ~/\s/)
// ※空白が12個書いてある
println matching_length("            \n", ~/\s/)
println matching_length("            ", ~/\s/)

/* 「も」は何文字か? */
println matching_length("すももももももももものうち", ~/も+/)


/* 空白類文字以外は何文字か? */
println unmatching_length("abc d eee ff\n", ~/\s/)
println unmatching_length("abc d eee ff", ~/\s/)
println unmatching_length("", ~/\s/)
// ※空白が12個書いてある
println unmatching_length("            \n", ~/\s/)
println unmatching_length("            ", ~/\s/)

/* 「も」以外は何文字か? */
println unmatching_length("すももももももももものうち", ~/も+/)



結果

[D:\workspace\groovy_SandBox]groovy D:\workspace\groovy_SandBox\Q044_5_特定の文字を含む部分の長さを調べる.groovy
9
4
3
0
13
12
9
9
9
0
0
0
4

これからは、groovyshではなく、groovyを使うとするか。

実行環境




最近のキーワード









Groovyの詳細についてはJavadocと以下の書籍を参考にしている。

Groovyイン・アクション
Dierk Konig Andrew Glover Paul King Guillaume Laforge Jon Skeet
毎日コミュニケーションズ
売り上げランキング: 294340


問題自体は以下の書籍のもの。rubyと似てる部分も多いので、ヒントにもなる。
写経でもいいが自分で考えるために他言語の例をGroovyで置き換えてる。

Rubyレシピブック 第2版 268の技
青木 峰郎 後藤 裕蔵 高橋 征義
ソフトバンク クリエイティブ
売り上げランキング: 80467


Groovyイン・アクションを読むならあった方が便利かな。

ブックストッパー

トモエ算盤
売り上げランキング: 614


Rubyレシピブックは「ほんたった」で立ててる