Hacker News new | ask | show | jobs
by tophercyll 5784 days ago
Ignoring user interface and the mechanics of peer discovery...

  user-interface, discovery-system |
  # This process is spawned with two pids as arguments
  
  # If either pid halts, I halt too.
  user-interface.link
  discovery-system.link
  
  user-interface.subscribe(when(
    text-entered: { message |
      # I entered a message in the UI, announce it to everyone subscribed to me
      my-actor.announce('exclaimed, message)
    }
  ))
  
  discovery-system.subscribe(when(
    peer-discovered: { name, peer |
      # Discovery system located another user in the chat room
  
      # Send message to user-interface showing a Growl style notification.
      user-interface << ('notify, "{name} arrived.")
  
      peer.subscribe(when(
        # This guy went away - "halted" is announced when a process stops
        halted: { result | user-interface << ('notify, "{name} left.") }
  
        # This guy said something
        exclaimed: { message | user-interface << ('text-append, "{name}: {message}") }
      ))
    }
  )
  
  main.loop
2 comments

Are the names discovery-system, user-interface, my-actor important?
Every process has "my-actor" defined in its global environment. It provides methods related to basic actor functionality. In this case I'm calling the "announce" method to say something to all of the process' subscribers.

user-interface and discovery-system are two hypothetical processes whose pids (process identifiers) are passed in to this process as arguments. user-interface would be responsible for creating a window with a text display area and a text entry box. discovery-system would use our remote service discovery model to find others chatting in the same "room."

what does ' before a symbol do?
in scheme, the ' is the syntax for a quote. i'm guessing they use it here as syntax to symbols, but in some of the example code, i see it in front of list-like things too...
Our use of ' was inspired by lisp's.

Immutability such is an important part of Spin's messaging model that we wanted a consistent way to declare an immutable construction.

  # An immutable string (or symbol)
  'foo

  # An immutable list
  '[ 1, 'foo, '[] ]

  # An immutable map
  '{ foo: 1, y: 'two, z: '[ 'three ] }

Slightly more advanced...

  # Also an immutable string
  "foo with spaces" 

  # An immutable pair
  "bar" -> 2

  # Syntactic sugar for that same pair
  bar: 2 

  # Syntactic sugar for "baz" -> baz
  ~baz
While we're talking about syntax, "\n" is an alternative to ",".

This helps multi-line expressions read more naturally.

  '{
    foo: 1
    baz: 'bar
    chunky: 'bacon
  }