8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Re: if文も三項演算子も書かないで条件分岐をスッキリさせる

Last updated at Posted at 2014-05-19

if文も三項演算子も書かないで条件分岐をスッキリさせるという投稿を見たとき「アレかな」と思ったら
そうではなかったのでそのネタで書く。

booleanなんか要らない

truefalseなんか自分で定義してしまえばいいんです、こんな感じに。
(PHPが難しすぎたので代わりにRubyで書きます。ごめんなさい)

no_if.rb
True  = ->x,y{ x }
False = ->x,y{ y }

And = ->x,y{ x[y,False] }
Or  = ->x,y{ x[True,y] }
Not = ->x{ x[False,True] }

puts True['Yes', 'No']
puts False['Yes', 'No']

puts '-' * 30
puts And[True,True]['Yes', 'No'];
puts And[True,False]['Yes', 'No'];
puts And[False,True]['Yes', 'No'];
puts And[False,False]['Yes', 'No'];

puts '-' * 30
puts Or[True,True]['Yes', 'No'];
puts Or[True,False]['Yes', 'No'];
puts Or[False,True]['Yes', 'No'];
puts Or[False,False]['Yes', 'No'];

puts '-' * 30
puts Not[True]['Yes', 'No'];
puts Not[False]['Yes', 'No'];

puts '-' * 30
True[->{ puts "LazyYes"}, ->{ puts "LazyNo" }][]
False[->{ puts "LazyYes"}, ->{ puts "LazyNo" }][]
output.txt
Yes
No
------------------------------
Yes
No
No
No
------------------------------
Yes
Yes
Yes
No
------------------------------
No
Yes
------------------------------
LazyYes
LazyNo

おわり

これで言語からtrueとかifとかの予約語を取り除くことができた。
スッキリ!

追記:
こちらもどうぞ PHPでif

8
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?