1. To the Daisy

    Daisy

            To the Daisy
                         by W. Wordsworth
    
    With little here to do or see
    Of things that in the great world be,
    Sweet Daisy! oft I talk to thee,
          For thou art worthy,
    Thou unassuming commonplace
    Of Nature, with that homely face,
    And yet with something of a grace
          Which Love makes for thee!
    
    Oft on the dappled turf at ease
    I sit and play with similes,
    Loose types of things through all degrees,
          Thoughts of thy raising;
    And many a fond and idle name
    I give to thee, for praise or blame,
    As is the humour of the game,
          While I am gazing.
    
    A nun demure, of lowly port;
    Or sprightly maiden, of Love's court,
    In thy simplicity the sport
          Of all temptations;
    A queen in crown of rubies drest;
    A starveling in a scanty vest;
    Are all, as seems to suit thee best,
          Thy appellations.
    
    A little Cyclops, with one eye
    Staring to threaten and defy,
    That thought comes next—and instantly
          The freak is over,
    The shape will vanish, and behold!
    A silver shield with boss of gold
    That spreads itself, some fairy bold
          In fight to cover.
    
    I see thee glittering from afar—
    And then thou ...
  2. 从新浪博客搬家到WordPress

    要从新浪搬到Wordpres,网上广为流传的方法是利用blogbus的博客搬家服务获得blogbus格式的xml,然后再用一个Python写的脚本把它转换成WordPress认识的格式。但是这种方法在最近新浪博客升级以后就失效了。于是自己用现学的scala写了一个小程序,搬家时能保留标签、目录、评论及评论回复这些信息。

    猛击这里下载。注意,此程序仅支持2010年初的新版新浪博客,之前或之后的版本都不支持。

    要运行程序,你需要确保已经安装过JRE。双击运行后显示如图界面,填入自己的博客地址(不要省略“ http:// ”),然后点击“Start”即可。这时“Start”按钮变为灰色,标题栏显示“Extracting”。等待几分钟,当标题栏显示为“Done”、“Start”按钮重新变为可用时,程序所在目录下会出现一个blog.xml文件。把这个文件直接导入WordPress就可以了。

    代码也打在jar包里了,MIT协议。欢迎报告bug。

    下面是废话。

    恩由于新浪用了ajax,评论信息是通过xhr异步读取的,用一般的方法没法抓到。我纠结许久,最后是用了非常ad hoc的方法解决的,不知道有没有什么什么不太麻烦的通用解决方案呢。

    再扯两句scala。我都想不起来当初具体是怎么想到要学scala的,也许是为了了解下函数式编程,也许只是想在jvm上有一个喜欢的语言吧——Java写起来太不爽了;Java社区的低效和保守也已经开始显出C++的影子。

    scala确实是非常强大和灵活;我在见到一些颇富技巧性的hack之后都有些怀疑scala社区的风气会不会慢慢变得像C++社区一样过分热衷技巧的炫耀。不过scala的设计目标就是以较简单的语法规则获得最大的scalability,不需要通过挖掘语言规范里的犄角旮旯来实现一些必要功能,所以不会像C++一样成为一门本身已相当复杂,却还需要别人反过来教语言发明者如何使用的语言。

    scala毕竟表现力比Java强太多,代码也简洁太多。比如这次我需要实现一个抛出异常后重试若干次的逻辑,只需定义一个函数:

    def tryFor[T](times: Int)(op: => T): T = {
      if (times <= 0) throw new RuntimeException("Operation failed.")
      try { return op } catch {
        case e: Throwable => e.printStackTrace
        tryFor(times - 1)(op)
      }
    }
    

    然后这样使用:

    val source = tryFor(5) {new Source(url)}
    

    程序就会不断获得网页源代码,并在5次失败后抛出异常。Java实现同样的东西可不会如此优雅了。又如下面这段代码返回一篇博文xml:

    private def generateEntryXml(entry: BlogEntry) = {
      <item>
        <title>
          {entry.title}
        </title>
        <wp:post_date>
          {dateFormat.format(entry.postDate)}
        </wp:post_date>
        <category>
          {entry.category}
        </category>
        {for (tag <- entry.tags) yield <category domain="tag">{tag}</category>}
        <content:encoded>
          {xml.Unparsed(handleNewLines(entry.content))}
        </content:encoded>
        <wp:status>publish</wp:status>
        {for (comment <- entry.comments) yield generateCommentXml(comment)}
      </item>
    }
    

    注意,xml标签直接作为scala的源代码的一部分在代码中出现!虽然我觉得这样会使scala语言多出一种“特殊情况”,增加语言的复杂性,但不得不承认这样的设计确实非常优美简洁。

    我比较看好scala,以后自己做跑在jvm上的东西scala应该是首选语言。推荐有兴趣的童鞋也了解一下。

  3. 有爱的小脚本:启动终端时显示一句箴言

    效果就是每次启动终端时都有一个小动物什么的讲一句有意思的话:

    image0

    说话的东西和说的话都随机出现。这个效果是我在Linux Mint里面看到的,感觉很有爱。下周考Unix环境编程,周末恶补一下,顺便写这个小脚本练手。

    以我在用的 ubuntu 为例。首先确保安装 fortunescowsay 两个包。前者用于显示各种各样的趣味短句,后者则提供了一头会说话的奶牛(和其它各种诡异的东西)。关于fortunes还有一些有趣的包你可能也想一起安装,比如fortune-zh里有唐诗宋词,fortunes-ubuntu-server则有关于使用Ubuntu Server的贴士,等等。

    #!/bin/bash
    # Cow randomly says a hopefully interesting adage
    
    # Get a short message from fortune, both offensive and not.
    # Remove -a if you don't want to see offensive ones.
    # Remove -s if you don't mind reading the long messages.
    msg=`fortune -a -s`
    
    # Randomly pick a mode of the cow
    modes=("" -b -d -g -p -s -t -w -y ); mode=${modes[$(($RANDOM % 9))]}
    
    # cowsay or cowthink?
    cowdos=(cowsay cowthink); cowdo=${cowdos[$(($RANDOM % 2))]}
    
    # Radomly pick a cow picture file
    speaker=`cowsay -l | sed '1d;s/ /n/g'| sort -R | head -1`
    
    # That's it ^^
    echo "$msg" | $cowdo -n -f $speaker $mode
    

    保存后加上执行权限:

    chmod +x cowsay-fortune
    

    然后把这个文件复制到/usr/bin

    sudo cp cowsay-fortune /usr/bin
    

    最后打开/etc/bash.bashrc

    sudo gedit /etc/bash.bashrc
    

    并在最后加上一行:

    cowsay-fortune
    

    保存后打开终端,应该就是这个效果了:

    image1

  4. [POJ1771] Elevator Stopping Plan

    Description

    ZSoft Corp. is a software company in GaoKe Hall. And the workers in the hall are very hard-working. But the elevator in that hall always drives them crazy. Why? Because there is only one elevator in GaoKe Hall, while there are hundreds of companies in it. Every morning, people must waste a lot of time waiting for the elevator.

    Hal, a smart guy in ZSoft, wants to change this situation. He wants to find a way to make the elevator work more effectively. But it’s not an easy job.

    There are 31 floors in GaoKe Hall. It takes 4 seconds for the elevator to raise one floor. It means:

    It costs (31-1)*4=120 seconds if the elevator goes from the 1st floor to the 31st floor without stop. And the elevator stops 10 second once. So, if the elevator stops at each floor, it will cost 30*4+29*10 = 410 seconds (It is not necessary to calculate the stopping time at 31st floor). In another way, it takes 20 seconds for the workers to go up or down one floor. It takes 30*20 = 600 seconds for them to walk from the 1st floor to the ...

Page 2 / 9