もう6年も記事を書いてないんだなぁ

最近は何でもかんでもやる人になってて、技術としてこれ!ってのがない。 昔はプログラミング言語にこだわってましたが、最近は少し勉強すればどれも一緒という感じで深く踏み入ってないところがある。 深く踏み入れば実際には言語でも得手不得手が違うのと…

clang3.2(trunk) on MinGW(MSYS) with GCC 4.7.0

clangのコンパイル方法は日本語でもあちこちにあるので省略。 現時点のsvn trunkからcheak outしたソースを使用した場合、 MinGWでGCC4.7.0を使っていると色々と問題が出るのでその修正方法を書きます。1.Include Pathの解決(既に知っている人が多いと思い…

Rubyでflip

関数合成できた。ポイントフリーができた。カリー化もある。 なら次はflipで部分適応の順序を入れかえてやりたい。 まずは下記のProc#flipを見て試す。 http://yuroyoro.hatenablog.com/entry/2012/08/10/232443 div = lambda{|x,y| x.to_f / y } div.(10,2)…

Rubyでポイントフリー続きの補足

def g ->x, y { x * y }.curry end def f ->y { y * 4 } end def foo(x, y) f.(g.(x).(y)) end あんなわけのわからんことしなくてもこれでいけた。 def foo # f.(g.(x).(y)) # (f << g.(x)).(y) # (f.method(:<<) << g).(x).(y) (f.method(:<<) << g) # hask…

Rubyでポイントフリー続き

さっきとは少し変わってgとf関数はλにしてます。 def g ->x, y { x * y }.curry end def f ->y { y * 4 } end def foo(x, y) f.(g.(x).(y)) end さて、このfoo(x,y)からxとyを取り除きます。 結果… def foo ((f.<<).<<.(g)) # f(g(x,y)) end foo.(2).(2) #=>…

Rubyでポイントフリー

まずは普通に関数定義するとこんなんですよね? def g(x) end def f(y) end def foo(x) f(g(x)) end foo関数をポイントフリーにしてみます。 まずは、関数合成 def foo(x) (method(:f) << method(:g).(x) #もしくは ->x { f (g x) } end 合成したので(method…

RubyでMaybeモナド

まずは、データとlookup関数を定義 db = {:alice => {:title => "Ms.", :job => "sales"}, :bob => {:title => "Mr.", :job => "engineer"}} def lookup(key) ->data { data.key?(key) ? Just.return(data[key]) : Nothing } end lookupのテスト p return!(d…

しばらく休養

休養中に勉強がてらRubyを触ってます。 でもなぜか調べて行くうちにHaskellについて調べてます。(モナドとか関数合成とかカリー化とか…。) やっぱりHaskellやれってことか?Rubyに関数合成がないとか知った時は残念すぎて断念しそうでしたが 自前で実装すれ…

プログラミング楽しい

C++

おひさしぶりです。 今回はstd::bindを使ってネストしたクラスのメンバ変数を束縛してみます。 以下サンプル #include <iostream> #include <vector> #include <functional> // std::bind std::placeholders::_1 #include <algorithm> // std::for_each struct Hoge { Hoge() : i(1) {} int i; }; stru</algorithm></functional></vector></iostream>…

Boost.勉強会感想とか

遅ればせながらBoost.勉強会の発表資料をアップしました。 http://cid-31a4fb569979bef3.skydrive.live.com/self.aspx/.Public/BoostAsio.ppt ※以下TLにあった質問ぽいものに対して解答 Q:AsioってCOMポート扱えるの? A:はい、発表資料に書いたとおり扱え…

C++でPropertyつづき

C++

前のエントリーに上書きしちゃったみたいです。汗 Boost.Propertyの実装案みたいなもの->http://codepad.org/A4vaAMPZ これみて簡単にならないかやってみました。 #include <boost/typeof/typeof.hpp> #include <iostream> template<typename T> struct property_get_type; template<typename ReturnType,typename Class> struct property_get_type</typename></typename></iostream></boost/typeof/typeof.hpp>

C++でPropertyつづき1

C++

self()呼び出しもできた。http://codepad.org/eKAdqttP 以下に同じコードを #include <boost/typeof/typeof.hpp> #include <iostream> template<typename T> struct property_get_type; template<typename ReturnType,typename Class> struct property_get_type<ReturnType (Class::*)() const> { typedef ReturnType type; }; template</returntype></typename></typename></iostream></boost/typeof/typeof.hpp>

もしもBoost.Threadにinterruptが無かったら。

C++

VC限定なコードですけが、こんな感じで追加できるんじゃないかと。 元ネタはtwitterでid:melponさんがstd::threadにinterruptが無いので欲しいってとこからです。 id:melponさんがつぶやいてたコードの大部分借りました。 #include "stdafx.h" #include <iostream> #pr</iostream>…

もはやBoost.ScopeExit・・・

C++

#include <iostream> #include <boost/typeof/typeof.hpp> #include <boost/preprocessor/seq/cat.hpp> #include <boost/preprocessor/seq/for_each_i.hpp> // Steven Watanabe's trick namespace closure { namespace args { template<int> struct declare; typedef void* declared; struct undeclared { declared dummy[2…</int></boost/preprocessor/seq/for_each_i.hpp></boost/preprocessor/seq/cat.hpp></boost/typeof/typeof.hpp></iostream>

boost::filesystemを使ってある拡張子のファイルの先頭行に文字列を

C++

ちょっと必要になったので書いてみただけです。>< #include <iostream> #include <string> #include <boost/filesystem.hpp> #include <boost/filesystem/fstream.hpp> #include <boost/xpressive/xpressive.hpp> void replace_file( const boost::filesystem::path& dir, const std::string& inset_str) { try { namespace fs = boost::filesyste…</boost/xpressive/xpressive.hpp></boost/filesystem/fstream.hpp></boost/filesystem.hpp></string></iostream>

ちょっとだけクロージャを作ってみるのメモ

C++

#include <iostream> #include <vector> #define CLOSURE_(A0) CLOSURE_DEF(##A0, __LINE__) #define CLOSURE_DEF(A0, id) CLOSURE_IMPL(A0, id) #define CLOSURE_IMPL(A0, id)\ struct Closure_##id {\ typedef decltype(A0) Arg0;\ Arg0& A0;\ Closure_##id(Arg0& a0) : A0(a</vector></iostream>…

N2855 noexceptについて

C++

C++0xの新しい提案でN2855(Rvalue References and Exception Safety)では、noexceptというキーワードが追加されています。 まずはコードから見ます。 noexcept int foo(int); int bar(int); noexcept void wibble(int x, int y) { x = foo(x); // OK: foo()…

練習でstd::reference_wrapperがラップしてる型を取得するメタ関数を作ってみた

C++

std::reference_wrapperの場合は保持してるTを返します。 std::reference_wrapperではない場合は受けた型をそのまま返します。 #include <functional> #include <tuple> namespace mpl { template<class T> struct remove_wrap; { typedef T type; }; template<class T> struct remove_wrap<std::reference_wrapper<T>> { typ</std::reference_wrapper<t></class></class></tuple></functional>…

ユーザ定義リテラルまとめ

C++

このエントリは見事に間違ってたので忘れてください。>< グローバルな名前空間でもユーザ定義リテラルは定義できます。参考 http://cpplover.blogspot.com/2009/09/user-defined-literal.htmlところで新たな疑問があったのでここにメモ。 template <char... C> std::s</char...>…

C++テンプレートテクニック

C++

やっとレビューを書きます。id:faith_and_braveさんとεπιστημηさんの共著 C++テンプレートテクニック この本はタイトルの通りC++のテンプレートを使ったテクニック集になってます。 対象の読者は中級以上といったところでしょうか。 STLを使ってプログラミン…

mpl勉強モード

C++

enable_ifを使ってテンプレートメンバー関数を特殊化してみた? #include <iostream> #include <string> #include <typeinfo> #if defined(__GNUC__) #include <type_traits> #include <cxxabi.h> #endif #if !defined(__GNUC__) template <bool b, class T> struct enable_if; template <class T> struct enable_if<true, T> { typedef T type; }; #…</true,></class></bool></cxxabi.h></type_traits></typeinfo></string></iostream>

Scalaメモ

http://itpro.nikkeibp.co.jp/article/COLUMN/20080613/308019/ ここ全部読む。

Windows版gcc4.4でC++0xのコードをコンパイルに`c++0x'だけではダメ

C++

コンパイルオプションに`-std=c++0x'だけではダメで、`gnu++0x'も指定する必要があります。 (もしくは`gnu++0x'だけでも良いかもしれません。) 結論としては`-std=c++0x'ではなく、`-std=gnu++0x'を指定しなければいけません。 C++0xのコードをコンパイルす…

クラスのポインタをvoid*でdeleteした場合デストラクタが呼ばれない

C++

意外と知らない人が多かったので簡単なサンプル #include <iostream> struct Hoge { ~Hoge() { std::cout << __FUNCTION__ << std::endl; } }; int main() { Hoge* p = new Hoge; void* pv = p; delete pv; return 0; } 実行結果 gcc4.4 実行結果 VC8 必ず正しいクラス</iostream>…

Erase-Remove Ideomを少しでも楽に使うために。(自分用メモ)

C++

こんな感じでコンテナの値を削除できるようにしてみた。 #include <algorithm> #include <vector> struct Hoge { int num_; explicit Hoge(int i) : num_(i) {} int getnum() { return num_; } }; int main(int argc, char** argv) { std::vector<Hoge> v; v.push_back(Hoge(1)); v.pu</hoge></vector></algorithm>…

boost::threadとstd::thread周りを調べてたら

C++

こんな記事を見つけました。 http://msdn.microsoft.com/ja-jp/magazine/cc163405.aspx 以下記事内に書かれているWindows Vista以降で使用可能な関数とPosixとの対応表を作ってみました。 PosixAfter Windows Vista pthread_cond_initInitializeConditionVar…

__FUNCTION__マクロのwchar_t版

C++

VCでは char版は__FUNCTION__ wchar_t版は__FUNCTIONW__ ほかには__FILE__も__FILEW__が存在します。 xutilityヘッダに以下のように定義されてます。 #define __STR2WSTR(str) L##str #define _STR2WSTR(str) __STR2WSTR(str) #define __FILEW__ _STR2WSTR(_…

月曜日から

徹夜です。 3日おきにリリースとかアジャイルにもほどがあります。 今までガチガチのウォーターフォールでやってきたくせに! ばか! そいえば昨日The翻訳2009プレミアム専門辞書パック買いました。 おかげで今月と来月は金欠状態です。 本当にありがとうご…

N2800 P29の一部を訳してみました。

C++

原文§ 2.13.7の2 2 A user-defined-literal is treated as a call to a literal operator or literal operator template (13.5.8). To determine the form of this call for a given user-defined-literal L with ud-suffix X, the literal-operator-id whos…

まだ考えてました。

C++

#include <tuple> #include <iostream> #include <string> #include <functional> #include <stdexcept> void printf(const char *s) { while (*s) { if (*s == '%' && *(++s) != '%') throw std::runtime_error("invalid format string: missing arguments"); std::cout << *s++; } } template</stdexcept></functional></string></iostream></tuple>