Wednesday, December 30, 2009

Assign initial value to a pointer

Today I suddenly got an idea. Most of time when we use pointer, we create a variable, and assign initial value to it, and then take its address become the value of a new pointer for succeeding use. But we create a redundant identifier here.

In fact, we could always create a fundamental type pointer and assign initial value to it through dereference operator like this snippet.



and no redundant variable here.

But when we define a simple struct like this.



We should assign initial value through arrow operator like third and fourth line in below snippet. Using brace to assign value as the same as first snippet is wrong syntax.



With a complex struct definition, this way increases many redundant code. Using brace to assignment initial value without creating redundant identifier could be done like this.



But I consider writing (*aPtr) = { 1, 2 }; may be the most intuitive way and it doesn't conflict with other syntax in C.

 

Friday, November 13, 2009

Library for Numerical Computing

Briefly summarize some numerical library here. It looks there are too many useful tools. Either for parallel computing or for matrix, numerical calculating and plotting.

PLOT:

MatPlotLib: it seems to be the most awesome plotting tool among this list

PGPlot: C library, also have binding in Perl, Python,Ruby

PLPlot: cross-platform plot library, also have binding in Perl

Scientific Python

MRPlot


PARALLEL:

MPI Ruby: MPI Ruby binding

PyMPI: MPI integrated Python interpreter

MPI for Python Python MPI binding

Simple Remote Python : SrPy

Parallel Python


CALCULATION:

Perl Data Language: difficult to be categorized. Including calculating, plotting together. include FFTW, PGPlot, PLPlot, and many data processing tool such as HDF. Awesome Environment!

MPMath: Multi-Precision math functions collection.

RNUM

NArray

LibFFTW, also have binding in Perl, Ruby

Ruby DCL

LAPACK

SciPy

NumPy

GNU Scientific Library: C library, also have bindings in Ruby named Ruby/GSL and Ruby-GSL. and Python

RSRuby: bridge between Ruby and R

ARTICLES:

Ruby for Science

 

Sunday, August 30, 2009

Discussion about anonymous function in Perl and Ruby

Just for fun, tonight I write a methods invoking two anonymous functions to iterate a Range with specified condition in Ruby. It looks like
class Range
def each_satisfy()
end
end
And I want to invoke it as

(1..10).each_satisfy(condition_lambda, callback_lambda)

(1..10).each_satsify { |n| ...condition statment... } do |n|
...callback statement...
end
When I try to define its prototype as
def each_satisfy(&condition, &callback)
end
Ruby Interpreter broke with syntax error. So that, finally I write it as

def each_satisfy(condition, &callback)
self.each do |n|
yield n if condition.call n
end
end
and it should be used as

(1..10).each_satsify lambda { |n| ...condition statment... } do |n|
...callback statement...
end

It reminded me that Perl also has the same problem. Even when we declare

sub each_satisfy(&&) {

}

Still only first sub keyword could be omitted. That means we should invoke it as

each_satisfy { ... condition ... } sub {
my ($iter) = @_;
... callback ...
}

The only difference is Perl omitted the first (sub) and Ruby omitted the last (lambda).

I do want to figure out WHY they could not be designed to accept arbitrary quantity of anonymous functions.

 

Sunday, August 23, 2009

LWP, cURL, OpenSSL and Posterous

The fun of programming is there is always subtle mechanism in implementation. It costs time to discover but sometime is deserving. for example.

$ curl --basic -u <user> http://example.com/api/

$ echo -n "user:pass" | openssl base64 -e

and



to



 

Monday, August 17, 2009

Posterous API in Perl

Posterous.com is a new (micro)-blog mesh-up system.

You can use it mesh up almost all your (micro)-blog. Post once, Publish Everywhere. And there is still many funny features.

It looks pretty good, so I translated its API to Perl. You can get it from CPAN. Still work in progress, but would be productive soon.

 

Saturday, August 15, 2009

Can't locate Git.pm in @INC

Well, the installation of Github::Import worked well on Linux boxs but always reported "can't locate Git.pm in @INC" on Macintosh boxs. It is just a little trick and the answer is here.

Git.pm comes with git instead of being a part of CPAN. Building a copy of git-core or copying from other box solve it quickly.

 

Tuesday, July 7, 2009

the roadmap of upgrowth

和長輩們聊天總是很有趣的。即使那是一句老生常談,從長輩提攜的口吻中說出,依然受用。

6/26 的謝師宴,和賴暎杰老師坐在一起,我是這麼問的:「研究遇到瓶頸時,是怎麼面對的。」

「每天都多學會一件事,就不用擔心。」

yep...

 

Monday, July 6, 2009

Data::Model

Data::Model is a new ORM created by yappo, Its usage style is similar to DataMapper and Jifty::DBI.

Different from most famous equivalent, Data::Model handles multi-database. In order to do that, every model was appointed its database and table name in addition to its schema specification.

Let's see a simple example:



This is a simplest model in Data::Model, Only two DDL in it.

The first, base_driver( $driver ) specify which database the model would connect to. $driver is a Data::Model::Driver::DBI object. Write those diffusion code in every model is really not perlish, so we extract all possible drivers to MyApp::DB::driver().



The second, pass the table name and schema to install_model(), this step is the same as above two ORM system. and then, all thing done. By the way, columns() is the best syntax sugar provided by Data::Model::Schema.

Wait... Because Data::Model is too young to do auto_migration, we simply write MyApp::DB::make_schema() to do that. Finally, we could do simple CRUD in our application. as below.



Though Data::Model is too young to have some important feature such as validator and hook, even has no ability to handle relation between two table. The prototype is really exciting.

[Chinese Version]

 

.

Thursday, June 25, 2009

disable beep in emacs

使用 Emacs 也有一段時間,在 Mac 的 GUI 環境下使用 touch panel 撥弄 emacs 的 scroll bar,總會有令人中斷思緒的嗶嗶聲。Emacs Wiki: AlarmBell 這頁把處理警告的設定方式寫的頗為清楚。

要把警示聲換成視覺警示,可以寫成

(custom-set-variables '(visible-bell t))

在 GNU Emacs,這樣預設是閃爍第一行或最後一行,端看 touch panel 是轉哪一邊,XEmacs 則是閃爍整個螢幕,要讓 XEmacs 僅閃爍一行,則要寫成

(custom-set-variables '(visible-bell 'top-bottom))

完全關閉警告,可以設成

(custom-set-variables '(ring-bell-function 'ignore))

ring-bell-function 還可以設定成一個 lambda,指定警示聲去播放某個音效檔案。
(setq ring-bell-function (lambda ()
(call-process "audioplay" nil 0 nil
"/this/is/my/errorsound.au")))

或是送出訊息
(setq ring-bell-function (lambda ()
(message "use the message to replace beep")))


Emacs is awesome :D

 

Connect to SQLServer(MSSQL) in php5 with FreeTDS

In Debian It needs to

$ aptitude install freetds-common php5-sybase

Then add configuration about SQLServer to /etc/freetds/freetds.conf
[ConfigName]
host = host_of_sql_server
port = port_of_sql_server
tds version = correct_version
client charset = use_utf8_as_better

All thing done.

 

Monday, June 8, 2009

Pseudo-OO syntax in C

總是會忘記的 syntax



 

Tuesday, April 28, 2009

some notes

1. do find => rescue

2. model operation more than 3 lines => define class method in model

3. def current_user

4. render_as_form

5. more than one relation in one table

6. composite primary key => hook

 

Tuesday, April 7, 2009

Class::Implant - No &import() !

Class::implant is a experimental little helper implemented when I developed Railsish with gugod. Manipulating mixin and inheritance outside package is its primary function, also with abilities to select methods which would import.

Original idea is inspire by the purpose: we want to import whole Helper methods into Controller which the Helper is named after. And it is should not be left to framework users to do it by themself. In Ruby, we just need to write
XxxController.send(:include, XxxHelper)
In Perl, it can also be written as
eval qq{ package XxxController; use XxxHelper; }
But a tedious &import() should be write in XxxHelper as follow.
sub import {
for (qw(hello world foo bar method...)) {
*{blah::$_} = \&$_
}
}
and edit the export methods list by hand.

Even use Exporter and edit @EXPORT and export by symbol :all, users still need to do follow by hand
use base Exporter;
our @EXPORT = qw(..);
Class::Implant provide simplified equivalent.

package main;
use Class::Implant;
implant "XxxHelper", { into => "XxxController" };

Everything done! All methods in XxxHelper is imported into XxxController!

If call use Class::Implant in other package, default import target would become it. That means, above code is the same as follow:

package XxxController;
use Class::Implant;
implant "XxxHelper";

In the release 0.02_01, two other options work. { inherit => 1 } make imported packages appear in @ISA of import target. { match => pattern } filter, and import methods conform to pattern.

For example,

package main;
use Class::Implant;
implant qw(Foo Bar), { into => "Cat", match => qr{h\w+} };

means import methods whose name start with h in Foo and Bar into Cat.

I also write UNIVERSAL::Implant. As its name, require once, and write as follow everywhere.

Cat->implant qw(Foo Bar), { match => qr{h\w+} }

Do the same thing as previous example. That means, caller package Cat assign { into => "Cat" }.



Chinese Version is @ Chupei.pm.org

 

debian manpage-zh

上次寫 C 的時候對 Debian 還沒有很熟,今天偶然查 manpages 時才注意到有中文的 standard C library manpages。真相如下:



希望我沒有太後知後覺...XD

 

Wednesday, March 25, 2009

Rails on FreeBSD 7.1

Today I was first time trying to install rails on freebsd 7.1. Neither the ports version nor the gem file version, rails could not work, even only simply creating a new project. It returns message as following.

undefined method `camelize' for "app":String

I had try to reinstall ruby, rubygem, and try other version rails but a nonsense. And finally I forgot how did I discover it needs ruby-iconv ports in fact. It seems occurring at the time I was using rails 2.2.2, some keyword appeared in the error message.

One post on the mailing list [Ruby on Rails : Talk] show the same problem. If you also have the same problem, try it.

 

Saturday, March 14, 2009

REPL in Perl

Read-Evaluate-Print Loop 在現代語言中非常常見,諸如 Scheme, Haskell, Ruby, 與 Python 都有內建,實際工作時是非常實用的工具,Perl 6 也即將內建 REPL Shell,Perl 5 則沒有。

這幾天偶然想在 Perl 中使用 REPL,搜尋了一下,在 use.perl.org 得到 2007 年發表的這篇文章,介紹了四個以 Perl 5 實做的 REPL Shell,翻譯如下。

Continue reading REPL in Perl.

 

Monday, March 2, 2009

two pragmatic commands

$ cp /some/path/to/myfile{,.back} # cp-the-file

$ mv filename.{old,new} # quickly-rename-a-file


 

Friday, January 16, 2009

day spec


#!/usr/bin/env perl

use warnings;
use strict;

$csg = CSG->new;

$csg->FristPhaseNote({
date => "2009/01/16",
location => "Lib, 5F, Column Q30",
prepare => ["9:40~11:50", "13:30~14:00"],
end => "17:10",
});

__END__


 

Thursday, January 8, 2009

摩羯座老公使用說明書

hmmm 只能說很中肯 XDDD

品名:
摩羯座老公

生產日期:
摩羯月(12月22日——1月19日),年份憑個人喜好。

主要成分:
奮鬥力51%,忠誠29%,正直13%,樸實木訥7%。

主要功能:
1、本產品能動性很強,可大量減少用戶勞作時間。
2、能為用戶的生活、情感、物質提供穩定保障。
3、能默默地儘量實現用戶需要。

適用人群:
適用於溫順嫻雅、勤儉節約的女性。

注意事項:
1、不要對該產品的浪漫因子抱有太多期望,不然失望的人一定是用戶。
2、本品在運作時,請別隨意打擾。
3、用戶在使用時,儘量少做錯事,否則要承受其冰冷外殼下的噪音騷擾。
4、別強行把該產品推到熱鬧、繁雜的環境中去。
5、請尊重本產品的勞作能力和對工作的偏愛。
6、用戶如遇到無法解決的障礙時,請立即諮詢「科技紫微星座網」,以免搶修不及時,要到民政局申請繁雜的換貨手續。

禁忌:
太以自我為中心、缺乏家庭經營觀念、無法理解本品事業心的人,為拒絕往來戶;本品對於「小心眼」之類的用戶評鑑頗為敏感,當面使用容易導致突發慘案,慎之。

貯藏:
該產品適宜在可施展抱負的工作環境中生存。

包裝:
看上去嚴謹、正式的包裝材料為宜。

備註:
使用一段時間後,本品的中部易出現膨脹,屬正常現象,不影響正常使用;如有其他影響使用之故障,可送至醫院相關科室進行維修。


 

Saturday, January 3, 2009

GitHub Creator

Install
# cpan Git::Github::Creator

Setting in ~/.github_creator.ini
[github]
login_page="https://github.com/login"
account=joe@example.com
password=foobar
remote_name=origin
debug=1
Then, in local git repository
github_creator --name my-project --desc "an awesome thing"

Arguments are optional in a Perl module with META.yml.

GitHub Creator's repository and manual on cpan