Sunday, June 30, 2013

Learning VIM (if using Windows)

vim在linux/unix底下因可以快速在shell和vim中切換,會有比較好的效益,然而,vim編輯器本身的能力即使在Windows底下仍是一樣的。
在Windows底下跑vim有幾種方法:
  1. 安裝gVim http://www.vim.org/download.php#pc
  2. 透過ssh等方式連到一台linux/unix的機器
  3. 使用Cygwin創立linux/unix模擬環境
純文字檔的編輯用方法1即可,但若是希望可以整合系統,同時也compile程式、執行、外部操作等,用方法2,3較合適。

//======================================

vim主要分成兩種模式:命令模式、編輯模式。我們一般notepad功能即是vim的編輯模式,鍵盤輸入什麼就出現什麼,而移動指標等操作需要透過方向鍵或滑鼠等。

vim可貴的地方在於命令模式,處於命令模式時,鍵盤輸入的東西並不會直接出現在螢幕,而是執行了所對應的指令,例如,j按鍵是下,k是上等。

vim越是熟悉之後,處於命令模式的比例越高,少數情況才進入輸入模式,如此狀況下編輯文件速度快。

//======================================

vim指令設計上其實是非常符合手指位置的(人體工學?)。一般而言,看到一份文件第一個用的指令是向下卷,即是指令 j ,可以使游標往下,而 j 恰好是右手食指位置,不需要移動手指就可以按到,向上的指令 k 則是位居第二名。

而有些則是從名稱的縮寫而來,例如 i 是insertion的縮寫 d 是delete等。

//======================================

vim非常適合處理反覆的情況,例如,往上的指令是 k ,若希望往上八行,打 8k 即可。
更深入的部分有巨集,使用指令 q ,可以記憶一連串的指令,然後再重複!(寫verilog十分需要)

//======================================

再者就是客制化,透過撰寫.vimrc這份設定文件,可以依照自己的需求設定vim,例如設定「編譯並執行」的快捷鍵、設定tab大小、設定程式syntax變色規則等。當移動到新的系統上,只需要將這份文件放到自己的home目錄,vim馬上又是自己最熟悉的樣子了。

//======================================

vim學習曲線比較陡,一開始可能會發現打文件反而變慢,必須經過一段時間才會達到效果。於是我畫了一張示意圖:

//======================================

Real Tutorial這裡:
  1. 基本指令(其實大概80%在用的都出現在此網頁):http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/
  2. Vim Adventure,透過玩遊戲的方式學習vim: http://vim-adventures.com/
  3. Vim Cheat Sheet,圖片方式的指令集: http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html
  4. Complete Tutorial: ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf
// end here

Drupal Installation

Follow the instruction in Drupal tutorial:
https://drupal.org/documentation/install/settings-file

However, for the permission setting part, it's not working if I follow the instruction.
During the installation, Drupal may have to do some modifications on the files in sites/default/, they may be modified. So, 'w' is required.

The easiest way to solve the permission issue here is to grant all the permissions and modify them back as soon as the installation is done.

  • chmod 777 settings.php && chmod 777 ../default/
  • process the installation
  • chmod 644 settings.php ^&& chmod 755 ../default/
Done.

Sunday, June 23, 2013

std::vector

C++中std的vector十分方便,可以解決多數動態大小的問題,是索引值的方式和array更是相同,而新的值用pushback加入。

#include <cstdio>
#include <vector>

using namespace std;

int main(){

 vector<int> myVector;

 // setter: push new
 myVector.push_back(2); // index = 0
 myVector.push_back(5); // index = 1
 myVector.push_back(1); // index = 2

 // getter: by index
 printf("%d\n", myVector[1]); // 5

 // get size
 int size = myVector.size();
 printf("size = %d\n", size);

 return 0;
}

std::map

std::map是C++中的函式,include <map>之後可以使用。

很多時候我們需要用「一種資料」去查另外一種「對應的資料」,這時候就是map上場的時候了,有些程式語言稱為dictionary,顧名思義就是查字典的意思。使用一個"key"去找對應的"value"。

注意key可以是任何的資料形態,value也是,以下是一個簡單的範例程式,包含:

  1. setter:設定新的項目
  2. getter:取得已存的項目,若不存在會回傳0,並被加入map中
  3. is not found:檢查該key有無存在在map中
  4. wall through:走過所有map中的項目

#include <cstdio>
#include <map>
#include <iostream>

using namespace std;

int main(){

 map<char, int> myMap;
 map<char, int>::const_iterator iter;

 // setter
 myMap['h']=2;
 myMap['e']=3;

 // getter
 printf("%d\n", myMap['h']); //2
 printf("%d\n", myMap['e']); //3
 printf("%d\n", myMap['r']); //0, not exist

 // check if exsit
 int isNotFound;
 isNotFound = ( myMap.find('z') == myMap.end() );
 printf("%c\n", (isNotFound)?'y':'n'); // y

 isNotFound = ( myMap.find('e') == myMap.end() );
 printf("%c\n", (isNotFound)?'y':'n'); // n

 // walk through all elements in map
 for(iter = myMap.begin() ; iter != myMap.end() ; iter++)
  printf("- %c:%d\n", iter->first, iter->second);

 return 0;
}

Thursday, May 16, 2013

Matchismo on iOS



This is the first assignment for the course CS193P at Stanford 2013 winter session. It shows how the "MVC" structure works, and also includes really good coding style in objective-oriented programming.

http://www.stanford.edu/class/cs193p/cgi-bin/drupal/

Some interesting stuffs I would like to share is as below:

  1. Connecting the "View" related events or properties by dragging the object from GUI programming interface to text code. Cool.
  2. NIL pointer design, I've written things about this in last post.
  3. General initialisation design. Objects in objective-C mostly have the some usage in initialisations, setters/getters, function callings, etc. It's easy for the developers to learn some basic examples and apply on different occasions.
  4. Auto-complete editing design in Xcode. Although this feature, auto-complete while typing the code, is not new stuff, it's still the best auto-complete mechanism I've ever seen. It's fast, and smart. It allows you to jump to next blank in a very short time. While writing the program, you spent very little time on typing. Around 80%~90% of the code can be done by the auto-complete function, only the new names for object/function/etc are needed to be typed completely. At last, Xcode also generates those an annoying special characters in the code, like "", [], {}, etc.
Finally, the developing environment for iOS is not only including great designs but also so much fun.



Saturday, May 11, 2013

NIL Pointer in Objective C

In many programming languages, we do the checking if the pointer is null (equally, 0) or not before we start to grab the content. However, in Objective C, there's no need to do this. The design makes you no need to do so.

When a pointer has no object points to, it's automatically storing the value 0, which we call a NIL pointer. And, we can complete our other designs without checking the pointer is NIL or not because that function calls just do nothing on NIL pointers, and return 0.

Take one example I learned in the class,
if ([card.contents isEqualToString:self.contents] ){ score = 1; }
No need to card.contents is NIL or not. If it's NIL, isEqualToString just simply skip it and return 0, which may cause the if-statement failed, which is also the right thing we want.

Thursday, April 18, 2013

Why VIM?


" Vim不是「很難用的編輯器」,只是你不夠瞭解他 "

Vim中一般文字編輯器沒有功能

  1. 命令模式:每個按鍵變成指令,可在短時間內編輯你想要的東西,例如:「在行尾加句點(2 keys)」、「大小寫互換(1 keys)」、「複製目前這行九遍(4 keys)」、...。而做反覆的事情只需要加上數字便可以表示做幾次,另外甚至可以寫一整串的指令。
  2. 客制化:透過撰寫.vimrc這份檔案,可以把vim改成自己喜好的樣式,例如:「設定compile and run的快捷鍵」、「設定android開發時需要的指令成快捷鍵」、...
  3. 結合shell:Vim可以簡易的在編輯器中輸入外部系統指令,也可以讓外部容易的連上Vim編輯文件,相輔之下,讓人在寫程式快速穿梭在系統和文字編輯之間。

說服大家使用Vim的原因很多人都寫過(如上),我個人會喜歡舉一些簡單例子:

Q:欲在游標指向的這行,句首第一個字元從小寫改大寫,再在句尾加上一個句點(一個我們常常會遇到的情況)

「一般文字編輯器」方法:
  1. 連續按左箭頭到句首(或是用滑鼠小心點擊)
  2. 按delete刪掉本來的小寫字元
  3. 輸入大寫字元
  4. 再連續按右箭頭到句尾(或是移動手改用滑鼠)
  5. 按'.'輸入句點
「Vim」方法:
  1. 輸入:「^~A.」即可

最後,雖Vim需要經過學習,一開始使用也不會比較快,但學習之後可以快很多很多很多....