👻 道化師 🃏 @wraith13 次の( )に当てはまる適切な言葉を述べよ。(10点):「(std::)tie が( )していてよ。」 [電波注意] 2010-04-28 00:43:41
👻 道化師 🃏 @wraith13 次の( )に当てはまる適切な言葉を述べよ。(10点):「(std::)tie が( )していてよ。」 [電波注意] 2010-04-28 00:43:41
sample サンプルの動作確認バージョン [GCC4.4/1.41.0] [VC9/1.41.0] // boost::optional的な物の再開発 #include <new> #include <boost/utility/in_place_factory.hpp> template<typename T> class my_optional { public: my_optional() : storage(0) {} template<class InPlace> my_optional( const InPlace& p ) : storage(reinterpret_cast<T*>(new char[sizeof(T)])) { p.template apply<T>(storage); } ~my_optional() { delete storage; } opera
boost::optional は「値 T あるいは無効値」を取ることのできるクラスです。 通常このクラスは、「失敗するかもしれない」関数の戻り値として使用されます: #include <boost/optional.hpp> #include <iostream> // 例として std::getline を optional で実装してみる boost::optional<std::string> getline_optional( std::istream& in ) { std::string result; if( getline( in, result ) ) { // 行の読み取りに成功 return result; } // 失敗。無効値( boost::none )を返す return boost::none; } int main() { // cat コマンドっぽい挙
クロージャが使えると、 それを使っていろんなことが可能になるので、 C でも似たことができないか試してみた。 内部状態をもつ簡単なカウンタを作ってみます。 最初に初期値を指定して、 呼び出す時に引数で数値を渡すと、 内部のカウンタがそれだけ増えて、 カウンタの値を返すというクロージャです。 Scheme だと Scheme で普通に書くと、 こんな風になると思います。 (define (make_counter k) (let ((n k)) (lambda (a) (set! n (+ n a)) n))) こうやってクロージャを返す関数を作って、 (define c1 (make_counter 0)) (define c2 (make_counter 5)) 初期値を変えてクロージャを 2 つ生成します。 (print (c1 3)) ; => 3 (print (c2 1)) ;
わりとありがちなネタで。まったく関係ない講義受けてるときにふと思いついた。 #include <stdio.h> #include <string.h> char COUNTER_CODE[] = { 0x55,0x89,0xe5,0xff, 0x05,0x00,0x00,0x00, 0x00,0xa1,0x00,0x00, 0x00,0x00,0xc9,0xc3 }; /* int falsecounter(){ return ++*((int *)0x00); } */ int (*makecounter())(){ char *counter = (char *)malloc(16); unsigned int count = (unsigned int)calloc(1, 4); memcpy(counter, COUNTER_CODE, 16); memcpy(counter+5
2007年05月22日22:00 カテゴリLightweight Languages Cで強引にたらいを後回し 404 Blog Not Found:λ萌え - たらいを後回しを書いた後、なんとかCでも出来ないかと、車輪の再発明する代わりに横車を押してみた。 GCCにはclosureがある! 実は以外と知られていないが、gccでは無名関数は使えなくとも、closureは使える。最近では無名関数をサポートしている言語が人気ということもあり、また無名関数の多くがclosureとしても用いられているため、無名関数の別名としてclosureという呼び方をすることもあるが(私もときどきやる)。この二つは本来は別物である。JavaScriptで書くと、 var outer = 1; var closure = function(x){ return outer++ }; var nonclosure
Interpreters Good programmers decompose data from the interpreter that processes that data. Compilers exemplify this approach, where they will typically represent the source code as an abstract syntax tree, and then pass that tree to one of many possible interpreters. We benefit from decoupling the interpreter and the syntax tree, because then we can interpret the syntax tree in multiple ways. For
先日の渋谷での第74回Scala勉強会では、nescalaの資料を読んだのですが 勉強会のwikiページ そのなかで、この資料 Dead-Simple Dependency Injection にでてくる Free Monad について。 勉強会中、とりあえずひと通り読んで、なんとなーく理解した後に 「あれ、これってIOモナドとか他のものでも書けるんじゃね?何が違うんだろう?」 的な意見がでて、だがしかしそれを詳しく説明出来る人もいないし、考えても調べてもすぐに分かるような問題じゃない・・・ので放置してた。 が、まさにそのことについて発表した本人が twitter 上で話していたのでメモ。 まぁこれもまた、なんとなくしか理解できてないですが・・・一応怪しい訳で、自分なりに簡単にまとめると Free Monad 以外でも書ける けど、Free Monad で書いたほうがプログラムが明確になる
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く