2014年4月6日 星期日

scala 語言特性優缺點分析

這篇文章闡述哪些 scala 特性"我認為"是好的,哪些是不好的。因為是"我認為",所以這是一篇充滿主觀意見的文章。


優點:

  1. type inference: 這個特性可以減少程式設計師輸入重複的資訊。
  2. lazy keyword: 可以做到延遲計算的功能 
  3. currying: 讓人更容易產生 partial function。
  4. 對於接受參數型別為 => Unit(Int等),要傳參數時,可以簡單的用 { } 代替。詳見範例 1
  5. actor: 多執行緒的函式庫,用 message queue share memory 的問題(當然,還是要看你的寫法。沒寫好還是有 share memory 的問題)



範例1
def repeat(times: Int) (func: => Unit) {
  for (n <- 1 to times) func
}

repeat (5) {
  println("SSS")
}


缺點:

  1. implicit conversion: 這會影響可讀性。因為你在閱讀時,無法立刻知道他在執行時,是以何種物件在執行。不過這個特性應該是為了能夠和 jvm 整合所產生的特性。
  2. 探討 scala 的函數呼叫與自動推論型別所產生的模糊情形


結論: scala 是一個很好的語言。不過他是 function programming language 的關係,可能不會紅起來。但是下一個如果能夠吸收 scala 的優點,改進 scala 的缺點,我猜能夠成為下一個主流語言。

2014年4月5日 星期六

scala play framework 使用方式





要建立專案,輸入

$ play new myFirstApp



進入 play 的 shell (用來輸入指令的)
$ cd myFirstApp
$ play shell


如果要啟用 dev server 在 play shell 輸入

run

或是

$ play run


進入 play 的 console (用來輸入程式碼的)

$ play debug console

如果要使用 stand-alone server

$ play start

你也可以把你的程式打包起來,複製到其他地方執行

$ play dist

他會產生一個 zip 檔,包含所有執行時必要的 jar。



其他可能有用指令

因為 neo4j 只支援 openjdk-7 ,所以用下面指令檢查 java version。不然 play start 未必能正常運作。

$javac -version
$java -version


2014年3月17日 星期一

2014年3月16日 星期日

chroot: failed to run command : No such file or directory

   If you use chroot and it said "chroot: failed to run command  : No such file or directory",  how to slove it? Ok, follow me.


   For example, if you type "chroot /tmp/root /bin/bash" and get error. First of all, check whether the file "/tmp/root/bin/bash" exists. If not, create it.

   But if the file exists and got "No such file or directory", use "file /tmp/root /bin/bash" to check the file. If the file is "statically linked", I don't know what happen to your system. If the file is "dynamically linked (uses shared libs)", you have two way to go. The one is that you can compile the file to "statically linked". The another is that copy the shared libs which your program use to the right path(here is /tmp/root/lib).
 
    You can use "ldd to find out all shared libs your prcess use. The output is like below.

    linux-vdso.so.1 =>  (0x00007fff747fe000)
libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007fd6544bc000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fd6542b8000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd653eef000)
/lib64/ld-linux-x86-64.so.2 (0x00007fd6546ff000)

2014年3月9日 星期日

golang 的 revel web framework 安裝與使用



要在 linux 下安裝 revel,要先設定環境變數,讓 go get 能夠將檔案下載到你指定的路徑

export GOPATH="/home/user/go"
export PATH="$PATH:$GOPATH/bin"

下載 revel
go get github.com/robfig/revel

編譯 revel

go get github.com/robfig/revel/revel

你可以用這個指令確定是否安裝成功

$ revel help

建立新應用程式

revel new myapp

注意:他是建立在 $GOPATH 的路徑上,而非當前目錄。輸出訊息會告訴你專案建在哪。

執行你的第一個程式

revel run myapp


有關 route 的資訊放在 conf/routes。內容依序為 method, path, function

GET     /                                       App.Index

App.Index 的程式碼大概長這樣。每個 Action 都會去找對應檔名的 view 來當 template。如下面程式碼會找 view 目錄下的 app/index.html 來當 template

package controllers

import "github.com/robfig/revel"

type App struct {
 *revel.Controller
}

func (c App) Index() revel.Result {
 return c.Render()
}

template 大概長這樣,這個 template 從 action 接受兩個參數,一個 name,一個 sum。

Hello, {{ .name }} {{ .sum }}

我們要傳資料到 template ,只要在 c.Render 裡放我們要的參數即可

func (c App) Index(name string, a int, b int) revel.Result {    sum := a + b return c.Render(name, sum)}

get/post method 的參數會被放在 action 相對應的參數裡。如 name=world&a=1&b=2,到 action 的話,變數 name是字串 world ,a 是整數1,b 是 整數2


如果你程式都寫完了,想要 deploy。用 

revel build import/path/to/app /tmp/app

之後你就可以到 /tmp/app ,用 ./run.sh 來跑 server 了。






2014年3月8日 星期六

web load balance

Nginx 要作到 load balance 很簡單,只要幾個設定就好了。以下面的 Nginx 設定檔為例(來源)。


worker_processes 2;
 
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
 
events {
    worker_connections 1024;
    use epoll;
}
 
http {
    charset utf-8;
 
    # Enumerate all the Tornado servers here
    upstream frontends {
        server 127.0.0.1:8000;
        server 127.0.0.1:8001;
        server 127.0.0.1:8002;
        server 127.0.0.1:8003;
    }
 
    include /path/to/nginx.mime.types;
    default_type application/octet-stream;
 
    access_log /var/log/nginx/access.log;
 
    keepalive_timeout 65;
    proxy_read_timeout 200;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1000;
    gzip_proxied any;
    gzip_types text/plain text/css text/xml
               application/x-javascript application/xml
               application/atom+xml text/javascript;
 
    # Only retry if there was a communication error, not a timeout
    # on the Tornado server (to avoid propagating "queries of death"
    # to all frontends)
    proxy_next_upstream error;
 
    server {
        listen 80;
 
        location ^~ /static/ {
            root /path/to/app;
            if ($query_string) {
                expires max;
            }
        }
 
        location / {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://frontends;
        }
    }
}
只要簡單的設定 upstream frontends(frontends名稱可自訂) 裡的 server ,即可作到 loadbalance。

下面連結有 lighttpd 的 load balance 例子。
http://www.playframework.com/documentation/2.2.x/HTTPServer
設定檔大概就長這樣



server.modules = (
      "mod_access",
      "mod_proxy",
      "mod_accesslog"
)

$HTTP["host"] =~ "www.myapp.com" {
    proxy.balance = "round-robin" proxy.server = ( "/" =>
        ( ( "host" => "127.0.0.1", "port" => 9000 ) ) )
}

$HTTP["host"] =~ "www.loadbalancedapp.com" {
    proxy.balance = "round-robin" proxy.server = ( "/" => (
          ( "host" => "127.0.0.1", "port" => 9001 ),
          ( "host" => "127.0.0.1", "port" => 9002 ) )
    )
}



apache 的 load balancer 的資料在這
http://httpd.apache.org/docs/2.2/mod/mod_proxy_balancer.html

2014年2月22日 星期六

2014年1月22日 星期三

ubuntu acer notebook brightness not work

    If you use ubuntu with acer notebook, and you fint that you can't change brightness using hotkey. Hot to solve it?

First edit the file, "/etc/default/grub". Add "acpi_osi=Linux acpi_backlight=vendor" to GRUB_CMDLINE_LINUX_DEFAULT. So the setting should look like below.

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash acpi_osi=Linux               acpi_backlight=vendor"

After saving the file, you should type the below command. And reboot.
sudo update-grub
Now you should change brightness using hotkey.


2014年1月20日 星期一

how to compile i3 window manager (i3 wm) 4.6

1.  Install the below packages.

sudo apt-get install -y libxcb1-dev libxcb-keysyms1-dev libxcb-util0-dev libxcb-icccm4-dev libyajl-dev libstartup-notification0-dev libxcb-randr0-dev libev-dev libxcb-xinerama0-dev libpango1.0-dev libxcursor-dev libxcb-keysyms1-dev libpango1.0-dev libxcb-util0-dev libxcb-icccm4-dev libyajl-dev libstartup-notification0-dev libev-dev libxcb-xkb-dev libxcb-cursor-dev libxcb-xinerama0-dev libxkbcommon-x11-dev libxkbcommon-dev

sudo apt-get install -y libxcb1-dev libxcb-keysyms1-dev libxcb-util0-dev libxcb-icccm4-dev libyajl-dev libstartup-notification0-dev libxcb-randr0-dev libev-dev libxcb-xinerama0-dev build-essential freeglut3-dev libfreeimage-dev libgl1-mesa-dev libopenal-dev bpango1.0-dev libsdl-ttf2.0-dev libsndfile-dev libxinerama-dev libxcursor-dev libxcb-cursor-dev


2. download i3-4.8.tar.bz2

3. uncompress i3-4.8.tar.bz2

4. cd i3-4.8

5. make

6. make isntall


Congratulations. You make it. I test the way in ubuntu 14.04 and mint 16.


If you want to compile i3-4.7, you need to compile http://cgit.freedesktop.org/xcb/util-cursor . But I don't know how to compile it.


How to compile i3status

1.  Install the below packages.

sudo apt-get install libpango1.0-dev libxcursor-dev libconfuse-dev libasound2-dev  libiw-dev


3. uncompress

4. cd to folder

5. make

6. make isntall

替 cubietruck 加上 bonding 模組

    如果你是根據 http://blog.blackwhite.tw/2014/01/cubie-truck-sd.html 這篇文章建立 lubuntu 的作業系統的話,你會發現他預設並沒有編譯 bonding 模組。因此如果我們想要使用 bonding 的話,必須自己編譯 kernel。

    編譯 kernel 是一件很麻煩的事。不過有人幫我們簡化了很多步驟了。https://github.com/davidandreoletti/cubietruck 這裡已經有人提供腳本幫我們完成 cross compile。不過你需要注意的是你要使用 Ubuntu 12.04 或是 arm-linux-gnueabihf-gcc 4.6.3 版本。不然可能會編譯失敗。我在 mint 16 (with arm-linux-gnueabihf-gcc 4.8.1 版本會編譯失敗。

    編譯完後,他會在 output 的資料夾產生很多檔案。你需要把 uImage 複製到你 sd 卡裡的 bootfs,覆蓋他。然後在複製 output/lib 裏面的內容到 sd 卡裡的 rootfs 的 lib 資料夾裡。etc 資料夾也一樣。複製完後,記得去改 sd 卡裡的 /etc/network/interface。他有寫好 bonding 的設定,記得去修改相關設定。