Skip to content

Instantly share code, notes, and snippets.

@plonk
plonk / make_recur.rb
Created July 21, 2013 16:07
無名関数を再帰させる構造
def make_recur(&def_block)
delegate = proc { |*args|
user_proc = def_block.call(delegate)
user_proc.call(*args)
}
end
p make_recur { |myproc|
proc { |n|
(n == 1) ? 1 : n * myproc.call(n-1)
@plonk
plonk / anon_func_recur.rb
Created July 21, 2013 16:46
無名関数を再帰させる構造
p (fac = proc { |n| (n == 1) ? 1 : n * fac.call(n-1) }).call(5)
# OUTPUT: 120
@plonk
plonk / anonymous_recursion.cc
Last active December 20, 2015 01:29
匿名再帰
#include <iostream>
#include <functional>
using namespace std;
int main()
{
function<int(int)> fac = [&] (int n)
{
if (n == 1)
@plonk
plonk / cataphora.cc
Created July 25, 2013 08:04
「後方照応」
#include <iostream>
int main()
{
std::cout << someConst << std::endl;
}
const int someConst = 10;
@plonk
plonk / cataphora.rb
Created July 25, 2013 08:06
「後方照応」
def main
p SomeConst
end
SomeConst = 10
main
@plonk
plonk / cataphora.cs
Created July 25, 2013 08:11
後方照応
static class Program
{
static void Main()
{
System.Console.WriteLine(someConst);
}
const int someConst = 10;
}
@plonk
plonk / rtl.cc
Last active December 20, 2015 14:49
Clojure の ->> の C++ によるイミテーション
#include <functional>
#include <vector>
#include <string>
#include <iostream>
#include <cassert>
using namespace std;
vector<string> split(const string& sep, const string& in);
string join(const string& sep, const vector<string>& in);
@plonk
plonk / poker.rb
Last active December 21, 2015 10:28
ポーカーの役を判定するやつ
# -*- coding: utf-8 -*-
# This is the Card class.
# An instance of it has a suit and value.
class Card
attr_reader :suit, :number
# Card numbers.
NUM_ACE = 14
NUM_JACK = 11
NUM_QUEEN = 12
@plonk
plonk / is_either.rb
Created August 21, 2013 12:16
AかBかCならば
class Object
def is_either?(*ary)
ary.include? self
end
end
value = :a
if value.is_either? :a, :b, :c
puts true
#include <iostream>
#include <functional>
using namespace std;
class Object
{
friend int main();
public:
Object(string data) : _data(data) {}