ときどきの雑記帖 再起編

最新ページへのリンク
目次ページへのリンク

一つ前へ 2012年2月(中旬)
一つ後へ 2012年3月(上旬)

ホームへ

2012年02月29日

■_

閏年
なんか閏年の2/29の扱いで不具合というのを見かけましたが、あるエピソードを一つ。 閏年の判定というのは西暦年が4で割り切れるというだけでなく 100でどうとか400でどうとかありますよね。 で、1990年代に開発された製品の中に この閏年の判定をただ単に4で割り切れるかどうかでしかやっていなかったのだけど、 2000年が400で割り切れるために手抜きの実装(バグ)が発覚しないですんでしまった ものがあったとかなかったとか。 それとは別に、なぜか100での判定はやってるのに400ではやってなくて以下略なものとか。

■_

Pythonのお勉強 Part46

66 デフォルトの名無しさん [sage] 2012/02/29(水) 08:31:09.46 ID: Be:
    >>> u'%10s' % (u'abc')
    u' abc'
    だとスペース7個のあとに「abc」が表示されますが
    >>> u'%10s' % (u'日本語')
    u' \u65e5\u672c\u8a9e'
    だとやっぱりスペース7個のあとに「日本語」が表示されます
    >>> u'%7s' % (u'日本語')
    u' \u65e5\u672c\u8a9e'
    結果としてはこちらを期待している訳ですが
    「%7s」(日本語の方の文字列長に依存するので)にしないといけない
    のが納得逝きません
    これはバグでしょうか仕様でしょうか 

69 デフォルトの名無しさん [sage] 2012/02/29(水) 08:49:36.13 ID: Be:
    知らんけど仕様じゃないの?
    しかもPythonに非はないと思うよ。
    文字一つを表示した時に二文字分か一文字分かなんて、
    fontによりけりになるんじゃないの?
    unicodeではそこらへん規定してるのかなー? 

70 デフォルトの名無しさん [sage] 2012/02/29(水) 08:50:24.15 ID: Be:
    コンソールアプリを書いてて
    右揃えで綺麗にテーブル表示したかったんです 

71 デフォルトの名無しさん [sage] 2012/02/29(水) 09:06:21.92 ID: Be:
    asciiの時は空白分を加算して、
    13s
    にするとかじゃないと対処できないと思うよ。
    Pythonで対処するのは無理だと思う。
    専用の関数を自作するしかないんじゃないの?

    unicodeは何byteで一文字を表現するかを規定するだけで、
    一文字を何文字分の幅で表示するかはfontの仕事。

    それか、asciiを全角表示するように変換するとかかな?
    'a': 'a'
    みたいなのをずらずら作っていくのが、
    作業量的にも技術的にも、一番簡単かつ現実的な解決方法だと思います。 

72 デフォルトの名無しさん [sage] 2012/02/29(水) 09:11:05.26 ID: Be:
    やっぱり完璧な解決方法じゃないな。
    ハンカク文字ってのがいたよ。
    でもま、ちょろちょろっと作るscriptなら、
    変換table用意するのが楽だと思うよ。 

73 デフォルトの名無しさん [sage] 2012/02/29(水) 09:40:46.04 ID: Be:
    てきとうに作ったらキモくなった
    isascii = lambda c: c in u''.join(chr(x) for x in xrange(0x20, 0x7f))
    u = u'アあa'
    '%*s' % (10 - len([x for x in u if not isascii(x)]), u)
    u' \uff71\u3042a'
    ちょっと後悔している 

76 デフォルトの名無しさん [sage] 2012/02/29(水) 11:10:37.90 ID: Be:
    教えてほしいんですが、文字が全角幅か半角幅かはどうやって判断するのがいいのでしょうか。
    フォントに依存するというのはなしで。 

77 デフォルトの名無しさん [sage] 2012/02/29(水) 11:11:11.01 ID: Be:
    フォントに依存する。

78 デフォルトの名無しさん [sage] 2012/02/29(水) 11:29:29.80 ID: Be:
    2byteのaもあったな 

79 デフォルトの名無しさん [sage] 2012/02/29(水) 11:47:05.97 ID: Be:
    u = u'アあa'
    u'%*s' % (10 - len(filter(lambda x: ord(x) > 127, u)), u) 

80 デフォルトの名無しさん [sage] 2012/02/29(水) 12:18:04.03 ID: Be:
    >>> import unicodedata
    >>> unicodedata.east_asian_width(u'あ')
    'W'
    >>> unicodedata.east_asian_width(u'a')
    'Na'
    >>> unicodedata.east_asian_width(u'a')
    'F'

    http://ja.wikipedia.org/wiki/%E6%9D%B1%E3%82%A2%E3%82%B8%E3%82%A2%E3%81%AE%E6%96%87%E5%AD%97%E5%B9%85 

82 デフォルトの名無しさん [sage] 2012/02/29(水) 14:17:10.78 ID: Be:
    >73 >79-80
    ありがとうございます
    そのあたりを参考に自作してみます

    元々は
    print u' name length size %'
    for r in lst:
    print u'%8s%8d%6d%4d' % (r.name, r.l, r.s, r.p)
    みたいなテーブル表示をしてて
    名前に日本語が混ざってるとずれてくるので
    変だなーと思った次第です
    日本語使わなければ済む問題なんですが

83 デフォルトの名無しさん [sage] 2012/02/29(水) 14:56:23.62 ID: Be:
    east asian widthがambiguousなケースは要注意(キリル文字とか)
    そのような文字をどう表示するかは本当に環境やソフト次第なので…
    「それらの文字を日本語フォントで表示するような環境なら」
    全角扱いにすべきだが、例えば端末上で動作するようなソフトウェアは
    端末がどのような文字をどのようなフォントでレンダリングするつもりなのか
    知りようがない
    なので、例えばvimのようなソフトウェアは、そうした文字を全角扱い
    するかどうかをユーザが設定で指定できるようになっている 

結構面倒な問題だったりする>表示幅の取得

■_

ボツ1

■_ Rakudo

最近、仕様に対する実装の比率というのをどこかで見かけた覚えがあるのですが 思ったより低かったような? 仕様が大きすぎるのかなんなのかはわかりませんが、 道のりはまだまだ厳しそうですねえ…

Rakudo Star 2012.02 out – and the focus for 2012.03 | 6guts

Rakudo Star 2012.02 out – and the focus for 2012.03

Posted on February 29, 2012

I've just cut the February 2012 release of Rakudo Star. The good news is that we got a 
bunch of nice improvements into the release.

    More typed exceptions, and better backtraces. This work has been done by moritz++; I'll
    simply point you to his excellent blog post on that topic today. I think everyone
    working with Rakudo will appreciate the new backtraces, which cut out a lot of the
    uninformative details and are thus much more focused.

    さらなる typed exception (型付き例外?) とバックトレース

    FatRat is now implemented. This type gives you rational number support with big integer
    semantics for both the numerator and denominator. The Rat type now correctly degrades to
    a Num if the denominator gets too large, as per spec (this avoids performance issues in
    certain cases). Again, we've moritz++ to thank for this.

    FatRat の実装

    We have object hashes! You can now make a declaration like:

    my %hash{Any};

    Which will have keys of type Any. You can constrain this as you wish. You can also 
    still constrain the value in the usual way:

    my Int %hash{Any};

    This latter example gives you a hash with Int values and object keys, meaning that keys
    are not coerced to strings, as is the default. Thus, using %hash.keys will give you back
    the original objects. Identify is done based on .WHICH and thus ObjAt.

    The coercion syntax, Int($foo), is now implemented. This gives you another – perhaps
    neater – way to write $foo.Int, but types may also implemented the method
    postcircumfix:<( )> if the target type is the one that knows how to do the 
    coercion.

    強制型変換 (coercion) 構文

    The reduction meta-operator used to have crazy bad performance, and parsed dodgy in some
    cases. Both of these issues are now resolved; it's an order of magnitude faster and
    parses more correctly.

    Various regex improvements and more problems caught at compile time, which I discussed
    in my previous post.

■_

ボツ2

■_

2012年02月28日

■_

なんで C の入門書で「参照渡し」の解説があるんですかね。 しかも C にそれがあるという前提で。 アドレスを渡して、それを * で dereference するやり方が 「参照渡しと言われている」って自分たちで言ってるだけじゃあ…

■_ What is a scripting language?

What is a scripting language? : Python

What is a scripting language? (self.Python)


I've heard python referenced as a scripting language, I was just hoping someone could give
me a rough definition on what it means, and how it differs from any other type of language.

There's no real definition. Loosely speaking, scripting languages are languages that don't
need to be compiled and can be used for system administration, but nowadays, Python can be
used for much more than that, so it doesn't really fit as a name.

There's a classic definition of a scripting language as a language used to control the 
execution of some other [normally machine language] program. In that sense, a scripting
language is a language used to script the runtime behavior of a program.

An early, common use for such languages was to allocate datasets¹, invoke the system's 
program loader, and begin execution. At one time, this "obvious, easy" task was
a manual process, so little script languages were created to automate the work. That's
where the "used for system administration" bit comes from. The interpreters for
these script languages evolved into interactive session programs (shells, "the command
line"), and the languages grew lots of features, becoming quite powerful.

The benefits of writing whole "programs" (as opposed to just a subset of programs
that might be called "control programs" or "scripts") in these
scripting languages became widely evident, so scripting languages informally divorced
themselves from program loaders, and eventually grew up into general purpose languages.
 

combine earthboundkid's answer with obtu's below.

Obtu notes another key aspect of a scripting language:

    has little overhead for the programmer: there is no minimum boilerplate (a script can
    be just one line), it can be used interactively, it can be run immediately


apropos, this is a great essay on the subject: http://www.perl.com/pub/2007/12/06/soto-11.html


Came here to say that. It was written with Perl in mind, but applies to Python just as well.


Some people say that there is no such thing as a scripting language, that it is too much
of an arbitrary thing.


Scripting is mostly a use case. A language that is good at scripting is a language that
has little overhead for the programmer: there is no minimum boilerplate (a script can be
just one line), it can be used interactively, it can be run immediately, it is already
installed, and it doesn't require special tools beyond a standard editor and a single
command.


Scripting languages are languages which are interpreted at run time opposed to 
compiled prior to execution.

That is the short and sweet.

これもバイク小屋の議論のタネになるんすかね。

■_

わりとバラエティにとんでて面白かった。

What's your favourite language besides Python? : Python

What's your favourite language besides Python? (self.Python)


I've got some time to look into another language, and I'm wondering what other 
people's favourites are. Functional and imperative languages are both welcome.

Though I think Python is far from perfect, I can't help but feel that it is much more 
readable than other languages. I look at Java code and there's so much boiler plate, 
and when I look at Lisp variants, I usually get lost in the parenthesis.

Right now I'm leaning towards Ruby. It seems like a lot of developers learn both 
Python and Ruby, and each language definitely has unique benefits.

What are your thoughts r/python?


Haskell - it's just mind blowing... I still don't think I fully understand it, but I feel
like some kind of wizard when I write code in it that just works first time. When I first
felt like I basically understood the laziness and how the type system worked, it was like
that scene from the matrix: "all I see now is blonde, brunette, red-head..."


C. It's simply beautiful and it nicely complements Python.


C used to be my favorite before Go came around :)


Lua. It's the only other one I've tried that I go back to and try to learn more.


Clojure. It pays to learn something different from Python, Ruby is too similar.


Javascript :P Ok not really, but minus the whole DOM mess and === it's actually a 
pretty cool language.

Seriously tho, Pascal was my favorite before python, I guess it still is. But I don't 
code in it anymore, I only code in Python or Javascript these days. Tho honestly, I'd 
rather that IE, Chrome, FF and Opera just put python in the browser and gave it a nice 
API to the DOM, and we can be done with the historic horrible baggage that is the 
Javascript DOM api. But that's just wishful thinking :P

Go, It's like a static, complied version of Python with type based interface and channel


Scala - It grants me type safety when I want it while also having all the niceties of 
a modern language such as list comprehensions, lambda functions, filters, etc... Plus 
it runs on the JVM, so it can interoperate with Java code.

Coffeescript. It still needs some time to mature (e.g. it's error messages are 
terrible) but it's beautiful and practical.

Coffeescript + jQuery = browser coding bliss.

ObjC. I see it like this. Points on the same line, it's just that ObjC is way close to 
C. But both gaining abstraction and powerful concepts.

| C ---> ObjC --------------------------> Python |

There are interesting overlaps in philosophy. And you can hold your programs in your 
hand and touch them. That's incredibly gratifying.

With regards to Ruby, I'd suggest against it. (Or at least, something else first.) I 
would learn something that lets you do something Python doesn't do very well (ie. 
program on an iPhone.) Python and Ruby are fairly interchangeable for many purposes. 
It comes down to personal preference, but you'll mostly do web and scripting stuff 
with both.
Prolog.


erlang 'cos it makes me realise there are many ways to implement a solution.


C++. It's powerful, fast and fun to use, especially with C++11. Not as fun as Python though :)


Ada.

■_ C+11

すぽすっぽセンセイがいろいろお答えに。 そこからひとつ。

Stroustrup reveals what's new in C++ 11 | Application Development - InfoWorld

InfoWorld: At Microsoft's GoingNative 2012 conference recently, you emphasized native 
programming, saying, "Something has to talk to hardware," and not everything 
can be a virtual machine. When should a developer opt for native programming, and when 
should a developer opt for a virtual machine-based language?

Stroustrup: Actually, it was Microsoft that emphasized "native" programming and
chose the title, but that's the kind of implementation techniques I've relied on for decades.
C++ has significant strengths compared to "virtual machine-based languages" when
it comes to building infrastructure. In other words, where performance, reliability,
resources, and complexity need to be tightly controlled.

For example, you wouldn't write a JavaScript engine in JavaScript, and you probably wouldn't
write a "first to market" simple Web app in C++. You would write the foundations of
a Google, an Amazon, a Facebook, or an Amadeus (airline ticketing) in C++, but maybe not the
rapidly changing top layers of such systems. C++ comes in strong where power consumption is
an issue -- for example, server farms and handheld devices. Naturally, C++ can be
competitive even where performance isn't a critical issue, but there the choice will be
made more on the availability of libraries and developers than on the languages themselves.

JavaScript 云々てのをちょっと前についったで見かけたのだけど これが元発言か。

■_

Day 1 Keynote - Bjarne Stroustrup: C++11 Style | GoingNative 2012 | Channel 9

■_

■_

中途半端に訳しているのがいくつかあるんだけど、切りのいいところまでやる余裕が

2012年02月27日

■_

屡々
昨日わからないと書いていたのはこの字(の旧字)でした。 「しばしば」と読みます。屡の一字でもそう読むみたいですが。

疑惑w
この人、ひょっとしてアシモフファンじゃろか breuleux/terminus - GitHub

■_ Fiber

Rupert Blog: Everything You Always Wanted to Know About Fibers * But Were Afraid to Ask

Rupert Blog

Everything You Always Wanted to Know About Fibers * But Were Afraid to Ask
あなたが Fiber について知りたいと思っているけれども訊くのが怖いようなことすべて

I recently found out that most Ruby developers that are not familiar with Event Machine
usually knows very little about fibers. So I decided to share some very trivial examples
that will help you understand them quickly. Each example should push your knowledge and
imagination about possible usages a little further. Tell me in comments if that worked
for you.

最近わたしは、Event Machine に馴染みが余りないような Ruby developer たちの大部分が
fiber についてほとんど知らないことに気がつきました。そこで fiber を手っ取り早く
理解するのを助けるような very trivial な例をいくつか共有しようと考えたのです。


Example 1:

  > f= Fiber.new { puts "x" }
  > f.resume
  x
   => nil
  > f.resume
  FiberError: dead fiber called

What happened here, you may ask. We created new fiber. At the begining it was stopped. So
we resumed it. It executed and ended. Trying to resume it again did not work. Actually that
was very similar to Proc except that we can always call Proc multiple times.

Example 2:
以下略

■_ reddit に訊け

100 年後にもHaskellは使われているだろうか? 的な

Do you think people will still be writing Haskell code in 100 years? : haskell

Do you think people will still be writing Haskell code in 100 years? (self.haskell)


People have been writing Haskell code for a little over 20 years already. People have been
writing code in C for over 40 years. LISP and Fortran for over 50. Do you think people will
be sitting down to write new Haskell programs 100 years from now?


The Lisp people write now is very different from the Lisp that people wrote 40 years 
ago. It's like saying that people still write Algol 50 years later because JavaScript 
syntax looks vaguely like Algol.

I predict that in 100 years, people will still be writing in Algol-syntaxed languages, 
but we'll think that higher-order functions, combinator libraries, and partial 
application are as basic building blocks as arithmetic operators and function calls.

The Haskell code people write now is very different from what they were writing 10 years
when I started: Hierarchical modules, GADTS, associated types, enumerators/conduits/pipes,
ByteStrings/Text, and a bunch of other things that I have forgotten..

And, on the horizon is more dependent type inspired functionality, data parallel haskell, etc.

A C programmer from 40 years ago could probably make sense of most modern real world C code ―
I wonder if Haskell programmers of today would be able to make sense of Haskell code from
20-40 years in the future.. (Without an updated manual, of course).

I don't know for Haskell, but in 100 years maybe C++ will have at last standardized garbage collection...

■_

■_

しまった。ジュンク堂池袋店での暗黒通信団フェアが終わってしまうではないか。

2012年02月26日

■_

昨日買った対数表、序文がこんなだった

昭和26年12月25日発行・昭和35年9月15日第13版発行
昭和36年10月10日第14版発行
昭和39年2月10日第14版第7刷発行
序 段々自然認識が正確になり学問が精密になるに従って物事を数量的に 表す必要が次第に高まってくる。 斯様な場合に先人の労作になる函数の数値表が利用できることは何事にも増して 有難いものである。たゞ使う側から言ってそれがなるべく見易い様に、 結果の出し易い様に排列され印刷されていることが望ましい。 本書に収められたものは対数表、三角函数表等既に広く知られ利用されているものに限られている。 相当に精密な数値計算には7桁表が役立ち、 実験値に関する処理には5桁表が便利であろう。 たゞ在来のものは不鮮明なる複写が主であったため使う際余り愉快ではなかった。

丸善出版部ではこの欠点に鑑み且つ洋書輸入の不可能なる今日であるので 定評のある数値表を適当に排列し鮮明に印刷することを企図せられた。 然し複写と違い新たに組み直す以上絶対正確なるべき校正が要求せらる、 そのことに要する努力の多大なるべきは容易に推察される所である。 筆者もかヽる表の責任として保有すべき絶対正確性を@@要求したのであるが、 出版部ではあらゆる努力を注がれてこれ以上望み得ないと思った次第である。 然しよく言われる様に「校正恐るべし」 で神様でない限り絶対に誤植がないとは保証できないわけであるが、 筆者は本書の優秀なる出来栄を心から期待し且つ長期にわたって我国学界に貢献することを祈念する次第である。
昭和十八年七月七日

なんと戦争中に作られたのが最初だったとは @のところは字がわからず。旧字なのか、オンラインの漢和辞典では見つけられなかった。

Right versus pragmatic – Marco.org

■_

「技術的負債」の話。

Building Real Software: Technical Debt - How much is it Really Costing you?
Building Real Software 

Developing and Maintaining Secure and Reliable Software in the Real World
Monday, February 13, 2012

Technical Debt - How much is it Really Costing you?

The idea behind the technical debt metaphor is that there is a cost to taking short cuts
(intentional technical debt) or making mistakes (unintentional technical debt) and that
the cost of not dealing with these short cuts and mistakes will increase over time.

technical debt (技術的負債) のメタファーの背後にある考えとは、
ショートカット(意図的な技術的負債のこと)したり間違いを犯す(意図していない技術的負債)は
コストがかかるものであり、それは対処しないと時間がたつにつれて増大していくというものです。

The problem with this metaphor is that with financial debt, we know how much it would cost
to pay off a debt off today and we can calculate how much interest we will have to pay in
the future. Technical debt though is much fuzzier. We don't really know how much debt we
have taken on – you may have taken on a lot of unintentional technical debt – and you may
still be taking it on without knowing it. And we can't quantify how much it is really
costing us – how much interest we have paid so far, what the total cost may be in the
future if we don't take care of it today.

このたとえの問題点は、financial な負債を使ってしまっていることです。
finalcial な負債であれば、今日どれだけの負債があり、将来どれだけ支払わなければならない
のかがわかります。しかし技術的負債はもっと fuzzier です。
実際にどれだけの負債があるのかをわたしたちは知りません。
あなたには多額の意図せざる技術的負債 (unintentional technical debt) があるでしょうし、
and you may still be taking it on without knowing it.
そして、どれだけのコストがあるのかを計算することもできないのです。
今日どれだけの負債を抱えているのかがわからなければ、
将来どれだけ支払わなければならないのかもわかりません。
#かなりむりやり


Some people have tried to put technical debt into concrete financial terms. For example,
according to CAST Software's CRASH report


 “applications carry on average $3.61 of technical debt per line of code”.

For some reason, the average cost of Java apps was even higher: $5.42 per line of code. 
These numbers are calculated from running static structural analysis on their customers' code.

略

■_ Opcode Tables

力作。 OpcodesTables

Opcodes' tables (instruction set maps) of Android, Java, .Net, x86, x64 : programming

No complete version of x86/64? Otherwise quite nice.

I'm not really sure that you want a complete map of x86/64... it's naturally CISC and the
Intel manual is 4000 pages with close to 600 pages just for instructions. Now-a-days almost
every x86/64 processor uses a CISC-RISC translator and compilers optimize out most of the
CISC-like instructions that tend to get translated into something really different.


PHP's opcodes, if anyone is interested. It's not complete, though I might go in and 
update the list.

Edit: Here is the updated list, which describes all currently-defined opcodes.


i didn't know php had a VM. is it worth making a map of it ?

ruby 1.9's https://github.com/ruby/ruby/blob/trunk/insns.def

■_

Ruby にかかわる Hero にインタビューとか。 Java でありましたよね

Interview With Konstantin Haase » RubySource

Q: How did you get into programming and web development?

    I did a lot of non-web programming for a long time, without ever publishing code or
    getting payed for doing it. When I was about ten years old, I started programming in
    Basic and Pascal. My main motivation: My computer sucked, so I couldn't play all those
    fancy games my friends in school were playing. I somehow [stuck] with it.

Q: How did you discover Ruby?

    Mainly due to Why's (Poignant) Guide to Ruby

Q: What do you think sets Ruby apart from other languages? Is it your favourite language?

    Define favorite language. It's far from perfect. It's not the most consistent, nor clean,
    nor expressive, nor performant language. However, it's comfy (I like expressing myself
    in Ruby) and has an amazing community.

Q: What plans do you have for the future of Sinatra? How do you see it developing?

    Stability, performance, scalability, simplicity are the main areas where Sinatra 
    already shines and will continue to shine. I also have high hopes for the sinatra-contrib
    project. There will be some changes internally for the successor of Rack, which Sinatra
    will of course support, but it's all too early to really talk about.

Q: How do you find the time to contribute so much to the Open Source community?

    Why do people ask always ask me that? I don't know, I just make the time. If you have
    the urge to write some awesome fun code just for the sake of doing it, just do it. I
    don't really have a solution for this other than that. Maybe earning a little less this
    month by spending less time on your job is acceptable for that. If that's not possible,
    maybe it would be if you had a different job. Maybe it's fine if you don't know if
    you'll have enough money to pay the bills in a few months from now, you probably will.
    If you do awesome work, there will be people paying you for it. Maybe not learning for
    exams is an option. I don't own a TV, nor do I play video games, but I don't necessarily
    do that on purpose, working on OSS just gives me more pleasure. I prefer spending my
    time on things I love. That includes spending time with my girlfriend or going out with
    friends, but it also includes solving the problems I love to solve instead of solving
    some problems for some company that I don't care about, probably don't even understand.
    Choose your problems. Does this even make sense? Scratch that, I have no idea how I make
    time. And to counter widespread rumors: Yes, I do sleep.

© 1998-2012 SitePoint Pty. Ltd. All Rights Reserved.

わたしが興味を持ったところを抜き出したので、 余裕があれば元記事を読むのを推奨 :)

最後の回答は訳したほうがいいかしらん

■_

んで、ぺちぺの問題のコード。 flex のコードか。yytext に加算してたりするけど、 これ flex じゃない lex だと問題にならなかったっけ?

[svn] Diff of /php/php-src/branches/PHP_5_2/Zend/zend_language_scanner.l

1255 	  	 <ST_IN_SCRIPTING>{HNUM} {
1256 	  	     /* Skip "0x" */
1257 	  	     yytext += 2;
1258 	  	     yyleng -= 2;
1259 	  	 
1260 	  	     /* Skip any leading 0s */
1261 	  	     while (*yytext == '0') {
1262 	  	         yytext++;
1263 	  	         yyleng--;
1264 	  	     }
1265 	  	 
1266 	  	     if (yyleng < SIZEOF_LONG * 2 || (yyleng == SIZEOF_LONG * 2 && *yytext <= '7')) {
1267 	  	         zendlval->value.lval = strtol(yytext, NULL, 16);
1268 	  	         zendlval->type = IS_LONG;
1269 	  	         return T_LNUMBER;
1270 	  	     } else {
1271 	  	         zendlval->value.dval = zend_hex_strtod(yytext, NULL);
1272 	  	         zendlval->type = IS_DOUBLE;
1273 	  	         return T_DNUMBER;
1274 	  	     }
1275 	 }

SIZEOF_LONG がよくわからない(面倒なので定義を調べもしないw)けども、 strtol がうまいこと + を解釈してしまってると。 さらに yyleng が 0 になってしまっているので 再度 + と 0x02 を解釈したので 2 + 2 で 4 と。

■_

■_ 読んだ

閉じこもるインターネット――グーグル・パーソナライズ・民主主義

面白かった。 といいいつ、内容はちょっと怖いところもあるのだけど。 たとえ悪意がなかったとしても 「パーソナライズ」された「バブル」に閉じ込められるかもしれないよ。と (おお、なんというまとめ)

2012年02月25日

■_

バグとかソフトウェアの評価の話
んでまあ、いろいろ調べた結果、この本をいちど読んでおかねばなるまいということで買ってきました。
品質と生産性を重視したソフトウェア開発プロジェクト技法―見積り・設計・テストの効果的な構造化

まあなんというか、デマルコさんすげーやというか 自分でぐだぐだ考える前にこの本の内容を抑えておくべきだと思ったわけですね (この本の冒頭にあるフレーズは広く知られていて、でも後に本人がそれを否定する発言してたりしてます)。 ASCII.jp:「効率化しすぎると、作業が遅くなり変化にも対応できない」とトム・デマルコ氏――“デブサミ2004”開幕

それと BugStories にあるような珍しい(難しい)ものでない 「ふつー」のバグの調査もやってみたいのですよね。 Knuth が TeX のバグで書いてたりしてますがあれともちょっと違うかな。

↑の本に行き着くまでにこういう本が目に入ってきて非常に困った( 要するに買う金やら読む時間が以下略) 丸善&ジュンク堂書店 世界鉄道史 丸善&ジュンク堂書店 同人音楽とその周辺 丸善&ジュンク堂書店公式サイト 武器の歴史大図鑑 予約ページ 丸善&ジュンク堂書店 図書館の主 1

神保町へ行って、高岡書店とか明倫館書店とか三省堂など回ってきたのですが 「本と街の案内所」 - ナビブラ神保町 にも寄ってみました(実は今まで入ったことがなかった)。 というのも、ここにはいろいろな電子書籍のリーダーが置かれているからなのですね。 やっぱりA4サイズの本をスキャンしたPDFを読もうとすると iPad くらいの大きさが欲しいかなあ(初代の Kindle も結構大きかった)。 でも意外に重いのねアレ。 本と街の案内所公式ブログ

kindle 4 や touch ではPDFを読むのにはいろいろ無理を感じるのがわかったので 早いところどうにかしたいなあ、でないと買うに買えないものがw (PCで読むことはまったく考えていないという) オーム社eStore(β)

買った
スーパーダッシュ&ゴー! 2012年 04月号 [雑誌]
スーパーダッシュ&ゴー! 2012年 04月号 [雑誌]
R.O.D. の新シリーズ? 目当て。

■_ ぐるぐる

無限ループになるのね、これ。 読むまで気がつかなかった。

Increment number to overflow : javascript

What happens when I do

  var x = 1;

  while(!!x) ++x;

I read up somewhere that an int overflow becomes a float, will there be a type conversion after that?

Related: How big is an int and float in javascript?


JavaScript does not have integers. All numbers are type Number, which is a floating point
type (IEEE 754 double precision). IEEE doubles can represent values up to (2 - 2^-52 ) * 2^1023
which is approximately 10308.3 . However, that does not mean it can represent every integer in
that interval. If you let that loop run for a long time it would eventually become Infinity,
and !!Infinity is still true, so the loop would continue. Infinity + 1 is still Infinity so it
would be an infinite loop. (But the loop would never get there, it would take decades or
centuries for a computer to enumerate all of the positive IEEE double values, as that is
essentially counting from 0 to 2^62 . Even if you could count at a rate of 10 billion per
second, that would still take 14.6 years.)


Thanks! This answers my question completely.
  var x = 1;
  for (var y = 1; y < 1030; y++) {
      x *= 2;
      console.log(x);
  }

Way to give it the old college try.

■_

WEB+DB PRESS SQL

■_ 文字列の表現形式

2冊目の著書「本当は怖いC言語」の見本本到着!:mtaneda ブログ:ITmedia オルタナティブ・ブログ のコメント欄

2冊目の著書「本当は怖いC言語」の見本本到着!:mtaneda ブログ:ITmedia オルタナティブ・ブログ

コメント
今関 	2012/02/25 22:52

本の内容はわかりませんが、Cといえば暴れ馬の言語ってイメージあります。自己責任できちん
と使わないといろいろな使い方&結果が待っていると教わりました。私は無難にPascalで...

mtaneda 	2012/02/25 22:55

今関 様

コメントありがとうございます。
そうですね、C言語は正しい知識がないと、何が起きるかわかりません。
本書は、そのあたりを隠さず、失敗しながら学んでみましょうという感じで書いてみました。
わざとバグらせてみたり(笑)

Pascalは文字列のデータ構造が特徴的ですね。

いわゆる Pascal 文字列ってわりとポピュラーな気がするんですけどそうでもない? 各種文字列の実際 長さを表すフィールドの大きさにいろいろあるとかいう話ではないですよねえ… >特徴的

8bit 時代の「マイコン」にROMで載ってた(あー、クリーンコンピュータとかはとりあえずおいといて) BASIC でも長さ+実体だったという記憶が。 長さを表すのに1バイトしかなかったのでいろいろ苦労する場面があったり。

■_

例によって reddit でちょっと古いエントリが取り上げられたというパターン

Don't Fall in Love With Your Technology

(略)

It's bizarre to realize that in 2007 there were still people fervently arguing Emacs 
versus vi and defending the quirks of makefiles. That's the same year that multi-touch 
interfaces exploded, low power consumption became key, and the tired, old trappings of 
faux-desktops were finally set aside for something completely new.

Don't fall in love with your technology the way some Forth and Linux advocates have. 
If it gives you an edge, if it lets you get things done faster, then by all means use 
it. Use it to build what you've always wanted to build, then fall in love with that.

あれ、これを取り上げてた reddit のスレ(?)どこだ

■_ R7RS

あとでよむ

SixRejection – Scheme Working Groups

These are condensed and anonymized  objections to R6RS by the people who voted against it,
with comments underneath each that show how R7RS meets (or fails to meet) that objection.
Statements about R7RS-large reflect WG votes, but are subject to change. This document is
still being edited. All errors and misstatements not present in the original comments are
Cowan's responsibility.

The order of the objections given here follows the order of the R7RS-small report as much
as possible, which follows the R5RS report as much as possible.

This document is explicitly a work of R7RS advocacy. Its purpose is not to attack R6RS, but
to explain why people who rejected R6RS at the time of ratification may find R7RS more to
their liking (and should therefore vote for it).

General
以下略

■_

■_ 今日の購入物

明倫館書店で購入。200円。 なぜ買ったと訊かれても答えに困るんですが、つい(笑)

何のためにこういうものがあるのかわからない(理解できない)人も多いでしょうね。 って自分もほとんどつかったことないんですが (統計学の講義の最初の頃にちょっとだけ使った)。

2012年02月24日

■_

ソフトウェアの「品質」
などということを会社でごにょごにょしているんですが、いろいろ面倒。 オープンソースと私権ソフトのコードクォリティをCoverity Scan 2011が比較 バグの数というのもひとつの目安ではあるんでしょうけど、 なにをどうすればソフトウェアの「質」を「評価」できるんでしょうか。 つぶしたバグの数や合格したテストの数? それもなにか違う気がするのですよね。

買った
栄光なき天才たち2011 (栄光なき天才たち)
栄光なき天才たち2011 (栄光なき天才たち)
今回は一巻かけて一つのエピソード。 とある日本の自動車メーカーのお話(といっていいんだろうか)なんですが、 ラストにはやりきれないものが。 であるからこそ「栄光なき~」で取り上げられる題材なんでしょうけど。 一巻かけて一つのエピソードというと鈴木商店の話もあったなあ。 「栄光なき天才たち」全部を読み返したいんだけどどうにかならんもんかしら (文庫ででたのも一部のエピソードだけだし)。 古いせいか漫画喫茶でも見ないのよねえ。

中畑監督インタビュー
とあるニュース番組で観ました。 思ったよりは考えて行動しているんだなあという印象だったのですがさて。

■_ D

D の記事が。 Inheriting Purity | Dr Dobb's

Inheriting Purity | Dr Dobb's
Inheriting Purity

  February 22, 2012

In the D programming language, functions can be specified as pure with an attribute:

D では、関数に対してその属性が pure であるように指定できます

  pure int foo() { ... }

and the compiler will check that, indeed, the body of the function foo() does not do 
anything that violates purity:

そうするとコンパイラーは関数 foo() の本体が purity を破るようなことをしていないか
チェックします。

 no reading or writing of global mutable data
 (グローバルで mutable なデータの読み書きをしていない)
 no calling impure functions
 (imupre な関数を呼び出していない)

In other words, the return value of a pure function will be the same if its arguments 
are the same. Pure functions are an essential characteristic of functional programming, 
and their value is widely recognized. So far, so good.

言い換えると、pure な関数では引数が同じであれば同じ戻り値になるのです。
pure な関数は関数プログラミングの essential characterritic (本質的な特性?) であり、
その価値は広く認められています。

Now, consider a class B that derives from class A and overrides one of its functions:

さて、クラス A から派生したクラス B と、関数をひとつオーバーライドすることを考えてみましょう

  class A {
    int foo(int i) { ... }
  }
  class B : A {
    override pure int foo(int i) { ... }
  }

It overrides an impure function A.foo with a pure function B.foo. But wait, don't they 
have to be the same type; i.e., either both pure or both impure?

この例では、pure な関数 B.foo で impure な関数 A.foo をオーバーライドしています。
でもちょっと待ってください。これらは同じ型ではありません。
つまり、両方ともが pure でもないし、両方ともが impure でもないのです。

以下略
 
  

■_ PHP can't add hex numbers

すでに各所で反応している方々がいらっしゃいますがそれはおいといて。

PHP :: Bug #61095 :: PHP can't add hex numbers

 [2012-02-15 15:32 UTC] tomek at przeslij dot pl

Description:
------------
These echoes 4:
echo (0x00+2);
echo (0x00+0x02);
but they should echo 2! This echoes 2 as expected:
echo (0x00 + 2);

Test script:
---------------
echo (0x00+2);

Expected result:
----------------
2

Actual result:
--------------
4

 [2012-02-15 15:54 UTC] anon at anon dot anon

That is fan-tastic!

http://i.imgur.com/uPC2b.gif

 [2012-02-15 16:24 UTC] nikic@php.net

Nice catch!

This can be fixed by adding a len == 0 check after the while loop in
http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_language_scanner.l#1515 (and returning 0 in that case).

 [2012-02-23 15:55 UTC] lepidosteus+phpbug at gmail dot com

Correct behavior for me in 5.3.3-7+squeeze3

$ php -r "var_dump(0x02+0x00);"
int(2)
$ php -r "var_dump(0x02+0);"
int(2)
$ php -r "var_dump('0x02'+0);"
int(2)

 [2012-02-23 15:56 UTC] lepidosteus+phpbug at gmail dot com

Sorry, forget my previous comment, I misunderstood the issue

 [2012-02-23 20:14 UTC] phpbug at vincent dot sh

Lexer level ? Russian. http://goo.gl/eqxZ8

 [2012-02-23 20:40 UTC] balthasar dot reuter at cs dot fau dot de

Behavior reproduced:

$ php -r 'echo (0x00+2);echo "\n";'
4
$ php -r 'echo (0x00+ 2);echo "\n";'
2
$ php -r 'echo (0x00 + 2);echo "\n";'
2
$ php -r 'echo (0x00 +2);echo "\n";'
4
$ php -v
PHP 5.3.8 (cli) (built: Dec 5 2011 21:24:09)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies

Mac OS X 10.6.8

 [2012-02-23 21:55 UTC] jordan dot sherer at definition6 dot com

Reproduced, but after adding spaces around the + it works as intended.

$ php -r 'echo (0x00+2);echo "\n";'
4

$ php -r 'echo (0x00 + 2);echo "\n";'
2

$ php -v
PHP 5.3.8 with Suhosin-Patch (cli) (built: Nov 15 2011 15:33:15)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies

 [2012-02-23 22:53 UTC] yk4ever at gmail dot com

It is already fixed in the trunk and relevant version branches.

 [2012-02-24 01:16 UTC] admin at jesus dot de

NOT reproduced in older version:

# php -r 'echo (0x00+2);echo "\n";'
2
# php -r 'echo (0x00+ 2);echo "\n";'
2
# php -r 'echo (0x00 + 2);echo "\n";'
2
# php -r 'echo (0x00 +2);echo "\n";'
2
# php -v
PHP 5.2.6-1+lenny13 with Suhosin-Patch 0.9.6.2 (cli) (built: Jul 1 2011 17:03:36)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies

 [2012-02-24 02:34 UTC] colder@php.net

This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/

Thank you for the report, and for helping us make PHP better.


PHP Copyright c 2001-2012 The PHP Group
All rights reserved. Last updated: Fri Feb 24 05:30:02 2012 UTC

どこをどうすれば 4 がでてくるんだろう…

■_

で、reddit での反応。 脱線するのはいつもの通りですが、結構面白いものが。

にしても、なにをどうやれば 4 という結果になるのか不思議だったのですが (コードは追いかけなかった)、なるほどこういう動作だったのね。 + 以降を二回スキャンしてたんだ。

PHP adds hexadecimal numbers incorrectly : programming

For the curious but lazy: it's the lexer. Dealing with "0x00+0x02" it skipped the 0x00,
strtol'ed "+0x02" expecting the rest of the LHS and then carried on correctly.

I've nothing good to say about PHP, so I won't say any more.


In case anyone else is going crazy looking at the code linked to from the bug trying 
to spot the bug: the link in the bug is to the trunk version of the code, which is now 
fixed. Here's the version before the fix, in which you can also play "spot the bug"
yourself (though the parent's comment pretty much gives it away).

http://lxr.php.net/opengrok/xref/PHP_TRUNK/Zend/zend_language_scanner.l?r=322378#1515


  1.  Parser encounters 0x00+0x02
  2.  ...attempt to lex 0x00:
  2a. ...skip 0x
  2b. ...skip leading 0 until it encounters + # len is 0 at this point
  2c. ...strtol("+0x02...") => 2              # len is 0 at this point
  3.  ...attempt to parse + => Add operator
  4.  ...attempt to parse/lex 0x02 => 2
  4a. ...strtol("02") => 2

So 2 + 2 = 4


bug was introduced in 2006 in php 5.2 era

http://svn.php.net/viewvc?view=revision&revision=225847


Here's the commit, yet another sacrifice to a tin god.

is_numeric_string() optimization

Original Patch by Matt Wilmas

https://github.com/php/php-src/commit/0accb4b0094b8fdda905e0a374843f0c775f4537


I'm a webdev thinking of switching away from PHP. Got any recommendations? If so, why do you recommend it/them?


You might consider switching to Python.


I'll also recommend python.

    Unlike some alternatives it'll be easy to pick up after php. You'll be productive after few hours.

    It's very, very widely used and not only for web development. It has bindings for every
    piece of software and libraries for every purpose imaginable. Again, this could be a
    problem with some of the alternatives.

    It's just a damn good language. I'm really trying hard to find any nasty surprises or
    limitations or just anything to dislike and I can think of 3 or 4 things. That's much,
    much less than for any other language I ever used extensively. C++ comes close, except
    it took me 5+ years to understand why the ways some things work make sense. This means
    that after you understand the basics, you can be really productive and your code won't
    be a ticking time bomb.

A different python user, but here's my list:

    Python 3/Python 2 incompatibilities. Python 3 is the new version with new features, but
    it's not backwards compatible with Python 2,and code needs some clunky constructs to work
    on both. A lot of libraries are still Python 2, so it's impossible to just go python 3.

    Some of the older parts of the standard lib are not as well done as newer parts. unittest
    violates pep8, the coding standard for pretty much every other python program ever. So
    while everything else in the stdlib uses underscored_names, unittest uses camelCase.
    Urllib/urllib2 are a disorganised mess in python 2,and a shuffled around unorganised
    mess in python 3. If you need these, look at the requests library to see if you can
    possibly use that.

    No anonymous functions. If you've done much javascript or any functional language,
    you'll know these can be useful. Now, it's easy to declare functions in python, and you
    can just declare it in the line before using it, but it's still a workaround, much 
    like functors are a workaround for the lack of functions outside classes in Java.

    No anonymous functions.

There are lambdas, what more could you want?


More than one line.


I agree; however, I just encountered a nasty problem. Python sucks as concurrency within
a single script. You can do some things but due to how the Python interpreter works, it is
very hard to see true concurrency gains. I ended up having to restructure to allow a
parameter that specifies which portion of the data set to run over and then run multiple
Python instances. With the way software is heading, this could grow into a larger problem.


A serious question: how can you love a language like that? Perhaps I am a bit of an old-school
guy, but my major reason for liking languages and techniques comes from their elegance. A
language that is an ill-thought-through bag-of-tricks, without any attempt at coherence, like
PHP feels like an abomination to me.

I'm struggling to understand your perspective.


It's easy to fall in love with the first girl that sucks your dick. Lots of PHP programmers
have never been exposed to anything better.


Not entirely true.. its more about the falling in love with the first girl that is popular
with everyone and you feel comfortable with... Some of the reasons why people (usually
newbies) end up using php are:


1) Popularity: It is one of the most widely used programming language. Many multi-million
   $$$ apps were designed on it. Million's of projects available on the internet to study
   and review from.

2) Learning Curve: It is really easy to learn, flexible and you can hack together complex
   tasks in minutes rather than hours.

3) Support: Have you seen the php docs? One of the best laid out documentation I have ever
   come across, not to mention the fact that the added user comments are majorly helpful.
   If I ever get stuck, I could do google search for that specific task in php and surely
   someone has written a tutorial on it or inquired about it on stackflow.

4) Functions and Extension: functions like strtotime? that shit is like magic, throw in any
   date or time in any format and it parses it with an accuracy of about 99%! Tons and tons
   of extensions.

5) Readily available and comes installed with most hostings.

So yes, it is pretty much like the new VB6 of the internet.

I don't think PHP is a terrible language, and it's ridiculously vilified. I think a 
large part of that comes from:

A. History - PHP was historically a somewhat crippled language due to a lot of missing 
   programming constructs & conventions and a lack of real OOP. I think a lot of 
   people dismissed it out of hand for these reasons. A lot (but not all) of this has 
   been addressed in recent years.

B. Ubiquity - as choice #1 for people learning how to script beyond client-side Javascript,
   it was met with derision by people with "real" programming skills. The logic
   (presumably) being "since so many people ARE doing it, anyone CAN do it, therefore
   it's a weak language."

C. Language design - still probably valid, but you can work around this if it bothers you
   that much. Yeah, some functions are verb_action, action_verb, verbaction, action2verb,
   whatever. And that's sloppy and stupid, particularly if you're used to a very short
   selection of pre-defined functions. But it isn't some insurmountable issue.

Would I recommend it as a first language? No. Would I recommend it for an application? 
Maybe ... the resource pool is enough to offset any functional or perceived deficiencies
in the language.

I have also seen more than my share of ugly Python code (my language of choice) to know
that this isn't necessarily an issue with the language.


    History - PHP was historically a somewhat crippled language due to a lot of missing
    programming constructs & conventions and a lack of real OOP.

The problem here is that the language has been evolved by people who were OK with its huge
flaws to begin with.

    The logic (presumably) being "since so many people ARE doing it, anyone CAN do it,
    therefore it's a weak language."

The logic is that anybody who is OK with its gaping flaws is the sort of person who should
not be allowed near important things.

The root of the problem is that PHP from the start was a subset of the worst parts of Perl.
When PHP was started, Perl already had 'use strict', good namespace support, lexical
variables, and a workable object system. But Perl had a lot of historical cruft, too.
Formats absolutely don't belong in the core language, for instance.

The subset of that language known as Modern Perl recognizes the flaws and tries to deal
with them; escew the bad and promote the good. A 'use strict' is mandatory, lexical scoped
variables should always be used unless you have a very good reason. Formats are forgotten
as the weird, archaic tempting system that it is.

In contrast, PHP took basically the opposite subset of the Perl language. Programmers who
are OK with this aren't OK with me.


PHP: The Visual Basic of this century

■_

2012年02月23日

■_

Algol-60
最近ちょこちょこ調べているんですが、結構面白い。

■_ 絶句

閉じこもるインターネット――グーグル・パーソナライズ・民主主義 WEB+DB PRESS Vol.67 この辺と一緒に、

も買ったのですが、これが実に(ry 気になったところをいくつか。

while やら for の e;se を書いてないのはわざと? 7. Compound statements — Python v3.2.2 documentation で、& や | は「論理演算子」じゃあないですよねえ。 5. Expressions — Python v3.2.2 documentation

それにしても豪快な「参考リソース」だ…

■_

実のところこれも欲しかったのだけど財布の中身がそれを許さず(ry
アルゴリズム設計マニュアル 上 アルゴリズム設計マニュアル 下 ビジュアル・コンプレキシティ ―情報パターンのマッピング

■_ 選考事例

誰もやってないことはないだろうと思って調べていくと、 やっぱりやっている人たちがいた。 ということでごにょごにょやってます(謎)

最初の頃にぜんぜん見つからなかったのは、うまいキーワードにあたらなかったからだな。 ひとつ見つけられると芋づる式に見つけられるようになった気がする。

■_

2012年02月22日

■_

なんか駱駝本半額が復活してたので今度はげっと。 きんどるさんに転送した。

■_

C のポインター宣言について

C Pointer Declarations — Gist

C/C++ Pointer Declaration Syntax – It makes sense!

I never really liked the way pointers are declared in C/C++:

C や C++ で次のようなポインターの宣言のやりかたをわたしは決してしません

  int *a, *b, *c; // a, b and c are pointers to int

The reason is that I am used to reading variable declarations as MyType myVar1, myVar2, 
myVar3; and I always read “int*” as the type “integer pointer”. I therefore wanted
the following

その理由は、MyType myVar1, myVar2, myVar3 のような変数宣言をいつも使っていて、
"integer pointer" という型として "int*" を使っているからです。
ですから次のようにして


  int* a, b, c; // a is a pointer to int, b and c are ints

to mean that a, b and c all were of type int*, i.e. pointers to int. and I therefore 
found it slightly annoying to repeat the asterisk for every variable. This also meant 
that the symbol * had two slightly different meanings to me: (1) It declares a pointer 
or (2) it dereferences a pointer. I usually don't declare a whole lot of pointers in 
one line, but still, this is a (minor) annoyance I have briefly discussed with few 
fellow programmers over the years. Today I started reading C Traps and Pitfalls by 
Andrew Koenig and after reading one sentence, in chapter two, the pointer declaration 
syntax suddenly makes – at least some – sense:

これを a, b, c すべての型がが int* となるようにしたいのです。
そして当然、すべての変数にアスタリスクを繰り返しつけていかなければならないことも
避けたいと考えます。このことは * というシンボルが非常に異なった二つの意味を
持っていることを意味しています。
(1) ポインターの宣言
(2) ポインターの dereference

[…] Analogously,

float *pf;

means that *pf is a float and therefore that pf is a pointer to a float.

Of course! If we instead of looking at it as a variable a of type int*, read it as *a 
– i.e. “a dereferenced” – it makes sense. That is indeed an int, and that also 
means that * always means “dereference”.

Discussion

    http://www.reddit.com/r/programming/comments/pz3n0/cc_pointer_declaration_syntax_it_makes_sense/
    http://news.ycombinator.com/item?id=3615750

■_

それに対する Hacker News での反応から

C/C++ Pointer Declaration Syntax – It makes sense | Hacker News

Yet another example of why C type declaration syntax could be better. First, C should 
have followed this golden rule:

  <scope> <type> <name>;   // variable declaration
  typedef <name> = <type>  // typedef declaration

  (with <scope> = "static", "auto", or nothing)

That would be a first step. A second one would be to use parentheses to denote grouping.
Such that extraneous parentheses does not screw up the whole type declaration:

  // the following two lines are equivalent
  []*int   p; // array of pointers (but this is not clear)
  [](*int) p; // Ah, now this is more obvious.
  
  int*[]   p; // alternate syntax, with postfix notation
  (int*)[] p; // (personally, I prefer the prefix one)
  *int[]   p; // mixing postfix and prefix does no good.

Same rule for const:

  const *int i; // constant pointer to mutable int.
  *const int i; // mutable pointer to a constant int.
  *(const int) i; // again, we can clarify.
  const (*int) i;

(By the way, I think we should make const the default, and use a "mutable" 
keyword instead. But that's another fight.)

Functions could be declared in simpler ways:

  bool (int i, float x) f;
  (int i, float x)    bool f; // alternate syntax
  (int i, float x) -> bool f; // alternate syntax 2
  bool <- (int i, float x) f; // alternate syntax 3

C doesn't do currying by default, so I don't really care wether the return type goes 
before or after the arguments. But the name of the function should definitely be on 
the right of its own type, so we still follow the golden rule. Function definition 
would be equally simple:

  bool (int i, float x) f =
  {
    return 2 * i + x;
  }

Note the similarity with

  int i = 42;

Now you want a pointer to a function? Easy: you just prefix (or postfix, depending on 
your ultimate choice) the type with a star:

  *(bool (int i, float x)) fp = f;

  // Note: this one is ambiguous without precedence rules:
  *bool (int i, float x) fp = f;
  // And this one clearly denotes a function wich returns
  // a pointer to bool
  (*bool) (int i, float x) fp = f;

Structure (and class) declaration could also use a bit of makup:

  typedef Foo = struct {
    int   i;
    float x;
    bool  b;
  };

There, the syntax of types is much nicer, and easier to parse (it doesn't really 
matter for C, but C++ could really use some love).

Also worthy of mention is, this syntax above is less restricted than the ANSI one. 
However, the semantic restrictions still hold. This declaration for instance would be 
syntactically valid, but semantically bogus:

  void (int i, struct { float f; bool b; }) f;

It could work if C were ducked typed. :-)


My cynic guess for all the re-using of existing C syntax in new and confusing roles 
for C++ is that the creators of C++ shirked away from writing a new parser as much as 
possible.

No, Stroustrup implemented a new parser before it was even called C++. He is very 
clear in "Design and Evolution of C++" and elsewhere why C++ is mostly 
backwards compatible with C: otherwise, he feared no one would use it.


There's a really interesting paper on the origins of C++, written by Bjarne himself:

http://www2.research.att.com/~bs/hopl2.pdf

■_

■_ y

eban さんが、ついったで sed の y コマンドの(名前?)がどこから来たんだろう とかツイートしてたんですが、なんでしたっけ? 前に聞いた覚えがあるような気もするんだけどなあ。

■_

ネタは溜まるが調理する時間なっしんぐ

2012年02月21日

■_

らくだ
Programming Perl 4th Edition E-Book Now Available (at half price) | Hacker News で知って、朝方見たときは確かに半額だったのですが、 先ほど買ってみるかとアクセスしてみたらすでに半額セールは終わっていた気配 ○| ̄|_ Programming Perl, 4th Edition - O'Reilly Media

きんどるさん
touch の方、横向きに表示させる「ランドスケープ」モードがない模様 (touch でない方にはある)。 んーむA4サイズのPDFファイル見るのに都合いいんだけどなあ>ランドスケープ

■_ IO Monad

Scala に Io Monad がないのはなぜ?

haskell - Why doesn't Scala have an IO Monad? - Stack Overflow

I'm wondering why Scala does not have an IO Monad like Haskell.

わたしは Scala に Haskell のような IO Monad がないことをなぜだろうに思っています。

So, in Scala the return type of method readLine is String whereas in Haskell the 
comparable function getLine has the return type IO String.

ですから、Scala では readLine メソッドの戻り値の型は Haskell のそれが
IO String という型を返す getLine 関数と comparable なのに対して、
String となっています。

There is a similar question about this topic, but its answer it not satisfying:

この topic について同様の質問があるのですが、次のような回答では満足できないのです

    Using IO is certainly not the dominant style in scala.

Can someone explain this a bit further? What was the design decision for not including IO
Monads to Scala?

Because Scala is not pure (and has no means to enforce that a function is pure, like D has)
and allows side effects. It interoperates closely with Java (e.g. reuses big parts of the
Java libraries). Scala is not lazy, so there is no problem regarding execution order like
in Haskell (e.g. no need for >> or seq). Under these circumstances introducing the IO
Monad would make life harder without gaining much.

それは Scala が pure ではなく、副作用を許すものだからです
(D が持っているような、ある関数が pure であることを強制するような手段もありません)。


But if you really have applications where the IO monad has significant advantages, nothing
stops you from writing your own implementation or to use scalaz. See e.g.
http://apocalisp.wordpress.com/2011/12/19/towards-an-effect-system-in-scala-part-2-io-monad/

[Edit]

    Why wasn't it done as a lazy and pure language?

This would have been perfectly possible (e.g. look at Frege, a JVM language very similar to
Haskell). Of course this would make the Java interoperability more compilcate, but I don't
think this is the main reason. I think a lazy and pure language is a totally cool thing,
but simply too alien to most Java programmers, which are the target audience of Scala.
Scala was designed to cooperate with Java's object model (which is the exact opposite of
pure and lazy), allowing functional and mixed functional-OO programming, but not enforcing
it (which would have chased away almost all Java programmers). In fact there is no point in
having yet another completely functional language: There is Haskell, Erlang, F# (and other
MLs) and Clojure (and other Schemes / Lisps), which are all very sophisticated, stable and
successful, and won't be easily replaced by a newcomer.

■_

Emacs まつり震源

Why I Still Use Emacs « Occasionally sane

Why I Still Use Emacs
なぜわたしはEmacsを使い続けているのか

At school, I'm known as the Emacs guy; when people have questions about configuring 
Emacs or making it work a certain way, they often come and ask me. Sometimes, some 
people ask me why use Emacs at all? Isn't it a really old editor and aren't Eclipse or 
Visual Studio much better? I mean, they don't have weird key bindings and have 
intellisense, that's surely better for a programmer, right?

学校ではわたしは Emacs guy として知られています。Emacs の configuring や
使い方について疑問を持った人が良くわたしのところに訪ねてきます。
中にはなぜEmacs を使っているのだと訊いてくる人もいます。
Emacs は古いエディターだし、Eclipse や Visual Studio の方がずっと良いのではないか
というのです。

I will attempt in this post to explain some of the reasons why I still cling to Emacs. 
Believe me, I don't think I have any emotional attachment to Emacs; the reason I 
cannot leave it is that it seems that the grass is yellower everywhere else. I'd love 
to be able to use something like Eclipse for most/all of my work. Alas, I can't (or 
rather, I won't).


Emacs has a GUI and a CLI interface

Emacs は GUI のインターフェースと CLI のインターフェースを持っている

I often connect to my university's server with SSH; since Emacs is capable of running 
in a terminal, I can use it in those instances. On my desktop, I can use the slightly 
cuter GTK interface. IDEs like Eclipse are entirely GUI-based, this means that unless 
you can do X forwarding, you cannot use it remotely. In my case, this makes Emacs 
available in all instances where I need it.

The interface also consists entirely of text, so you can always use the very useful 
editing features that you are used to (e.g. searching, copying, etc.). One of the most 
common example is using the search function in a customize screen to find a certain 
parameter.
略
Conclusion
結論

Emacs is not perfect, it is lacking in many regards:

Emacs は完璧ではありません。多くの欠落があります


    It has no multi-threading support
    マルチスレッドサポートがありません

    Elisp is not a great language, and is quite slow
    Emacsl Lsip は great language ではなく、とても遅いものです

    Using multiple major modes in a file sucks (e.g. PHP + HTML + JavaScript)

    ひとつのファイルで複数のメジャーモード (たとえば PHP + HTML + JavaScript) を
    使うことは

    Most programming modes have no semantic understanding of the language they support, and
    thus offer no facilities like intellisense, or re-factoring. (I'm not sure what the
    progress of the semantic mode is; please comment if you know.)

    大部分のプログラミング言語用モードではサポートしているその言語の semantic を理解しません。
    また、インテリセンスやリファクタリングのような facilities の offer がありません。


But I think that for the most part, it compares favorably to the modern IDEs. In the next
few years, I imagine that Emacs will start gaining and solidifying the IDE features that
many users want. I don't know what IDEs will do; I would hope that they'd take a step
back and learn some of Emacs' lessons and offer an experience that power users can
appreciate more.

EDIT: I've tried to fix the newlines problem; let me know if this comes out better.

■_

Hacker news の方でも盛り上がってましたが reddit からいくつか。

Why I Still Use Emacs : programming

It's frightening, every time I read one of those vim/emacs articles I feel like I am not
productive at all. It seems like people are punching so much code that occasionally moving
the hands to the mouse hinder them from writing all those code lines they could have
written. How many code lines people write in a day that they need all this efficiency?


One of my most productive days of work ever, I spent hours and hours and hours--and 
wrote a single line of code.

A good programmer spends much more time thinking than he does writing code. A good 
programming editor makes it easier to explore code than write it. Combine vim with 
Exuberant CTags, and you can drill down through code with Ctrl+] and Ctrl-T so fast 
it'll make your co-workers' heads spin.


"Emacs saves you time when you work, but takes it back when you play with it."

Emacs is an environment, not an editor. It is an environment that has been around for 
very long, and people tend to adopt it permanently. As an environment, it has shortcomings
and warts that make it annoying for a non-programmer, but the power it gives to a
programmer that's willing to keep learning and extending it is tremendous.

■_


一つ前へ 2012年2月(中旬)
一つ後へ 2012年3月(上旬)

ホームへ


リンクはご自由にどうぞ

メールの宛先はこちらkbk AT kt DOT rim DOT or DOT jp