Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Instruct a shared whiteboard to save and refresh

The following code used with the ProjectX API informs the client web browser that the whiteboard will be refreshed in 5 seconds. It then archives the current whiteboard information, formats it, and sends a message to each web browser to refresh their view.

<project name="whiteboardqueue">
  <methods>
    <method name="create">
      <params>
        <param var="type">ecmascript</param>
        <param var="body">startRefresh(5)</param>
        <param var="sender">system</param>
      </params>
    </method>
    <method name="timer">
      <params>
        <param var="timer">5</param>
      </params>
    </method>
    <method name="archive_and_format">
      <params/>
    </method>
    <method name="create">
      <params>
        <param var="type">ecmascript</param>
        <param var="body">reloadDocument()</param>
        <param var="sender">system</param>
      </params>
    </method>
  </methods>
</project>


*update 1:14am*
The whiteboard demo [rorbuilder.info] allows the user to draw using the mouse within the web browser which renders SVG. Tested on Flock and Firefox.

*update 4:42pm 28 Mar 08*
You can also view the whiteboard message queue [rorbuilder.info].

*update 6:29pm Mar 08*
I've created a short url (http://rubyurl.com/vxHD) (to demonstrate the cleaning of the whiteboard) which redirects to this http://rorbuilder.info/api/projectx.cgi?xml_project=<project name="whiteboardqueue"><methods><method name="create"><params><param var="type">ecmascript</param><param var="body">startRefresh(5)</param><param var="sender">system</param></params></method><method name="timer"><params><param var="timer">5</param></params></method><method name="archive_and_format"><params/></method><method name="create"><params><param var="type">ecmascript</param><param var="body">reloadDocument()</param><param var="sender">system</param></params></method></methods></project>

I've

a silly FIFO in Erlang (you should use OTP's queue.erl instead)

-module(fifo).
-export([new/0, loop/0, push/2, pop/1]).

new() ->
    spawn(?MODULE, loop, []).

loop() ->
    receive
        {Pid,pop} ->
            Pid ! {self(),receive {push,X} ->
                                  X
                          end),
            loop()
    end.

push(Fifo,X) ->
    Fifo ! {push,X},
    X.

pop(Fifo) ->
    Fifo ! {self(),pop},
    receive {Fifo,X} ->
            X
    end.

priority-queue functions

REBOL [
    Title: "Cheasy (Cheap-n-Easy) Priority Queue"
]

pq-insert: func [
    list [any-block!]   "The queue"
    item
    priority [integer!]
][
    sort/skip/reverse append list reduce [priority item] 2
]

pq-remove: func [
    "Remove an item from the priority queue"
    list [any-block!]   "The queue"
    /index  "Remove a specific item"
        idx [integer!] "The specific item to remove"
][
    remove/part either index [at list (idx * 2 - 1)][head list] 2
]

pq-first: func [
    list [any-block!]   "The queue"
][
    ; skip over the priority value and return the actual value
    ; that was inserted in the queue.
    first next head list
]

priority-queue: make object! [
    data: copy []

    insert: func [item priority] [pq-insert data item priority]

    remove: func [/index idx] [
        either index [pq-remove/index data idx][pq-remove data]
    ]

    first: does [pq-first data]
]



pq: copy []
print pq-insert pq "A" 1
print pq-insert pq "B" 10
print pq-insert pq "C" 100
print pq-insert pq "D" 1000
print pq-remove/index pq 3
print pq-remove pq
print mold pq
print pq-insert pq "CC" 100
print pq-insert pq "CCC" 100
print pq-insert pq "D" 1000
print pq-insert pq "CCCC" 100
print pq-insert pq "CCCCC" 100
print pq-remove pq
print pq-insert pq "CCCCCC" 100
print pq-insert pq "CCCCCCC" 100

print ""

pq: make priority-queue []
print pq/insert"A" 1
print pq/insert"B" 10
print pq/insert"C" 100
print pq/insert"D" 1000
print pq/remove/index 3
print pq/remove ;pq
print mold pq/data
print pq/insert"CC" 100
print pq/insert"CCC" 100
print pq/insert"D" 1000
print pq/insert"CCCC" 100
print pq/insert"CCCCC" 100
print pq/remove ;pq/data
print pq/insert"CCCCCC" 100
print pq/insert"CCCCCCC" 100


halt

Priority Queue

pq = PriorityQueue()

pq.put(('b', 1))
pq.put(('a', 1))
pq.put(('c', 1))
pq.put(('z', 0))
pq.put(('d', 2))

while not pq.empty():
  print pq.get(),   
# z b a c d

Get the implementation of Priority Queue here.
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS