Skip to content

Instantly share code, notes, and snippets.

@maraigue
maraigue / Makefile
Last active January 1, 2016 08:18
Storing functional objects in C++ (pointer for function, functor and lambda) in std::function or template class
CXXFLAGS = -std=c++11
default:
# make sample-{std-function,template}
sample-std-function: sample-std-function.cpp sample-std-function.hpp
$(CXX) $(CXXFLAGS) $< -o $@
sample-template: sample-template.cpp sample-template.hpp
$(CXX) $(CXXFLAGS) $< -o $@
@maraigue
maraigue / rglpk-mip.rb
Last active December 29, 2015 20:29
An example of solving mixed integer programming by rglpk (Ruby interface for GLPK)
require "rglpk"
# An example of solving mixed integer programming by rglpk (Ruby interface for GLPK)
# Original C-language-version by Masahiro Sakai
# https://gist.github.com/msakai/2450935/32efeace1ce27b2259b9792e8731436d7b0bb523
#
# rglpk must be installed
# https://en.wikibooks.org/wiki/GLPK/Ruby
# Maximize
@maraigue
maraigue / hash_access.rb
Created September 26, 2013 07:41
[Ruby] Hashのキーをドット繋ぎでもアクセス可能にする(hoge[:piyo] → hoge.piyo)
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
class Hash
# hashobj[:hoge] を hashobj.hoge でもアクセス可能にする
# ・値の読み取りはどんなキーでも対象
# ・値の書き込みはすでに存在するキーのみ対象
# ・すでに存在するメソッドと名前が重複した場合は、
#  すでに存在するメソッドへのアクセスが優先される
# ・当該名称のキーがStringとSymbolの両方で存在する場合は例外
@maraigue
maraigue / shogi-moves-osl.cpp
Last active December 17, 2015 16:18
OpenShogiLib version 0.4.2 の簡単なサンプル / A simple sample of using OpenShogiLib version 0.4.2
// OpenShogiLib version 0.4.2 の簡単なサンプル
// http://gps.tanaka.ecc.u-tokyo.ac.jp/gpsshogi/index.php?OpenShogiLib
//
// ライブラリのインストール方法(APTが使える場合)
// sudo apt-get install libosl-dev libosl-doc
// ※libosl-docも導入を推奨。Webに上がっているリファレンスは古いので。
// ※自前でソースコードからコンパイルする場合、Boostを導入した上で行う必要が
// あります。
#include <osl/state/simpleState.h>
@maraigue
maraigue / eucjp-unavailable-chars.sh
Created March 19, 2013 09:31
Unicode(UTF-8でもUTF-16でも)のファイルを与えて、その中にあるEUC-JPで扱えない文字を検出するためのスクリプト
#!/bin/sh
nkf -e --no-best-fit-chars --fb-perl | grep -n '\\x{[0-9]\+}'
# --fb-perl は、変換において扱えない文字が存在した場合に、それをPerlの形式(\x{123})で出力する。
# これをgrepで検出している。
@maraigue
maraigue / README
Created September 19, 2012 06:44
A zsh script for showing "[git]" in the prompt if you are a git repository directory
※日本語の解説: http://blog.livedoor.jp/maraigue/archives/1689443.html
A zsh script for showing "[git]" in the prompt if you are a git repository directory
If NOT in a git repository directory, the prompt will be:
<USERNAME@HOSTNAME CURRENTDIR>$
If in a git repository directory, the prompt will be:
<USERNAME@HOSTNAME CURRENTDIR>[git]$
If in a remote directory (e.g. SSH-logged-in), the prompt will be:
<USERNAME@HOSTNAME CURRENTDIR>[remote]$
@maraigue
maraigue / define_singleton_method.rb
Created August 19, 2012 07:03
[Ruby] def文によらずに特異メソッドを定義する To define singleton methods without `def' sentence
# ---------- definition ----------
class Object
def define_singleton_method(name, method = nil, &block)
unless !!(method) ^ !!(block)
raise ArgumentError, "Only one method should be given"
end
source_object = self
Module.new do
define_method(name, method || block)
これをg++でコンパイルすると
find2.cpp: 関数 ‘TYPE str2num(std::string&) [with TYPE = int, std::string = std::basic_string<char>]’ 内:
find2.cpp:17:20: instantiated from here
find2.cpp:7:28: エラー: ‘iss’ has incomplete type
というエラーになる。
#include <sstream> を加えると解決する。
@maraigue
maraigue / nyaruko.rb
Created May 30, 2012 17:52
Rubyのソースコードを与えると、同じ働きをする「(」・ω・)」うー!(/・ω・)/にゃー!」だらけのコードにするプログラム ※最新版は http://github.com/maraigue/nyarucode-ruby をご覧下さい
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# Rubyのソースコードを与えると、同じ働きをする
# 「(」・ω・)」うー!(/・ω・)/にゃー!」だらけの
# コードを出力するプログラム
#
# Ruby1.9系列限定、文字コードは1種類に統一する必要あり
#
# 元ネタ(JavaScript版):
@maraigue
maraigue / proc-rescue.rb
Created May 27, 2012 06:02
Rubyのrescue演算子に例外クラスが指定できないので無理やり指定できるようにしてみた(その2)
# その1: http://gist.github.com/2802312
class Proc
def rescue(klass, value)
begin
self.call
rescue klass
return value
end
end