スターティングGo言語が良い

Go言語学ぶならA tour of Goより初めからスターティングGo言語買うと良い。とても良い。
付随的な知識でGoは「スーバーC」なので私はC言語で仕事をしたことが無いのもあるけれど、桁数とか桁あふれとか丸めとか補数とか体感できるし、そういう解説もある。JavaC#との比較もあるし「ベターなのはこの書き方」とかも書いてあるので非常に良い。本気でおすすめ。高い本ではないのでKindleで購入しましょ。まじ良い。

開発環境はGogland一択。デバッグに設定いらない、補完めっちゃ賢い。Idea系統の優れたUI最高です。CE版出てほしいなぁ。仕事でGoを使う可能性はゼロなので趣味のプログラムのために有償のIDE購入はちょっと違うよね、と。

今日の勉強おしまいー

スターティングGo言語 (CodeZine BOOKS)

スターティングGo言語 (CodeZine BOOKS)

再掲:javascriptでActiveRecordっぽくハッシュを検索するコードの断片

2011年の投稿ですが個人的によくできたと思ったので再掲します

記事

javascriptrubyと同様にインスタンス*1に対して動的にフィールドを追加できるようだ。javascriptなのでバインドは名前を付けた関数でも可能である。
ということでActiveRecordみたいな手触りのものが作れる。
めっぽう簡単なコードで。
今のところ機能として絞り込み(and検索)にも対応しているが都度実処理してしまっているところを最終段階まで遅延させたいところ。

// コード
var AR = {
    Init : function(hash){
      //関数をバインド
      for(x in this)
         if(typeof this[x] == "function")
            hash[x] = this[x];
      return hash;
    },
    find :function(k) {
        //関数をバインド
        for(x in this)
          if(typeof this[x] == "function")
             this[k][x] = this[x];
        return this[k];
    },
    query : function(m){
        //検索クロージャ
        this.selecta = function(finder,_this,qs){
            var ret = {};
            //検索をしつつ絞り込みのために関数もバインド    
            for(x in _this){
                if(qs == _this[x][finder])
                    ret[x] = _this[x];                
                else if(typeof _this[x] == "function")
                    ret[x] = _this[x];
            }
            return ret;
        };
        this.finder = m;
        return this;
    },
    is :function (qs){ // 検索を実行
       return this.selecta(this.finder,this,qs);
    },
    clean : function(){
       // バインドした関数を取り外す
       for(x in this)
         if(typeof this[x] == "function")
            delete(this[x]);
       return this;
    }
};
//対象データ 
var datasource = {
    tableA : { 
               rowA: { field1 : 1,field2 : 1, field3 : 4 } ,
               rowB: { field1 : 2,field2 : 1, field3 : 5 } ,
               rowC: { field1 : 3,field2 : 2, field3 : 6 } ,
               rowD: { field1 : 4,field2 : 2, field3 : 7 } ,
               rowE: { field1 : 5,field2 : 3, field3 : 8 } ,
               rowF: { field1 : 5,field2 : 3, field3 : 9 } 
            },
    tableB : { 
               rowA: { field1 : 1,field2 : 1, field3 : 4 } ,
               rowB: { field1 : 2,field2 : 1, field3 : 5 } ,
               rowC: { field1 : 3,field2 : 2, field3 : 6 } ,
               rowD: { field1 : 4,field2 : 2, field3 : 7 } ,
               rowE: { field1 : 5,field2 : 3, field3 : 8 } ,
               rowF: { field1 : 5,field2 : 3, field3 : 9 } 
            },
    tableC : { 
               rowA: { field1 : 1,field2 : 1, field3 : 4 } ,
               rowB: { field1 : 2,field2 : 1, field3 : 5 } ,
               rowC: { field1 : 3,field2 : 2, field3 : 6 } ,
               rowD: { field1 : 4,field2 : 2, field3 : 7 } ,
               rowE: { field1 : 5,field2 : 3, field3 : 8 } ,
               rowF: { field1 : 5,field2 : 3, field3 : 9 } 
            }
};
//使い方
var ret = AR.Init(datasource).find("tableB").query("field2").is(2).clean();
for(x in ret)
  console.log(ret[x].field1 + " - " + ret[x].field2 + " - " + ret[x].field3 );
/*
3 - 2 - 6
4 - 2 - 7
*/
//絞り込み
var ret = AR.Init(datasource).find("tableB").query("field2").is(2).query("field1").is(3).clean();
for(x in ret)
  console.log(ret[x].field1 + " - " + ret[x].field2 + " - " + ret[x].field3 );
/*
3 - 2 - 6
*/

都度回すよりも簡単です。ハッシュはjavascriptでも(やはり)速いので配列よりもハッシュがこのましい。
配列を回すと処理時間が不安定なのでsetTimeoutとか使って処理をバックグラウンドで行う場合にはやはりハッシュが好ましいです。

速度

dev状態のmusicdbで使用しているが軽快っぽい
http://lovesaemi.daemon.asia/musicdb_dev/

*1:呼び方知らない

Zabbix3.2のグラフで日本語を表示する。(どうしてもだめな方へ)

デフォルトだと日本語のグラフがだめ。
raspi.ryo.sc
このへんでフォントいれるんだよーとか言われてやってみてもだめ。

結論

/usr/share/zabbix/include/graphs.inc.phpをいじれ。

function imageText($image, $fontsize, $angle, $x, $y, $color, $string) {
    mb_convert_variables("EUC-JP", "UTF-8", $string); // コレたせ。

以上です。

A Tour of Go "moretypes/26"

フィボナッチ実装に5分位かかってもう廃業しようかもと思った。

package main

import "fmt"

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
	n := -1
	fn := 0
	fnP1 := 1
	fnP2 := 0
	return func() int {
		n += 1
		if n == 0 {
			return 0
		} else if n == 1 {
			return 1
		} else {
			fnP2 = fn + fnP1
			fn = fnP1
			fnP1 = fnP2
			return fnP2
		}
	}
}

func main() {
	f := fibonacci()
	for i := 0; i < 10; i++ {
		fmt.Println(f())
	}
}

A Tour of Go "moretypes/23"

package main

import (
	"fmt"
	"golang.org/x/tour/wc"
	"strings"
)

func WordCount(s string) map[string]int {
	fmt.Println(strings.Fields(s))
	var m = map[string]int{}
	for _, v := range strings.Split(s, " ") {
		_, exist := m[v]
		if exist {
			m[v] += 1
		} else {
			m[v] = 1
		}
	}

	return m
}

func main() {
	wc.Test(WordCount)
}

/usr/local/Cellar/go/1.8.1/libexec/bin/go run /Users/seijiro/Dropbox/code/GoglandProjects/first/10.go
[I am learning Go!]
PASS
f("I am learning Go!") =
map[string]int{"I":1, "am":1, "learning":1, "Go!":1}
[The quick brown fox jumped over the lazy dog.]
PASS
f("The quick brown fox jumped over the lazy dog.") =
map[string]int{"fox":1, "lazy":1, "dog.":1, "The":1, "quick":1, "brown":1, "jumped":1, "over":1, "the":1}
[I ate a donut. Then I ate another donut.]
PASS
f("I ate a donut. Then I ate another donut.") =
map[string]int{"another":1, "I":2, "ate":2, "a":1, "donut.":2, "Then":1}
[A man a plan a canal panama.]
PASS
f("A man a plan a canal panama.") =
map[string]int{"man":1, "a":2, "plan":1, "canal":1, "panama.":1, "A":1}

Process finished with exit code 0