����äȤ⡣

0��1�򼡡��֤���ˡ - a2c.get.diary
True���ä���False�ǡ�False���ä���True�ˤ�������
�ʤ󤫤���ʤ��Ȥ�����������ɬ�פǡ����ΰ٤������ʤ�Τ�
����Τ��ʤ��Ȼפä��������̵���ݤ�

Closure

����ϰ��֤�������ʤΤ�����

JavaScript

()���Ѥ路������perl��ruby���ϼ�����

#!/usr/bin/js
var flipflop = function(p){
  p = !p;
  return function(){
    return p = !p;
  };
};

var fl = flipflop();
console.log(fl());
console.log(fl());
console.log(fl());
console.log(fl());

Perl

->()���鷺��路����

#!/usr/bin/perl
use 5.010;
use strict;
use warnings;

sub flipflop{
  my $p = !shift;
  sub { $p = !$p }
}

my $fl = flipflop();
say $fl->();
say $fl->();
say $fl->();
say $fl->();

Ruby

call���Ѥ路����[]�Ϥ����礤��

#!/usr/bin/ruby
def flipflop(p=false)
  p = !p
  Proc.new{ p = !p }
end

fl = flipflop()
puts fl.call
puts fl.call
puts fl[]    # .call �������OK
puts fl[]

Python

Python�ε����Closure�����Τ�ʤ����ʤ�Ȥ��줬ư���ʤ���

def flipflop(p=False):
  p = not p
  def ret():
    p = not p
    return p
  return ret

fl = flipflop()
print fl()
print fl()
print fl()
print fl()

Python 3���Ȳ��Ȥ��񤱤뤬��

def flipflop(p=False):
  p = not p
  def ret():
    nonlocal p
    p = not p
    return p
  return ret

fl = flipflop()
print(fl())
print(fl())
print(fl())
print(fl())

nonlocal�������礤��

Class + Operator Overloading

Perl

�����ϤȤˤ��������ѤϤ��줬���ּ��������Τ�ʤ���

#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
{
    package FlipFlop;
    use overload
      'bool' => \&fetch,
      '0+'   => \&numify,
      '""'   => \&numify
      ;
    sub new {
        my $class = shift;
        my $self  = !shift;
        bless \$self, $class;
    }
    sub fetch {
        my $self = shift;
        $$self = !$$self;
    }
    sub numify {
      shift->fetch + 0;
    }
}

my $fl = new FlipFlop;
say $fl;
say $fl;
say $fl;
say $fl;

python

bool()�ǥ��㥹�Ȥ��ʤ���Фʤ�ʤ��Τ��Ѥ路����

#!/usr/bin/python
class FlipFlop:
  def __init__(self, v=False):
    self.x = not bool(v)
  def __nonzero__(self):
    self.x = not self.x
    return self.x

fl = FlipFlop()
print bool(fl)
print bool(fl)
print bool(fl)
print bool(fl)
print fl

ruby

puts o��o.to_s�����ۤǸƤФ��ΤȰۤʤꡢ#to_bool����������᥽�åɤ������黻�λ��˰��ۤǸƤФ��Ȥ������ȤϤʤ�����Ҥ�Yield����˻��Ƥ��ޤ���

#!/usr/bin/ruby
class FlipFlop
  def initialize(init=false)
    @p = !init
  end
  def to_b
    @p = !@p
  end
end

fl = FlipFlop.new()
puts fl.to_b
puts fl.to_b
puts fl.to_b
puts fl.to_b
puts fl && true # false �ˤʤäƤϤ���ʤ�

Yield

python

�����ϼ���������next()���鷺��路����

#!/usr/bin/python
def flipflop(p=False):
  p = not not p
  while True:
    yield p
    p = not p

fl = flipflop();
print fl.next();
print fl.next();
print fl.next();
print fl.next();

JavaScript 1.7(����)

����Firefox/SpiderMonkey�������ݡ��Ȥ��Ƥ��ʤ���

var flipflop = function(p){
  p = !!p;
  while(true){
    yield p;
    p = !p;
  };
};

var fl = flipflop();
console.log(fl.next());
console.log(fl.next());
console.log(fl.next());
console.log(fl.next());

Macro

C

�ճ��Ȥ�����?

#define flip(p) !((p) = !(p))

void main(){
  int p = 0;
  printf("%d\n", flip(p));
  printf("%d\n", flip(p));
  printf("%d\n", flip(p));
  printf("%d\n", flip(p));
}

����򵤤ˤ��ʤ���Ф���Ǥ⡣

#define flip(p) ((p)++ & 1)

void main(){
  int p = 0;
  printf("%d\n", flip(p));
  printf("%d\n", flip(p));
  printf("%d\n", flip(p));
  printf("%d\n", flip(p));
}

��ñ�ʤ��Ż��������äơ��������¿�������

Dan the Binary Blogger

�ɵ�

Perl

Re: Algorithm - 0��1�򼡡����֤���ñ�ʤ��Ż� - TokuLog ���� tokuhirom��s blog
Perl5.10 �ʤ� state ���Ĥ����뤫�顢�ʲ��Τ褦�˽񤱤�ΤǤϤʤ�����
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use Test::More;

sub flipflop {
    state $p = !shift;
    $p = !$p
}

ok !flipflop();
ok flipflop();
ok !flipflop();
ok flipflop();

done_testing;

state���򤱤��Τϡ�$p��ͭ�������ʤ����פ����singleton�ˤ������ʤ��ä����顣�פ�

my $true1st  = flipflop(1);
my $false1st = flipflop(0);

�Ȥ������ä����ȡ��Ǥ�singleton�Ǥ����ʤ�state���٥��ȥץ饯�ƥ������Ȼ��פ���

Python

@dankogai python�Ǥ�__nonzero__�ǤϤʤ�__call__����������bool�˥��㥹�Ȥ���ɬ�פʤ��ΤǤϡ� QT: 404 Blog Not Found:Algorithm - 0��1�򼡡����֤���ñ�ʤ��Ż� http://htn.to/bGWDnqless than a minute ago via web

()��ɬ�פȤϤ��������줬���֤褵����

#!/usr/bin/python
class FlipFlop:
  def __init__(self, v=False):
    self.x = not bool(v)
  def __call__(self):
    self.x = not self.x
    return self.x

fl = FlipFlop()
print fl()
print fl()
print fl()
print fl()
print fl

JavaScript

Re: Algorithm - 0��1�򼡡����֤���ñ�ʤ��Ż� - �դä�����
JavaScript �ǸƤӽФ��� () ���Ѥ路���ʤ顢�ʲ��Τ褦�˽񤱤�ΤǤϤʤ�����

�Ȥ����櫓�Ǿ������ѡ�JS�Ϥ���Ƿ��꤫��?

#!/usr/bin/js
var flipflop = function(p){
  return {
    p : 0,
    valueOf : function(){ return this.p = !this.p; },
    toString : valueOf
  };
};
var fl = flipflop();
console.log(+fl);
console.log(+fl);
console.log(+fl);
console.log(+fl);
console.log(fl);
' ).text(); } var query = {s:text}; if (lang) query['l'] = lang; var url = 'http://api.dan.co.jp/lleval.cgi'; var proto = 'json'; if ($.browser.msie || $.browser.opera){ url += '?c=?'; proto += 'p'; } /* falls back to JSONP */ $.get(url, query, function(json){ $(dstNode).html(json2table(json)).hide().show('slow'); }, proto); }; $('#mysignature').click(function(ev){ var text = $(this).text(); var query = {s:text,l:'bf'}; var url = 'http://api.dan.co.jp/lleval.cgi'; var proto = 'json'; if ($.browser.msie || $.browser.opera){ url += '?c=?'; proto += 'p'; } /* falls back to JSONP */ var me = this; $.get(url, query, function(json){ $(me).text(json.stdout).unbind('click').removeClass('bgB'); }, proto); }); })(jQuery);