<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/xml.xsl" type="text/xsl"?><feed xmlns="http://www.w3.org/2005/Atom">
  <title>nulog, NULL::something : out of the headphone &gt; 2004 &gt; December &gt; 07</title>
  <link href="http://lowreal.net/logs/2004/12/07"/>
  <icon>http://lowreal.net/img/banner.png</icon>
  <link rel="self" type="application/atom+xml" href="http://lowreal.net/logs/2004/12/07.atom"/>
  <link rel="alternate" type="application/xhtml+xml" href="http://lowreal.net/logs/2004/12/07.xhtml"/>
  <updated>2004-12-07T22:19:20+09:00</updated>
  <author>
    <name>cho45(砂糖)</name>
  </author>
  <id>http://lowreal.net/2004/12/07</id>
  <entry>
    <title>xyzzy io-mode</title>
    <link rel="alternate" type="text/html" href="http://lowreal.net/logs/2004/12/07/1.html"/>
    <link rel="alternate" type="application/xml+xhtml" href="http://lowreal.net/logs/2004/12/07/1.xhtml"/>
    <updated>2004-12-07T14:38:08+09:00</updated>
    <published>2004-12-07T14:38:08+09:00</published>
    <id>http://lowreal.net/2004/12/07/1</id>
    <category term="prog"/>
    <category term="soft"/>
    <content type="xhtml" xml:base="http://lowreal.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p><a href="/2004/io-mode.zip">io-mode.zip</a>, <a href="/2004/io-mode/io-mode.l">io-mode.l</a></p>
        <p>簡単そうなので作ってみる。殆ど c-mode のパクりなわけだけど……結局インデントレベル計算するところしか作ってないわけで、そしてそれも括弧しか使わない Io では数えるだけなわけで的な。とはいえアフォだからかなり詰ったんですけども <abbr>orz</abbr></p>
        <p>ioServer の Regex て何か変じゃありませんか。nextMatch と firstMatch が全く同じ挙動名気がする。つーかマニュアル書くなら効果的なサンプルと一緒に書いて欲しいなって思う。戻り値も書いてないこと多すぎ。むしろ最初から String replace に正規表現とれるようにしれと。正規表現がデフォで使えないとかキツすぎて死にます。</p>
        <ins datetime="2004-12-10T05:22:25+09:00">
          <p><a href="/2004/io-mode/readme">io-mode readme</a>, iolanguage</p>
        </ins>
      </div>
    </content>
  </entry>
  <entry>
    <title>iolanguage 引数のデフォルト値</title>
    <link rel="alternate" type="text/html" href="http://lowreal.net/logs/2004/12/07/2.html"/>
    <link rel="alternate" type="application/xml+xhtml" href="http://lowreal.net/logs/2004/12/07/2.xhtml"/>
    <updated>2004-12-07T16:16:20+09:00</updated>
    <published>2004-12-07T16:16:20+09:00</published>
    <id>http://lowreal.net/2004/12/07/2</id>
    <category term="prog"/>
    <content type="xhtml" xml:base="http://lowreal.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>リファレンスを見た限りではメソッドの引数を省略したときの値を指定できないっぽい。ようは可変長変数を使うんだと思うんだけど、これまた長い。んなわけで適当にマクロ化するメソッドを定義しとく。もしかしたらこんなことやらないでももっといい方法があるかもしれない。</p>
        <pre title="オプション引数" class="Io">/*
    pos     : 引数の位置
    name    : 代入されるスロットのキー (勝手に初期化)
    default : デフォルトの値
 */
Object opt := method(pos, name, default,
    if (sender doString("thisMessage argAt(" .. pos .. ")")) then (
        sender doString(name .. " := sender doMessage(thisMessage argAt(" .. pos .. "))")
    ) else (
        sender setSlot(name, default)
    )
)</pre>
        <p>んで以下のように使う。</p>
        <pre title="Ruby の Array#join を Io で定義してみる。" class="Io">List join := method(
    opt(0, "sep", "")
    ret := ""
    self foreach(index, value,
        ret = ret .. value
        if (index &lt; self count - 1) then (
            ret = ret .. sep
        )
    )
    ret
)
l := List clone push("aaa") push("bbb") push("ccc")
l join(", ") print //=&gt; "aaa, bbb, ccc"
l join print //=&gt; "aaabbbccc"</pre>
        <p>Ruby の Array#join っぽいことをしてみる。 String join というメソッドがあるけど何かこのメソッドは位置がおかしいし、セパレータを引数にとれない。<q>っぽい</q> というのはデフォルト引数が <code>""</code> きめうちだから。Io に <code>$,</code> がないから (なくていい) ってことです。<ins datetime="2004-12-07T16:49:14+09:00">あーあと再帰的に join してないや。to_s メソッドとかないからその辺もやってないし。ようは List の中身が String 以外だと例外はきますよと。</ins></p>
        <p>どうでもいいけど <code>if</code> を書くときに <code>then</code> と <code>else</code> を使うのは構造的違和感がある。でも可読性はこっちのほうがいいし、<code>case</code> がないうえに <code>if</code> の引数として書く場合 <code>elseif</code> が書けないからどっちにしろ書くことにはなるんだけど。<code>if</code> の引数に全部書くほうはワンライナー向けかしら。</p>
      </div>
    </content>
  </entry>
  <entry>
    <title>iolanguage Directory items</title>
    <link rel="alternate" type="text/html" href="http://lowreal.net/logs/2004/12/07/3.html"/>
    <link rel="alternate" type="application/xml+xhtml" href="http://lowreal.net/logs/2004/12/07/3.xhtml"/>
    <updated>2004-12-07T21:14:33+09:00</updated>
    <published>2004-12-07T21:14:33+09:00</published>
    <id>http://lowreal.net/2004/12/07/3</id>
    <category term="prog"/>
    <content type="xhtml" xml:base="http://lowreal.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>何で Directory が ioServer なのかはとりあえず置いておいて、<code>Directory items</code> が File しか返さない。<q cite="http://www.iolanguage.com/Source/release/Io/IoServer/Directory/_docs/Directory.html" title="Directory" xml:lang="en">Returns a list object containing File and Directory objects</q> ですよ。<span class="fn">ついでに list object じゃなくて List object だろうとか思ったけど理解できるからとりあえずフットノート。</span>いや英文解釈が間違っているのかもしれないとか思いつつしかし常識的に考えて (まぁたしかに Directory も File ではあるけど) Directory を File として返さないだろうとか、むしろ File としてしか返さなくて File にタイプを識別するメソッドがなかったら使えないよねって思ったから File にそんなメソッドあるのかなって思ったらなかった。そういや File Primitive にはファイルの更新時間を取得するメソッドもない気が。つまるところ今のデフォルトの Io では blosxom クローンは作れないっぽい。</p>
        <p>てか Regexp, Directory を IoVM の Primitive に……</p>
      </div>
    </content>
  </entry>
  <entry>
    <title>iolanguage メソッドの引数の評価</title>
    <link rel="alternate" type="text/html" href="http://lowreal.net/logs/2004/12/07/4.html"/>
    <link rel="alternate" type="application/xml+xhtml" href="http://lowreal.net/logs/2004/12/07/4.xhtml"/>
    <updated>2004-12-07T22:19:20+09:00</updated>
    <published>2004-12-07T22:19:20+09:00</published>
    <id>http://lowreal.net/2004/12/07/4</id>
    <category term="prog"/>
    <content type="xhtml" xml:base="http://lowreal.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>素朴な疑問にひっかかった。if とか else もメソッドで、引数にメッセージをとるけど、何でこの引数は評価されずに渡されるんだろう。ちょっと解りにくいから実際にコードを書いてみる。</p>
        <pre title="Io if-then-else メソッド" class="Io">if (Nil) then (
    "not print" print
) else (
    "print" print
)</pre>
        <p>この場合もちろん <samp>not print</samp> は出力されないし、それが願う動作。しかしながら <code class="Io">"not print" print</code> も引数だから、メソッドに渡される前に評価されて <samp>not print</samp> と出力されるんじゃないかと悩んだ。というか正確に言えば if をユーザから定義するときに評価されてしまってハマった。以下にハマったコードを示す</p>
        <pre title="io myif メソッド問題" class="Io">myif := method(test,
    ifObj := Object clone
    ifObj test := test
    ifObj do (
        mythen := method(msg,
            if (test, sender doMessage(thisMessage argAt(0)))
            self
        )

        myelse := method(msg,
            if (test isNil, sender doMessage(thisMessage argAt(0)))
            self
        )
    )
)

myif (Nil) mythen (
    "nil" print
) myelse (
    "aaa" print
)
</pre>
        <p>これは予想に反して <samp>nilnilaaa</samp> と出力される。引数が渡される前に評価されしまっているからだ。リファレンス見ててもよくわからないので、適当に書き直していたらできた。書き直した結果を書いてみる。</p>
        <pre title="io myif メソッド解決" class="Io">myif := method(test,
    ifObj := Object clone
    ifObj test := test
    ifObj do (
        mythen := method(
            if (test, sender doMessage(thisMessage argAt(0)))
            self
        )

        myelse := method(
            if (test isNil, sender doMessage(thisMessage argAt(0)))
            self
        )
    )
)</pre>
        <p>何が変わったかっていうと method の仮引数を書かないようにしただけ。仮引数を書くと評価されてしまうらしい。微妙な罠。プログラミングガイドに明記してもいいじゃん！　とはいえこれを踏まえてから読み直すと <q cite="http://www.iolanguage.com/Source/release/Io/_docs/IoProgrammingGuide.html#Methods" title="Io Programming Guide" xml:lang="en">The thisMessage slot that is preset (see next section) in locals can be used to access the <em>unevaluated</em> argument messages.</q> と書かれていて<span class="fn">といっても原文は引用しただけで日本語訳から探した。</span> (強調はされていない) 頭がよければ thisMessage スロットには未評価なメッセージが入っていて、仮引数を書かなくても呼び出せる。もしかして仮引数を書かなければメッセージは評価されないんじゃないか、なんて推論できる (苦しい) かもしれないけど、俺には無理！</p>
      </div>
    </content>
  </entry>
</feed>
