Blogged by Ujihisa. Standard methods of programming and thoughts including Clojure, Vim, LLVM, Haskell, Ruby and Mathematics written by a Japanese programmer. github/ujihisa

Thursday, August 20, 2009

Visited Bay Area

It was my fourth visit to Bay Area also known as Silicon Valley. I stayed at my friend rakusai's house.

During the visit, almost all time I had been in Palo Alto.

Palo alto from wikipedia

palo alto facebook

Bay Area in Summer

Actually it was my first time to stay there in a summer season. I was truly surprised how it was cold, particularly in San Francisco. It was colder than in Vancouver, Canada.

summer san francisco

san francisco

I met some great people

First, I went to San Francisco to meet github people. San Francisco is pretty far from Palo Alto, so I had to came back soon. And also unfortunately the meetup place was very crowded and noisy, I had trouble with listening English there but I certainly met them. Thanks Tom, the next time is my turn to treat you to good alcohols.


Actually I would like to visit Engine Yard to meet Yahuda again as well, but he seemed to be so busy and I didn't want to bother him, so I couldn't met him this time.

Second, I met some Japanese friends include kenn, nobu and shotbt. I heard that kenn is making something new. Here I'll pass it over in silence.

photo from twitshot

Third, I went to Stanford University to meet a researcher and a professor. I asked the professor if my thought would be suit for your research team or not. I was anxious about it, but the professor answered it's absolutely theirs. I decided where to join a research team at the time if I would be admitted the university.


Now I'm in a hot country, Vancouver. Now I can see my goal in 2009 and the beginning of 2010. I will keep doing the thing what I should do.

My next visit to Bay Area may be November.

All photos are from flickr CC search.

Tuesday, August 18, 2009

Any Programming Languages Without Their Syntactic Sugars Are Esoteric Languages

Haskell is one of the ultimate programming language. Haskell has user friendly syntax, pragmatic purely functional model and really simple notation. But when it comes to throw the syntactic sugars, Haskell became an esoteric languages like yhara showed.

You can easily imagine how the Ruby programming language without its syntactic sugars is like.

Sunday, August 16, 2009

Try EventMachine

The Ruby library EventMachine makes us easy to write a network server/client software on Ruby.

Here's an echo server described in the EventMachine README.

require 'eventmachine'

module EchoServer
  def post_init
    puts "-- someone connected to the echo server!"

  def receive_data data
    send_data ">>>you sent: #{data}"
    close_connection if data =~ /quit/i

  def unbind
    puts "-- someone disconnected from the echo server!"

EventMachine::run {
  EventMachine::start_server "", 8081, EchoServer

Let's try it. Save the code to a.rb and do the command:

$ rake build
$ ruby -Ilib a.rb

Now the echo server started on port 8081. Let me check it on another terminal.

$ telnet localhost 8081
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying fe80::1...
telnet: connect to address fe80::1: Connection refused
Connected to localhost.
Escape character is '^]'.
>>>you sent: hi
>>>you sent: hey

It certainly worked.

Say server

OSX has say command which speaks the given sentence. Now I'll write Say Server to handle notification from a remote machine.

require 'eventmachine'

module SayServer
  def receive_data(data)
    system 'say', data

EventMachine::run {
  EventMachine::start_server "", 8081, SayServer

It was too easy to fix it.

Unfortunately I didn't know how to make the server say a word in one line. The following command didn't work like I expected.

$ echo 'hi' | telnet localhost 8081

Spawned Processes

I found an interesting document in docs/SPAWNED_PROCESSES.

Spawned Processes in EventMachine are inspired directly by the "processes" found in the Erlang programming language. EM deliberately borrows much (but not all) of Erlang's terminology. However, EM's spawned processes differ from Erlang's in ways that reflect not only Ruby style, but also the fact that Ruby is not a functional language like Erlang.

Note that the word spawn doesn't mean Kernel.#spawn in ruby 1.9. This EventMachine.spawn generates something like an Erlang's tiny process, which can receive and send a message each other.

Here's sample ping-pong code:

require 'eventmachine' {
  pong = EM.spawn {|x, ping|
    sleep 0.1
    puts "Pong received #{x}"
    ping.notify( x-1 )

  ping = EM.spawn {|x|
    if x > 0
      puts "Pinging #{x}"
      pong.notify x, self

  ping.notify 3
p :finished

The result is:

$ ruby -Ilib a.rb
Pinging 3
Pong received 3
Pinging 2
Pong received 2
Pinging 1
Pong received 1

I expected that the message :finished would appers during the ping-pong, but didn't. I fixed the line 'ping.notify 3' into 'Thread.start { ping.notify 3}', but the result was still same. Even worse, 'fork { ping.notify 3 }' got frozen up.

[Aug 18 Added] Stoyan suggested that EM::Deferrable is suit to be used instead of Thread or spawn. The page he introduced shows the following sample code (I added comments on it a little to show the order of processing)

class Worker
  include EM::Deferrable

  def heavy_lifting
    # (3)
    3.times do |i|
      puts "Lifted #{i}"
      sleep 0.1
    set_deferred_status :succeeded
end do
  # (1)
  worker =
  worker.callback {
    # (4)
    p "done!"
  } {
    # (2)
    # (5)
  # (3)'
  puts "resuming remaining program operations"

And the result is

Lifted 0
resuming remaining program operations
Lifted 1
Lifted 2

Not Equal Is Not Not And Equal

In Ruby, != is tokenized as tNEQ instead of ! and =. Although the Ruby parser must be able to parse a sequence of ! and = as not-equal operator, it is considered as one token for ease. We cannot write those two symbols separately, but nobody wants to do.

By the way, send suggested that it would be very comprehensible to introduce a new not-equal operator ><. I wrote a patch for it for ruby.

diff --git a/parse.y b/parse.y
index 33660a2..77e6cbe 100644
--- a/parse.y
+++ b/parse.y
@@ -6586,6 +6586,11 @@ parser_yylex(struct parser_params *parser)
         return '<';

       case '>':
+        if ((c = nextc()) == '<' ) {
+            lex_state = EXPR_BEG;
+            return tNEQ;
+        }
+        pushback(c);
         switch (lex_state) {
           case EXPR_FNAME: case EXPR_DOT:
             lex_state = EXPR_ARG; break;

As a result, now we can run such a code.


Tuesday, August 4, 2009

Hacking parse.y with me

Yesterday I made my first English presentation.

What was the topic?

Since this year I have tried some short hacks with MRI (Matz Ruby Implementation), and showed the results on my blog. This presentation was what and how did I fix ruby. Almost half time of my presentation was demonstration, which includes applying patches and building ruby.

How was the presentation?

I must have spoken in awkward and broken English. I hope attendees could understand what I tried to explain. It was still difficult for me to speak and listen English. Even worse I had a trouble in listinig English. I hope I had answered correctly to questions.

The ruby meetup people were friendly and generous. Thank you very much!




(These photos are not my presentation. It's just for showing how the room was like.)