Erlang: If-else
Very simple usage of If-Else Statement in Erlang Language
-module(what_the_if). -export([heh_fine/0, oh_god/1, help_me/1]). %% should check if this actually works (hint: an error will be thrown) heh_fine() -> if 1 =:= 1 -> works end, if 1 =:= 2; 1 =:= 1 -> works end, if 1 =:= 2, 1 =:= 1 -> fails end. oh_god(N) -> if N =:= 2 -> might_succeed; true -> always_does %% this is Erlang's if's 'else!' end. %% note, this one would be better as a pattern match in function heads! %% I'm doing it this way for the sake of the example. help_me(Animal) -> Talk = if Animal == cat -> "meow"; Animal == beef -> "mooo"; Animal == dog -> "bark"; Animal == tree -> "bark"; true -> "fgdadfgna" end, {Animal, "says " ++ Talk ++ "!"}.
Erlang: 簡単なサーバーとクライアントのメッセージプログラム
Erlangプログラミングではよく使う二つ以上の間でのメッセージ交換。
主に並行処理で必要不可欠の基礎ポイントをここで。
その簡単な例を作ってみた。
まずモジュールの名前をexample
-module(example).
関数を定義する。ここでは三つの関数を使うので三つとも定義する必要がある。関数名のあとの数字はパラメーターの数をあらわす。
-module(example). -export([main/0,server/0,client/2]).
ではまずMain関数から。新しいプロセスを作ることと、
-module(example). -export([main/0,server/0,client/2]). main() -> Server_function = spawn(fun server/0), %サーバーと言う関数をfunと名付け、Spawnを生成し、そのリターン値をServer_functionと名付ける spawn(example,client,[Server_function,1]), %Spawnでは上の書き方以外に、Spawn(モジュール名、関数名、 [その関数を呼ぶ際のパラメーター]) spawn(example,client,[Server_function,2]), spawn(example,client,[Server_function,3]), spawn(example,client,[Server_function,4]).
ここで重要ポイントがSpawn関数。以下がその使い方。
Pid = spawn(Fun)
Pid2 = spawn(mymodule, myfun, [Arg1, Arg2, ...])
Pid3 = spawn(OtherMachine, Fun)
Pid4 = spawn(OtherMachine, mymodule, myfun, [Arg1, Arg2, ...])
では次にServer関数を作る。これはサーバー上で何をするかを決める。
ここではシンプルに受け取った全てのメッセージをサーバーは表示させるだけにする。
-module(example). -export([main/0,server/0,client/2]). main() -> Server_function = spawn(fun server/0), %サーバーと言う関数をfunと名付け、Spawnを生成し、そのリターン値をServer_functionと名付ける spawn(example,client,[Server_function,1]), %Spawnでは上の書き方以外に、Spawn(モジュール名、関数名、 [その関数を呼ぶ際のパラメーター]) spawn(example,client,[Server_function,2]), spawn(example,client,[Server_function,3]), spawn(example,client,[Server_function,4]). server() -> receive({Pid,msg,N}) -> io:format("received from ~w ~b~n",[Pid,N]), server() end.
クライアントはどうやってサーバーにメッセージを送るか。
メッセージの送信の仕方は、
Pid ! Message
Spawnのリターン値であるプロセス識別子と!と送りたいメッセージを記す。
ではクライアント側の関数を作る。クライアントは何をするかと言うと、
Main関数で生成された四つのプロセスをサーバーに送り出すこと。
ただそれだけ。
-module(example). -export([main/0,server/0,client/2]). main() -> Server_function = spawn(fun server/0), %サーバーと言う関数をfunと名付け、Spawnを生成し、そのリターン値をServer_functionと名付ける spawn(example,client,[Server_function,1]), %Spawnでは上の書き方以外に、Spawn(モジュール名、関数名、 [その関数を呼ぶ際のパラメーター]) spawn(example,client,[Server_function,2]), spawn(example,client,[Server_function,3]), spawn(example,client,[Server_function,4]). server() -> receive({Pid,msg,N}) -> io:format("received from ~w ~b~n",[Pid,N]), server() end. dofor(I,F) -> if I > 0 -> F(), dofor(I-1,F); true -> done end. client(S,N) -> dofor(5, fun() ->% timer:sleep(100), S!{self(),msg,N}, done end).
上のdofor関数はただループするためだけのもの。一つ一つのプロセスをループし、サーバーに送る役割。
おしまい。
Erlang: exception error: no match of right hand side典型的なミス
Erlang は他のプログラミングと違い変数の作りかたと値を入れるやり方が違う。
変数名は全部最初は大文字から
Xxxx = 1. Yyyy = 2.
変数に値をあげるのは一度きり
%エラー Xxxx = 2.
他の関数で値を変えてもダメ
f(Y) -> Y = 5. %エラー main() -> X = 3, f(X), X.
小文字からの変数名はアトムといい、不変定数で、識別のために用いる記号と覚えてもいい。
%名前がxのアトム x. %名前がyのアトム y.
関数のパターンマッチングでの変数扱い
mylength([]) -> 0; mylength([_ | T]) -> 1 + mylength(T).
PHP: 二重引用符と単一引用符の違い
二重引用符(””)と単一引用符(’’)とは少し違う。
変数の扱い
<?php $value = "Masa"; echo "Hi $value"; ?>
<?php $value = "Masa"; echo 'Hi $value'; ?>
上の二つには違う結果が出る。
一つ目は
Hi Masa
と出て、二つ目は
Hi $value
結論
二重引用符で囲まれた文字列に変数名を使うと変数の値が文字列に置き換えられるが、単一引用符で囲まれた 文字列では置き換えられない。
プログラム書くとき気をつけないと。
おしまい
FeulPHP: データをデータベースにインサートする方法
まずORMを元に作ったModelがある。
<?php class Model_Survey extends \Orm\Model{ protected static $_table_name = 'Survey'; protected static $_primary_key = array('SurveyId'); protected static $_properties = array( 'SurveyId', 'Q1', 'Q2', 'Q3' ); } ?>
あとはControllerでこのモデルを呼び、行をインサートする。
インサートする方法はいくつかあるが、ここではDBクラスとQuery Builderで行う。
テーブルの詳細
CREATE TABLE `Survey` ( `SurveyId` int(11) NOT NULL, `Q1` varchar(1000) NOT NULL, `Q2` varchar(1000) NOT NULL, `Q3` varchar(1000) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- Indexes for dumped tables -- -- -- Indexes for table `Survey` -- ALTER TABLE `Survey` ADD PRIMARY KEY (`SurveyId`);
インサートするテーブルを設定する。
<?php $query = DB::insert('Survey'); ?>
テーブルの列を設定する。
<?php // Set the columns $query->columns(array( 'SurveyId', 'Q1', 'Q2', 'Q3' )); ?>
MySQLにインサートしたい情報は以下の通り。
<?php // Set the values $query->values(array( time(), "A", "B", "C", )); ?>
MySQLにインサートしたい情報がいっぱいある場合は以下の通り。
<?php // Set the values $query->values(array( time(), "A", "B", "C", )); $query->values(array( time(), "A", "A", "C", )); $query->values(array( time(), "B", "B", "C", )); ?>
最後は実行だけ。
<?php $result = DB::query($query)->as_object('Model_Survey')->execute(); ?>
おしまい