<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Aquabu - Home</title>
  <id>tag:www.aquabu.com,2008:mephisto/</id>
  <generator uri="http://mephistoblog.com" version="0.7.3">Mephisto Noh-Varr</generator>
  <link href="http://www.aquabu.com/feed/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://www.aquabu.com/" rel="alternate" type="text/html"/>
  <updated>2008-07-31T18:35:02Z</updated>
  <entry xml:base="http://www.aquabu.com/">
    <author>
      <name>Noah</name>
    </author>
    <id>tag:www.aquabu.com,2008-07-16:237</id>
    <published>2008-07-16T09:25:00Z</published>
    <updated>2008-07-31T18:35:02Z</updated>
    <category term="Ruby"/>
    <category term="Ruby on Rails"/>
    <link href="http://www.aquabu.com/2008/7/16/scruffy-ruby-gem-debugging" rel="alternate" type="text/html"/>
    <title>Scruffy Ruby Gem Debugging</title>
<content type="html">
            !http://www.aquabu.com/assets/2008/7/16/cast.jpg!

In the process of creating some graphs in ruby I decided to go upstream from ruport and use the scruffy gem directly. I made some discoveries along the way that touch on issues that could be encountered by various permutations of scruffy, macport, and rubygems users.

h2. A Storm Brewing

The first issue I encountered was this error:
&lt;pre&gt;&lt;code&gt;testing_scruffy.rb:2:Warning: require_gem is obsolete.  Use gem instead.
/opt/local/lib/ruby/vendor_ruby/1.8/rubygems.rb:304:in `report_activate_error': Could not find RubyGem Scruffy (&gt;= 0.0.0) (Gem::LoadError)
	from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems.rb:238:in `activate'
	from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems.rb:76:in `active_gem_with_options'
	from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems.rb:61:in `require_gem'
	from testing_scruffy.rb:2&lt;/code&gt;&lt;/pre&gt;

This seems like a straightforward case of not installing the gem. However, in my case the the gem *was* installed and there was additional strange behavior: require 'scruffy' would load the library in irb but not from a script (even with require 'rubygems' before it).This seemed inconsistent because $LOAD_PATH (aka $:) variables were identical in irb and in the script. After investigating a number of possibilities I updated rubygems (which was at 0.9.4) to 1.2.0 and this seem to resolve the blatantly bizarre behavior.

You can check which version of ruby gems you are running with &quot;gem -v&quot;. If you are using macports you may have an older version of ruby gems, in which case you may find it useful to update rb-rubygems. Caveat emptor: dependent updates may also be installed - such as ruby. Here's how you can update your macport rb-rubygems library on OS X:

&lt;pre&gt;&lt;code&gt;sudo port upgrade rb-rubygems&lt;/code&gt;&lt;/pre&gt;

h2. Scruffy Needs A Shave

If you do upgrade ruby gems you may run into the another issue. With an updated version of ruby gems the require_gem method is obsolete. Because of this you may see the following error since scruffy 0.2.2 is not updated to use the *gem*  method instead of *require_gem*:

&lt;pre&gt;&lt;code&gt;NoMethodError: undefined method `require_gem' for main:Object
	from /opt/local/lib/ruby/gems/1.8/gems/scruffy-0.2.2/lib/scruffy.rb:18
	from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:32:in `gem_original_require'
	from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:32:in `require'
	from (irb):3&lt;/code&gt;&lt;/pre&gt;

I used the hack below to get around this and to avoid unpacking the gem in to my project directory for modifications. Once again caveat emptor - you may ultimately be better off unpacking (freezing) the gem into a local directory and including it in your application directly. Here's the hack which simply creates an alias to suspend the deprecation of the require_gem method indefinitely:

&lt;pre&gt;&lt;code&gt;require 'rubygems'
alias require_gem gem #later versions of gem use gem instead of require_gem
require 'scruffy'&lt;/code&gt;&lt;/pre&gt;

h2. Case Of The Missing Pie

Ah, but that's not all. It appears that tutorials and documentation are out of sync with the rubygem download... see example code at &quot;http://scruffy.rubyforge.org/&quot;:http://scruffy.rubyforge.org/ which suggests this usage (with my require hack at the top):

&lt;pre&gt;&lt;code&gt;require 'rubygems'
alias require_gem gem
require 'scruffy'

graph = Scruffy::Graph.new
graph.title = &quot;Favourite Snacks&quot;
graph.renderer = Scruffy::Renderers::Pie.new

graph.add :pie, '', {
      'Apple' =&gt; 20,
      'Banana' =&gt; 100,
      'Orange' =&gt; 70,
      'Taco' =&gt; 30
}

graph.render :to =&gt; &quot;pie_test.svg&quot;
graph.render :width =&gt; 300, :height =&gt; 200, :to =&gt; &quot;pie_test.png&quot;, :as =&gt; 'png'&lt;/code&gt;&lt;/pre&gt;

Running the sample code throws this error:

&lt;pre&gt;&lt;code&gt;test_scruffy.rb:7: uninitialized constant Scruffy::Renderers::Pie (NameError)&lt;/code&gt;&lt;/pre&gt;

Well, this error occurs because there is no such class as Scruffy::Renderers::Pie. It's not in the docs and if you do grep -r &quot;pie&quot; . from within the scruffy-0.2.2 folder it comes up with nothing that would lead to the creation of such a class. FYI, the docs are visible online here &quot;http://scruffy.rubyforge.org/doc/&quot;:http://scruffy.rubyforge.org/doc/

Well, it's hard to use a class that doesn't exist. It appears that this may be present in scruffy 0.2.3 released on July 13th - but as of this writing it's not downloaded via sudo gem update scruffy. If you need this and it it is not yet available through ruby gems - you may need to do a manual install. In the mean time let's try something else instead.

h2. Steady Into Port (Mind The Wireshark)

&lt;pre&gt;&lt;code&gt;graph = Scruffy::Graph.new
graph.title = &quot;Sample Line Graph&quot;
graph.renderer = Scruffy::Renderers::Standard.new

graph.add :line, 'Example', [20, 100, 70, 30, 106]

graph.render :to =&gt; &quot;line_test.svg&quot;
graph.render  :width =&gt; 300, :height =&gt; 200, :to =&gt; &quot;line_test.png&quot;, :as =&gt; 'png'&lt;/code&gt;&lt;/pre&gt;

Well if you have RMagick installed that may have worked for you. If not you will need to make sure rmagick is installed. On my machine I had to remove some dependencies before reinstalling tiff with different compilation options. Here are the commands I ran. You will probably have different dependencies, so know what you are doing. Don't just blindly run these commands or you may uninstall something you need:

&lt;pre&gt;&lt;code&gt;sudo port uninstall wireshark
sudo port uninstall gtk2
sudo port uninstall tiff @3.8.2_1+macosx
sudo port install tiff -macosx imagemagick +q8 +gs +wmf
sudo gem install rmagick&lt;/code&gt;&lt;/pre&gt;

Note that installing and compiling rmagick took several minutes. I would suggest looking at the &quot;rmagick installation page&quot;:http://rmagick.rubyforge.org/install-osx.html if you need to install this. 

At this point you should be able to output your svg and png files.

h2. Conclusion
 
Careful, or due to &quot;environmental conditions&quot; you could set out expecting a simple three hour tour happily outputting beautifully rendered svgs as you sail effortlessly through graph land; but instead, end up whistling along sympathetically to the Gilligan's Island theme song as you set ground on the shore of an uncharted desert isle. Hopefully this will help you through some of the things you may run into, and you can have a smooth sail enjoying scruffy.
          </content>  </entry>
  <entry xml:base="http://www.aquabu.com/">
    <author>
      <name>Noah</name>
    </author>
    <id>tag:www.aquabu.com,2008-06-18:233</id>
    <published>2008-06-18T17:42:00Z</published>
    <updated>2008-06-18T17:42:49Z</updated>
    <category term="Ruby"/>
    <category term="Ruby on Rails"/>
    <link href="http://www.aquabu.com/2008/6/18/proc-new-lambda-and-proc" rel="alternate" type="text/html"/>
    <title>Proc.new, lambda, and proc</title>
<content type="html">
            In response to the &quot;harmony of thought posting on good ruby interview questions&quot;:http://blog.ritirisi.com/2008/06/17/15-questions-to-ask-during-a-ruby-interview I wrote the following (posted here for interest and to preserve formatting)...

One thing about lambdas and procs. Even though they appear to be the same class they do not behave completely the same. From running my_lambda.inspect and seeing the Proc instance you would never guess it but they are different.

Essentially lambda and Proc.new handle arguments and the &quot;return&quot; statement differently. Also the &quot;proc&quot; method returns a lambda not a Proc.

Here's some code to prove the point. The argument handling is the easiest to demo:

&lt;pre&gt;&lt;code&gt;my_proc = Proc.new {|x,y| print x,y}
my_lambda = lambda {|x,y| print x,y}
my_other_proc = proc {|x,y| print x,y}

# pass in one variable instead of 2
my_proc.call(1) #this doesn't throw an error and sets y to nil
my_lambda.call(1) #this throws an error
my_other_proc.call(1) #this throws an error like a lambda because it actually is a lambda not a proc in ruby 1.8.6&lt;/code&gt;&lt;/pre&gt;

Also in Ruby 1.9 I think that the &quot;proc&quot; method (as seen in line three) will actually return a Proc and not a lambda. Ruby 1.9 will also add a &quot;lambda?&quot; method for you to clarify the difference between a Proc and a lambda.

O'Reilly's book &quot;The Ruby Programming Language&quot; by Matz (Yukihiro Matsumoto) and David Flanagen has a great chapter on this.
          </content>  </entry>
  <entry xml:base="http://www.aquabu.com/">
    <author>
      <name>Noah</name>
    </author>
    <id>tag:www.aquabu.com,2008-06-13:232</id>
    <published>2008-06-13T23:43:00Z</published>
    <updated>2008-06-16T21:44:32Z</updated>
    <category term="Class &amp; Event Notes"/>
    <link href="http://www.aquabu.com/2008/6/13/hosting-and-the-woes-engine-yard-panel" rel="alternate" type="text/html"/>
    <title>Railsconf 2008 Scribblings: Hosting and the Woes (Engine Yard Panel)</title>
<content type="html">
            5/30/2008, 11:45AM, Portland, OR&lt;br /&gt;

Here are some of my notes from Railsconf 2008. Warning these notes are only vaguely edited (but arguably still useful). There are certainly typos and errors. If executing code from one of these articles, your mileage may vary (from spontaneous self destruction of everything you know and love to spontaneous coolness). Also, you may want to double check “facts”. You definitely should not use these articles as a spelling reference. Feel free to post corrections.

Engine Yard Panel:
* Tom Mornini - CTO
* Taylor Weibley - Application Support Director
* Edward Muller - Automation Manager (fluent in binary)
* Ezra Zygomowitz -  System Architect
* Jamie van Dyke

Problems they have had and how they solved most of them. Then questions and answers.

h2. Main problem with rails is active_record

Great for quickly building with RESTful resources. But...
* find(:all).each do |leak|
* testing is usually with 20 records.
* no indexes.

Engineyard has a team of SQL guys to optimize in this area. 

One of the first problem they noticed that you need indexes on all foreign keys. If you don't have them they are full table scans. Migrations automatically index ids but not foreign keys. Basically anything that ends with _id needs an index. There may be some database experts that will disagree but this is not usually the case. Use explain in mysql /G to make sure that the indexes are being used in the queries. Take a look at the new Relic optimization stuff it's super cool.

h2. Plugins that give common problems

* Most common and hated plugin is ferret. Indexes grow past 1G and indexes frequently corrupted. Use sphinx instead. Sphinx will just update indexes on the delta.
* Image Science uses ruby inline which needs environment variables to be set. Can be used perfectly in development and then when they shift to production monit clears those variables out. Not a huge problem but you should be aware of it.
* Hodel 3000 loggers default is a verbose logging tool and it can generate huge log files. Adjust settings. Logging should be set to info.

h2. How much traffic can you take?

*Depends on coding* 

General stats:
* Digg 10,0000+ visitors and 1% signup. What should you do? Cache it. At engine yard a slice could handle this quite easily. Customers are often concerned with this sudden out of the blue Digg or TechCrunch. But in reality you usually are and there is no surprise. Makes it so you can be ahead of the curve.
* TechCrunch 1000+ visitors but they are more interested in what you are doing.
* Ruby Inside / Flow 500 - 1000 visitors a day, also don't get many sign ups. It's more about what can the visitor do.
* The Today Show - 100,000+ visitors in the first hour or so. Can get up to 10,000 signups in the first couple hours. You will have advance warning which is great.
* Fox Business - 2000+ connections.

h2. How many queries can your site take? 

It depends on code.
* Queries per page
* But you should cache!
* Best way to handle hike in traffic is cache / memcache
* Keep file input output down as low as possible
* Separate out to different disks, you don't want simultaneous reads and writes

h2. Deployment

Lots of customers not up to speed on Capistrano, git / subversion. Engine yard gives deploy.rb (shameless plug from them :-)
gem source - a http://www.engineyard.com ... Includes cap examples list of extra tasks - loads of things that it can do.

h2. ebb | mongrel | thin. Which do customers prefer?  

Alternatives to mongrel are possible but not necessarily production ready (ebb). Don't need to be super concerned about it unless you are doing 1,000 hits per second. The bottleneck is usually the user code, this is far more relevent. That said, thin may be 13% faster but you need to be in the top 5% of internet traffic for this to matter.

h2. Audience Q&amp;A

What happened with NGINX?
You haven't heard about much new because it's in use and it just works. They haven't really had any problems with NGINX. Some minor issues but they have been ironed very quickly. They have seen 40MB/sec of static images served and not show up top command.

Interpreters?&lt;br /&gt;
Rubinius and Modrubinius are going to be the way to go in the future.

Thin instead of mongrel because of lower memory footprint?&lt;br /&gt;
There are a lot reasons to save memory but mongrel is a minor concern. More than 3-4 mongrels per CPU is pointless. The bottleneck is file io rather than memory. A common misconception is that you need more mongrels. Thin tends to backup processes more than mongrel. It is important to watch the real behavior of the app because every app uses resources differently.

Should static files be local or shared across cluster?&lt;br /&gt;
Cluster is fine @ Engine Yard, but depends on file system. NFS was cool in 1979...

How to handle asynchronous background processes?&lt;br /&gt;
Background Job (BJ) is pleasurable to work with. Written by NGINX.

Do you leave keep alive with NGINX?&lt;br /&gt;
Untested. But historical use with apache pre-rails was not succesful.

Seperate server for search workload? How should resources be distributed across server farm and how does this map to virtualization?
Engine Yard are strong believers of virtualization. Intended from earlier on to only have virtualized servers. The more that they have gotten involved in it they have discovered that if you have a well built virtualization environment you quickly get the sense that you are operating on separate machines. NGINX, 3 Mongrels, and an memcache per instance. If you have a solid architecture you could have databases on multiple instances. Single server rack at engine yard with 18 servers would run bottom 97% of all internet apps. There's huge CPU capacity in a virtualization environment. CPUs are not typically the bottleneck (unless you are doing fluid dynamic calculations).

What is the Engine Yard uptime?&lt;br /&gt;
3-4 9s up time. Rack Space rates using a questionable rating system.
          </content>  </entry>
  <entry xml:base="http://www.aquabu.com/">
    <author>
      <name>Noah</name>
    </author>
    <id>tag:www.aquabu.com,2008-06-13:231</id>
    <published>2008-06-13T23:26:00Z</published>
    <updated>2008-06-13T23:27:53Z</updated>
    <link href="http://www.aquabu.com/2008/6/13/maglev-ruby-that-scales-talk-with-avi-bryant-and-bob-walker" rel="alternate" type="text/html"/>
    <title>Railsconf 2008 Scribblings - MagLev: Ruby That Scales</title>
<content type="html">
            Talk with Avi Bryant and Bob Walker&lt;br /&gt;
5/30/2008, 3:40PM, Portland, OR&lt;br /&gt;

Here are some of my notes from Railsconf 2008. Warning these notes are only vaguely edited (but arguably still useful). There are certainly typos and errors. If executing code from one of these articles, your mileage may vary (from spontaneous self destruction of everything you know and love to spontaneous coolness). Also, you may want to double check &quot;facts&quot;. You definitely should not use these articles as a spelling reference. Feel free to post corrections.

MagLev is rails that go fast. Typically have mongrel cluster talking to storage device and memcache instance for each server. Maglev unifies this stack: Storage, Cache, and Ruby

Example:
&quot;Ordinary ruby shells&quot;. Two terminals using the same shared objects! You can do this for any number of boxes across the entire system.
Local state is not shared, global variables ($hat).

Ruby shell has transactions as you are used them. Transactions are there for mutable state.Commits don't only go to in memory caching, they go to disk and if there is a failure then the machines can be rebooted with the existing state. Could this be on multiple machines? Yes. The storage can be greater than ram because they can be persisted to disk.

The system does not currently run rails, but it will be soon. Can currently run webrick with hello world.

Question: Is the ruby spec implemented and at what level?
Next phase is implementing the ruby specs against this.

Question: What happens to files and sockets?
Each VM will have it's own seperate copy of that resource and they will be nil'ed out when brought over.

Question: Is this a written from scratch ruby machine?
Yes, it is not MRI.

Gemstone founded in 1982 in Portland.
* Cretated GemStone smalltalk
* Enterprise proven technology
* Production systems deployed worldwide
* Support domains that are not well served by relational database systems

* Worldwide container
* Intelligence
* Telecom
* Etc.

MagLev
* 1 dual core up to really large systems
* Mutliple ruby virtual machines
* Oject Caches/Repository
* Transactional
* Distributed
* Scalable

They are currently 100 days into the implementation process.
* Dynamic Language VM
* Based on Gemsone/s 64VM
* Well tuned garbage collection
* Extended with new byte codes for Ruby
* JIT native code generation

Object Caching
* Shared caching uses shared memory
* Repository objects are read into the cache
...

Transactional
* Fully recoverable ACID Transactions
* Simple API
* Conflict management

Persistence
* Transparent
* Native Language
* Simple API
* You can store a trillion instances
* Up to 17PB

Benchmarks
* bm_so_exceptions 5.6 x faster than MRI
* fib 12.5 x MRI
* vm2 method 24 x MRI
* bm_so_sieve 68.4 times
* bm_loop_whileloop2 111.5 x MRI
&quot;Holy Shit&quot; -Antonio Gangiano (Great Ruby Shootout)
Numbers will be independently verified shortly.

Audience question. What is missing from Ruby that will decrease this performance?
The plan is ruby spec and core library.

But does it scale?
Yes.

What's the vision?
Rails - taking it to a different level of scalability.

What Else?
* Open Community Involvement
* Ruby Spec Compliance
* MagLev Ruby open source
* Free version of MagLev will be feature limited for small to medium scale
* Info @ maglev.gemstone.come

A lot of rails makes use of the relational database structure and indices. Will Maglev address these?
Yes. And you will be able to make your queries in the native language. Will likely clone the API but run gemstone underneath.

Continuations?
Yes. They are supported in smalltalk.

How to optomize? Will it be a linear scan?
Gemstone does will do an linear scan for a basic select but gemstone does have various indexing.

Will the Gemstone VM be open sourced (asked by Charles Nutter from jRuby team):
Bob: &quot;In a world of infinite possibilities it's possible.&quot;

Will existing extensions in C be possible to integrate?
Not currently. Rubinius has done some good work in this area and it may be possible to use a similar approach for Gemstone. C extensions may not be necisary in certain cases.

However rails depends on C like BigDecimal(Wilson)?
Will solve those problems when they get to them.

ActiveRecord uses SQL fragments. How would that work since it's not part of ruby?
To port a rails app may require getting rid of optomizations done in sql.... this is an area where feedback will be welcome. This is yet to be designed built.

Which pieces are open source?
All of the stuff at the ruby level is hopefully to be shared with Rubinius. ActiveRecord API will be. But as for the underlying VM Bob answered that earlier (e.g. &quot;in a world of infinite possiblities....&quot;).

Is the persistent store distributable?
More like typical SQL statement. Master slave replication...

How is the VM implemented?
In short... in C.

When is going to be ready?
Pay attention to what we are doing. More info in September.
          </content>  </entry>
  <entry xml:base="http://www.aquabu.com/">
    <author>
      <name>Noah</name>
    </author>
    <id>tag:www.aquabu.com,2008-06-13:230</id>
    <published>2008-06-13T23:17:00Z</published>
    <updated>2008-06-13T23:17:47Z</updated>
    <category term="Class &amp; Event Notes"/>
    <link href="http://www.aquabu.com/2008/6/13/railsconf-2008-scribblings-joel-spolsky-keynote" rel="alternate" type="text/html"/>
    <title>Railsconf 2008 Scribblings: Joel Spolsky (Keynote)</title>
<content type="html">
            5/30/2008, 9:15am, Portland, OR

Here are some of my notes from Railsconf 2008. Warning these notes are only vaguely edited (but arguably still useful). There are certainly typos and errors. If executing code from one of these articles, your mileage may vary (from spontaneous self destruction of everything you know and love to spontaneous coolness). Also, you may want to double check &quot;facts&quot;. You definitely should not use these articles as a spelling reference. Feel free to post corrections.

Intro by DHH on Joel:
* Joel is author of Joel On Software
* Generated programming language to generate other programming language
* fighting complexity (anti-architect astronaut)

h2. Joel's Talk Begins...

Why does Brad Pitt make so much more money and have a bigger brand than Ian Somerhalder?
What becomes &quot;blue chip software&quot; rather than off brand?

Herman Miller Aeron Chair was the bluechip chair. All types of people make clones.
Clones didn't have market share. This happens in every field.
Happens in music as well.

Great software has three components that make them blue chip:
* Make people happy
* Obsess over aesthetics
* Observing the culture code

h2. Make People Happy (Agency)

[Very funny example about logging in to windows to post a photo and then having to install windows downloads and then use original install a file from the insert original install disk. In a word you feel enslaved (you are not in control of the process)]

Martin Seligman pioneering psychologist of &quot;Learned Helplessness&quot; liked to study happy people which is rare amongst psychologists. 

Example: Someone dies &gt; you are stuck in bed depressed &gt; you loose your job and that you begin to believe that you lack agency and then you begin to teach yourself that you are helpless. Seligman's strategy was to teach people that they have power over there environment through tricking the brain into a virtuous cycle of having agency (initially through accomplishment of simple tasks).

Abercrombie ecommerce checkout forces you through 4 pages. Amazon gives you all the options at once (change address, shipping options, etc). This is Joel's first example of giving the user agency.

Make people happy by putting them in control (speed of response is also important here - the business case for AJAX).

h2. Obsess over aesthetics

Samsumg Blackjack vs iPhone.
* Blackjack faster
* iphone bricks if you install third party software
* Blackjack ugly &amp; iphone is seamless and shiny (if you accidentally swallowed one it would go right down)
* Blackjack replaceable battery - iphone doesn't (avoids the non aesthetic seam). Another example is looking at the bottom of the MacBook vs. thinkpad.

Apple design decisions are about fashion. Another example is a historical Paris apartment building in Paris has no fire escapes. It turns out fire escapes are zoned, but the appartment buildings are reclasified as monuments and they are not inspected. This is another example of taking aesthetics as more important than core functionality.

Programers call this lipstick and say I want to work in the guts (the 90% of the iceberg below the surface). But it turns out that aesthetics are very important. To programmers it is not clear why artists such as Basquaiat etc. sell paintings for $50+ million. Specifically programmers don't get modernist architecture.

Modernist principles remove all decoration from architecture. There is a parallel here between the programers gravitation to the command line vs. glossy operating systems.

h2. The Culture Code

Example: the promotion of sport utility vehicles.
Deaths per million cars:
* Toyota Camry 41
* Ford Explorer 88

This has to do with a number of factors. But you feel safe in these vehicals. This has to do with promotion (see the culture code).
* Everything around you should be round and soft
* You should be up high (on the reptilion level if you feel tall you feel you are dominating)
* Key element of safety as a child is being fed if everything is round, soft, high, and you feel fed (cupholders) you feel safe.

Another example like this is the word &quot;Enterprise&quot;. Recalls tokyo at night, a clean server room, the starship enterprise, people in clean business suits looking up(image).

Another example is Web 2.0. Lots of logos. They don't have visions they go to parties... etc. Why the luck stiff :-) 
How could beauty, happiness, motivation, pride, pleaseure, enthusiasm be involved in coding.
Very funny counter examples for python and java go here...

This all boils down to Misattribution. You have a physical reaction and you think that it has something to do with the mental plane but it doesn't. People on the right hand side of the movie have 10% greater ratings than the left.
          </content>  </entry>
  <entry xml:base="http://www.aquabu.com/">
    <author>
      <name>Noah</name>
    </author>
    <id>tag:www.aquabu.com,2008-05-27:198</id>
    <published>2008-05-27T07:55:00Z</published>
    <updated>2008-05-29T06:21:38Z</updated>
    <link href="http://www.aquabu.com/2008/5/27/installing-vim-with-ruby-or-enable-rubyinterp-on-os-x-10-5-2" rel="alternate" type="text/html"/>
    <title>Installing vim with +ruby or --enable-rubyinterp on OS X 10.5.2</title>
<content type="html">
            The problem: When installing vim from source with --enable-rubyinterp option you are receiving numerous unsolvable errors (such as ncurses cannot be found when it is installed and you are directing ./configure to it using --with-tlib ncurses). Or, you are trying to install vim +ruby using MacPorts and you are getting similar errors. 

The solution: install vim using MacPorts and install Ruby first.
&lt;pre&gt;&lt;code&gt;sudo port install ruby
sudo port install vim +ruby +huge&lt;/code&gt;&lt;/pre&gt;

Newly installed version is in /opt/local/var/macports/software/vim/7.1.270_0+huge+ruby/opt/local/bin

After launching this version of vim it responds to:
&lt;pre&gt;&lt;code&gt;:ruby puts &quot;hello world!&quot;&lt;/code&gt;&lt;/pre&gt;

If you have the following in your .bash_profile or .profile file and you reopen your terminal, the newly installed MacPort will take precedence over the default OS X install of vim:
&lt;pre&gt;&lt;code&gt;export PATH=/opt/local/bin:/opt/local/sbin:$PATH&lt;/code&gt;&lt;/pre&gt;

Note that having your MacPort ruby install take precedence over your default ruby installation can modify your environment. Particularly, it may cause ruby to look for a different gem library. You can verify you are using the correct configuration by checking your gem path, version, and gems that you have installed:
&lt;pre&gt;&lt;code&gt;which gem
gem -v
gem list --local&lt;/code&gt;&lt;/pre&gt;

If your MacPorts gem version is taking precedence over other gem libraries that you would prefer to use, you can modify the file name or remove the version of the gem that you do not want to use. It is likely that MacPorts is referencing /opt/local/bin/gem and you would like it to reference /usr/bin/gem.
          </content>  </entry>
  <entry xml:base="http://www.aquabu.com/">
    <author>
      <name>Noah</name>
    </author>
    <id>tag:www.aquabu.com,2008-02-16:13</id>
    <published>2008-02-16T20:00:00Z</published>
    <updated>2008-02-19T01:01:41Z</updated>
    <category term="Erlang"/>
    <link href="http://www.aquabu.com/2008/2/16/fibonacci-sequence-recursion-in-erlang" rel="alternate" type="text/html"/>
    <title>Fibonacci Sequence Recursion in Erlang</title>
<content type="html">
            Erlang uses recursion extensively - so lets do some classic &quot;Fibonacci sequence&quot;:http://en.wikipedia.org/wiki/Fibonacci_number recursion code katas for Erlang. Note, that I define things a little differently than the Wikipedia example I just linked to. In the algorithms below, I have defined the first first two elements as = 1 and there is no N defined for 0. So: fib(1) = 1, fib(2)=1, and fib(N) where N &gt; 2 = fib(N -1) + fib(N - 2).

For fun, lets try a few different algorithmic approaches to this sequence. The classic example of Fibonacci sequence recursion is a literal representation of the algorithm, which ends up being rather computationally intensive. It looks rather elegant but starts getting massively slow around N = 35. This is because N is recomputed recursively every time it is used!

&lt;pre&gt;&lt;code&gt;fib_slow(1) -&gt; 1;
fib_slow(2) -&gt; 1;
fib_slow(N) -&gt; fib_slow(N-1) + fib_slow(N-2).&lt;/code&gt;&lt;/pre&gt;

Here's an example of a different algorithm, where each value is accumulated in a list and looked up rather than recomputed. There also is a io:format print statement that returns the final sequence of N numbers. This method is a lot faster than the inefficient version above. You can set N = 1,000 without much fuss. If you comment out the print statement you can get to N = 20,000 speedily.

&lt;pre&gt;&lt;code&gt;fib_list(1) -&gt; io:format(&quot;~p~n&quot;,[[1]]);
fib_list(2) -&gt; io:format(&quot;~p~n&quot;,[[1,1]]);
fib_list(N) when N &gt; 2 -&gt; 
	hd( fib_list1(N-1,[1,1]) ).

% delegate for numbers greater than 2
fib_list1(1,L) -&gt;
	io:format(&quot;~p~n&quot;,[lists:reverse(L)]),
	L;
fib_list1(N,L) -&gt;
	[H1,H2|_T] = L,
	fib_list1(N-1,[H1 + H2 | L]).&lt;/code&gt;&lt;/pre&gt;

The final method computes the value but does not store it in a list. This one is the fastest (although I have not benchmarked them). This method also eliminates the risk of filling up memory with the list of previous numbers. N = 50,000 breezes by with this algorithm on my 2.2 Ghz Intel Dual Core MacBook Pro.

&lt;pre&gt;&lt;code&gt;fib(1) -&gt; 1;
fib(2) -&gt; 1;
fib(N) when N &gt; 2 -&gt; fib1(N,1,1).

fib1(3,P1,P2) -&gt; P1 + P2; 
fib1(N,P1,P2) -&gt;
	fib1(N-1,P2, P1 + P2).&lt;/code&gt;&lt;/pre&gt;

Here are all of the different approaches (in reverse orer) in a module.

&lt;pre&gt;&lt;code&gt;-module(fib2).
-compile(export_all).
% get nth fibonacci no storage
% quite fast, get's nth number with no list storage

test() -&gt;
	13 = fib(7),
	13 = fib_list(7),
	13 = fib_slow(7),
	horray.

fib(1) -&gt; 1;
fib(2) -&gt; 1;
fib(N) when N &gt; 2 -&gt; fib1(N,1,1).

fib1(3,P1,P2) -&gt; P1 + P2; 
fib1(N,P1,P2) -&gt;
	fib1(N-1,P2, P1 + P2).
	
% fib sequence made by accumulating values in a list
% pretty fast even at N = 20,000.
fib_list(1) -&gt; io:format(&quot;~p~n&quot;,[[1]]);
fib_list(2) -&gt; io:format(&quot;~p~n&quot;,[[1,1]]);
fib_list(N) when N &gt; 2 -&gt; 
	hd( fib_list1(N-1,[1,1]) ).

%delegate for numbers &gt; 2
fib_list1(1,L) -&gt;
	io:format(&quot;~p~n&quot;,[lists:reverse(L)]),
	L;
fib_list1(N,L) -&gt;
	[H1,H2|_T] = L,
	fib_list1(N-1,[H1 + H2 | L]).

% ineficient recursive method for fibonacci computation
% very slow at N = 35
fib_slow(1) -&gt; 1;
fib_slow(2) -&gt; 1;
fib_slow(N) -&gt; fib_slow(N-1) + fib_slow(N-2).&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://www.aquabu.com/">
    <author>
      <name>Noah</name>
    </author>
    <id>tag:www.aquabu.com,2008-02-15:12</id>
    <published>2008-02-15T19:07:00Z</published>
    <updated>2008-02-16T19:08:18Z</updated>
    <category term="Class &amp; Event Notes"/>
    <category term="Erlang"/>
    <link href="http://www.aquabu.com/2008/2/15/erlang-pragmatic-studio-day-3-notes" rel="alternate" type="text/html"/>
    <title>Erlang Pragmatic Studio - Day 3 Notes</title>
<content type="html">
            &lt;p&gt;Erlang Pragmatic Studio with Joe Armstrong &#38; Dave Thomas &#8211; Chicago, Feb 13-25, 2008.&lt;/p&gt;


	&lt;h2&gt;Day 3 &#8211; Feb 14,2008.&lt;/h2&gt;


	&lt;p&gt;Here are my notes from Day 3 of the Pragmatic Studio Erlang course. Note that these notes are only vaguely edited. Be aware that these are notes so there are certainly typos and errors.&lt;/p&gt;


	&lt;h2&gt;Beginning of the Day&lt;/h2&gt;


	&lt;p&gt;Always good to develop from the specific to the general. e.g. make a simple app, then parameterize it with functions. Universal server that evaluates F is a very powerful generalization of a specific application (e.g. messaging).&lt;/p&gt;


can send a message to your self:
&lt;pre&gt;&lt;code&gt;self ! a. % Could tie into recursion&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;a lot of users
Erlang guru&#8217;s (tailf, Kredita) don&#8217;t use &lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt;, as they participated in the &lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt; creation. But if you build up the Gen server from incremental steps, you may find it not so difficult. The question is, does &lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt; really fit the problem at hand.&lt;/p&gt;


	&lt;p&gt;If you do spawn_link from the shell, report errors back to the shell.&lt;/p&gt;


	&lt;p&gt;erl -bootstart sasle % spelling system application support library. Get error logger and a few other items.&lt;/p&gt;


	&lt;p&gt;sasle libraries were added into Erlang at a later date for historical reasons.&lt;/p&gt;


	&lt;h2&gt;Bit Syntax&lt;/h2&gt;


In ruby or other languages would be fairly tedious (ands, ors, bitshifts, etc). Erlang has a high level syntax that lets you specify the structure of binary data.
&lt;pre&gt;&lt;code&gt;Red = 2, Green=61, Blue=20.
=&gt; 20
Bin = &amp;lt;&amp;lt;red:5&gt;&gt;
=&gt; &amp;lt;&amp;lt;23&gt;&gt;
io:format(&quot;~8.2.0B~8.2.0b~n&quot;, binary_to_list(Bin)).
=&gt; 00010111 10110100&lt;/code&gt;&lt;/pre&gt;

	&lt;h2&gt;Bit Twiddling History:&lt;/h2&gt;


	&lt;p&gt;Erlang had lots of protocols internally in Erickson. Decided to implement every &lt;span class=&quot;caps&quot;&gt;RFC&lt;/span&gt; &#8211; but was too difficult. Lots of bit twiddling. Because of this they made a sub-language for doing this bit-twiddling so that this was implement any protocol. Optimal and efficient unpacking of datastructures.&lt;/p&gt;


	&lt;p&gt;1/3rd of details were in the Erlang book. The rest needs to be found in the man pages.&lt;/p&gt;


	&lt;p&gt;There are also bit comprehensions &#8211; generators of bit stream. Good for huffman encodings, &lt;span class=&quot;caps&quot;&gt;MIPS&lt;/span&gt; processing, etc. Very efficient. This is in the experimental phase but will be integrated into the system. Undocumented items are not official and may be changed. Once the docs are written it&#8217;s official.&lt;/p&gt;


	&lt;p&gt;Erlang mailing list erlang.org &amp;gt; FAQs and mailing lists.&lt;/p&gt;


	&lt;p&gt;Book page 86 great shoutcast server for distributed audio. There is an excercise for bit processing, but we are skipping it in the interest of time.&lt;/p&gt;


	&lt;h2&gt;file at a time I/O&lt;/h2&gt;


	&lt;p&gt;Reading file at at time is about 10-20 times faster than reading line at a time. Erlang is not as efficient as some other languages for string matching. Use [H|T] to parse. Only multi-gig pattern are difficult for this. binary input and output is definitely fastest.&lt;/p&gt;


	&lt;p&gt;file:consult(File) -&amp;gt; {ok, Term} | {error, Why}&lt;/p&gt;


Term Access &#8211; read configuration file
&lt;pre&gt;&lt;code&gt;-module(test4).
-
read_config() -&gt;
    {ok,[{host,Host,port,Port}]} = file:consult(...)
    ...&lt;/code&gt;&lt;/pre&gt;

Term IO
	&lt;ul&gt;
	&lt;li&gt;inefficient&lt;/li&gt;
	&lt;/ul&gt;


&lt;pre&gt;&lt;code&gt;io:read('enter a term').
enter a term &gt; {hello, &quot;joe&quot;}
io:get_line(...) %read from a shell

erl_scan:string(&quot;abc,123,{hello,joe}&quot;). % turn into erlang tokens - very efficient
% written with explicit recursion that is more efficient than regex

file:pread ...&lt;/code&gt;&lt;/pre&gt;

map onto standard file manipulations:
&lt;pre&gt;&lt;code&gt;file:list_file(Dir)
file:delete()
...

filename:split(FileName) =&gt; [Component]&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;DNS&lt;/span&gt; servers are actually fairly easy. In a real &lt;span class=&quot;caps&quot;&gt;DNS&lt;/span&gt; server there is tree walking for the sub-domain, domain, and tld xxx.xxxx.xxx&lt;/p&gt;


	&lt;p&gt;lots of &lt;span class=&quot;caps&quot;&gt;DNS&lt;/span&gt; servers have interesting traffic patterns&#8230; tie ins with advertising sites, etc.&lt;/p&gt;


	&lt;p&gt;Erlang doesn&#8217;t link code into it&#8217;s Kernal. Need to establish a socket for C libs. Send messages using binary.&lt;/p&gt;


Orientation:
	&lt;ul&gt;
	&lt;li&gt;nodes &#8211; when you say erl it starts a node. Doesn&#8217;t register this node by default.&lt;/li&gt;
		&lt;li&gt;distributed erlang consists of a number of nodes that know about eachother.&lt;/li&gt;
		&lt;li&gt;&lt;span class=&quot;caps&quot;&gt;SMP&lt;/span&gt; takes advantage of multi-core &#8211; this is not distributed Erlang&lt;/li&gt;
		&lt;li&gt;Socket distribution &#8211; is also not distributed Erlang&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;same network:
erl -sname %short name
erl -name % different names&lt;/p&gt;


	&lt;p&gt;There is a magic cookie that they must share. Simple challenge request, traffic is not encrypted. Can communicate over &lt;span class=&quot;caps&quot;&gt;SSH&lt;/span&gt;. DNS and name recognition systems are often poorly configured and this causes some difficulties for setting up distributed erlang apps. There are som FAQs on debugging distributed Erlang.&lt;/p&gt;


Three new primitives:
	&lt;ul&gt;
	&lt;li&gt;spawn(Node, Mod, Func, Args) &#8211; links work the same&lt;/li&gt;
		&lt;li&gt;alive(Node) &#8211; tell the system you are alive&lt;/li&gt;
	&lt;/ul&gt;


Main libs:
	&lt;ul&gt;
	&lt;li&gt;rcp&lt;/li&gt;
		&lt;li&gt;global&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Test on local nodes, then over a cluster.
Dave: mnesias cool be cause you can query it using list comprehensions.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;erl -sname dave

rpc:call('joe@Daves-Powerbook', erlang, node, []).
rpc:call('joe@Daves-Powerbook', erlang, exit, [kill]).
rpc:call('joe@Daves-Powerbook', erlang, halt, []).&lt;/code&gt;&lt;/pre&gt;

Shell commands for interacting with erl nodes:
	&lt;ul&gt;
	&lt;li&gt;^g gets you into the shell&lt;/li&gt;
		&lt;li&gt;j &#8211; jobs&lt;/li&gt;
		&lt;li&gt;r &#8216;dave@Daves-Powerbook&#8217; % open a remote shell&lt;/li&gt;
		&lt;li&gt;c 2 &#8211; change to the second  process&lt;/li&gt;
		&lt;li&gt;now at (dave@Daves-Powerbook)&amp;gt;&lt;/li&gt;
		&lt;li&gt;toolbar:start(). % show current processes &#8211; queries &lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt; structure&lt;/li&gt;
	&lt;/ul&gt;


Security:
	&lt;ul&gt;
	&lt;li&gt;Security on distributed Erlang is very course grain. It&#8217;s all access or nothing.&lt;/li&gt;
		&lt;li&gt;Perfect for a fire-walled corporate cluster.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Need to shore cookie to other machines. Check your home directory &#8211; .erlang.cookie&lt;/p&gt;


	&lt;h2&gt;&lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt; &#8211; Open Telecomms Platform&lt;/h2&gt;


This is the platform / framework for building Erlang applications.
	&lt;ul&gt;
	&lt;li&gt;Error logs&lt;/li&gt;
		&lt;li&gt;Hot swapping modules&lt;/li&gt;
		&lt;li&gt;Common tasks that everyone needs&lt;/li&gt;
		&lt;li&gt;etc.&lt;/li&gt;
		&lt;li&gt;Mnesia&lt;/li&gt;
		&lt;li&gt;&lt;span class=&quot;caps&quot;&gt;SASL&lt;/span&gt;&lt;/li&gt;
		&lt;li&gt;&lt;span class=&quot;caps&quot;&gt;SNMP&lt;/span&gt; Agents&lt;/li&gt;
		&lt;li&gt;Web Server&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Lots of undocumented corners. Programming Erlang book is very helpful. It&#8217;s Open Source in the sense that you can see the source. But commit rights are controlled. It is product quality. There is no difference between Erickson&#8217;s products released and the Open Source distribution. There are 15 people working full time to maintain this &#8211; stable, battle tested.&lt;/p&gt;


&lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt; Principles
	&lt;ul&gt;
	&lt;li&gt;Joe Armstron &lt;span class=&quot;caps&quot;&gt;PHD&lt;/span&gt; Thesis&lt;/li&gt;
		&lt;li&gt;erlang.org/doc/design_principles/part_frame.html&lt;/li&gt;
		&lt;li&gt;client-server gen_server&lt;/li&gt;
		&lt;li&gt;finite state machines gen_fsm&lt;/li&gt;
		&lt;li&gt;event handling gen_event&lt;/li&gt;
		&lt;li&gt;supervisor gen_sup&lt;/li&gt;
		&lt;li&gt;Applications &#8211; e.g. mnesia, standard libraries, collections of processes shipped in it&#8217;s own right. start and stop etc.&lt;/li&gt;
		&lt;li&gt;Releases &#8211; 4-5 applications combined form a product&lt;/li&gt;
		&lt;li&gt;Application upgrade &#8211; upgrade a running system&lt;/li&gt;
	&lt;/ul&gt;


Finite state machines
	&lt;ul&gt;
	&lt;li&gt;State x Event -&amp;gt; State1 x Action&lt;/li&gt;
		&lt;li&gt;Way of writing pattern matching on state and event.&lt;/li&gt;
	&lt;/ul&gt;


gen_event
	&lt;ul&gt;
	&lt;li&gt;no reply back (error log)&lt;/li&gt;
		&lt;li&gt;event to new state, no reply&lt;/li&gt;
	&lt;/ul&gt;


Order of reading:
	&lt;ul&gt;
	&lt;li&gt;Joe&#8217;s thesis &#8211; best intro to &lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt;: sics.se/~joe/thesis&lt;/li&gt;
		&lt;li&gt;erlang.org/doc/design_principles/part_frame.html&lt;/li&gt;
		&lt;li&gt;forthcoming O&#8217;Reilly book&lt;/li&gt;
	&lt;/ul&gt;


Behaviors:
	&lt;ul&gt;
	&lt;li&gt;&lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt; name for &#8220;design patterns&#8221; &lt;/li&gt;
		&lt;li&gt;Callback module&lt;/li&gt;
	&lt;/ul&gt;


Case studies:
	&lt;ul&gt;
	&lt;li&gt;&lt;span class=&quot;caps&quot;&gt;AXD301&lt;/span&gt; &#8211; 1.6 million lines of Erlang, built using &lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt;.&lt;/li&gt;
		&lt;li&gt;Nortel Networks&lt;/li&gt;
	&lt;/ul&gt;


Factoids:
	&lt;ul&gt;
	&lt;li&gt;gen_server is the most used behavior.&lt;/li&gt;
		&lt;li&gt;supervisor bridge &#8211; allows a C module to be fault tolerant in use with Erlang.&lt;/li&gt;
		&lt;li&gt;gen_server &#8211; A generic client-server model&lt;/li&gt;
	&lt;/ul&gt;


Types of Supervision:
	&lt;ul&gt;
	&lt;li&gt;1:1 vs 1:N supervision&lt;/li&gt;
		&lt;li&gt;1:1 &#8211; if child dies only 1 is restarted&lt;/li&gt;
		&lt;li&gt;1:N &#8211; if challed dies all child sibling processes restarted&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;trapexit.org is a good site.&lt;/p&gt;


Distributed applications:
	&lt;ul&gt;
	&lt;li&gt;Must have the same beam code on all machines. There are checksums.&lt;/li&gt;
		&lt;li&gt;Must have the same version of Erlang.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;One way of solving this is to have the share the same backend &lt;span class=&quot;caps&quot;&gt;NFS&lt;/span&gt; system.&lt;/p&gt;


Explicit code distribution. Send the code in a message:
&lt;pre&gt;&lt;code&gt;{ok, Bin} = file:read_file(&quot;xxx.beam&quot;),
Term = {apply, erlang, load_module, [Mod, Bin]}),
gen_tcp:send(Socket, term_to_binary(Term))&lt;/code&gt;&lt;/pre&gt;

run it:
&lt;pre&gt;&lt;code&gt;{apply, xxx, start, [...]}&lt;/code&gt;&lt;/pre&gt;

dynamic code upgrade:
	&lt;ul&gt;
	&lt;li&gt;Mod:Func(Args, ...) the latest version of Mod is called&lt;/li&gt;
		&lt;li&gt;If you reload Mod then you can run two versions of the code at the same time&lt;/li&gt;
		&lt;li&gt;You can only have two versions of the code running at the same time, an old and a new version&lt;/li&gt;
		&lt;li&gt;To load a third version you must call erlang:purge_module(Mod) before reloading the module (think of this as  a two place shift register)&lt;/li&gt;
		&lt;li&gt;to load code call erlang:load_module(Mod, BeamCodeBin)&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;You can find all the number of processes running a particular version of a module. Some may never change, those you may have to kill.&lt;/p&gt;


	&lt;p&gt;When you load new code you get old and new code running at the same time. You must call purge_code before you add a third version of the code. This is going to effect 0.0x of people using erlang. After a crash you need to look at these issues.&lt;/p&gt;


	&lt;p&gt;This code swapping is not well documented. Mailing list may be helpful for this area.&lt;/p&gt;


	&lt;h2&gt;Q &#38; A With Joe Armstrong&lt;/h2&gt;


	&lt;h3&gt;What is the relationship between processes on the OS and Erlang?
One process per whole Erlang instance on the OS.&lt;/h3&gt;


	&lt;p&gt;When you spawn there is a run queue. The process that currently gets to the top of the queue. Every time you do a function call it&#8217;s called a reduction. If it&#8217;s waiting to receive a message, it&#8217;s taken out of the scheduling queue and is put into the waiting for message queue. If mail is delivered it&#8217;s put back in the run queue. There is no polling that is used. When you send a message to a processor and it hasn&#8217;t used all it&#8217;s reductions &#8211; may preempt &#8230;&lt;/p&gt;


	&lt;h3&gt;Consider exposing schedule to Erlang?
This would be very dangerous. But GC and as many things as possible should be written in Erlang.&lt;/h3&gt;


	&lt;h3&gt;What do you do in an app to seperate tests into a seperate file?
Hierarchy of tests:
	&lt;ul&gt;
	&lt;li&gt;Unit test framework that comes from France (used by ejabber). &lt;/li&gt;
		&lt;li&gt;Erickson has a test server that is distributed with Erlang. Massive regression and unit tests.&lt;/li&gt;
		&lt;li&gt;Credita has modified to run test any time anything is checked into their repositories.&lt;/li&gt;
		&lt;li&gt;Quick check written by John Hues. Was written in Haskell has been ported to Erling. Generates random tests that satisfy certain properties. Does some automatic reduction of code execution to minimize error conditions. This is an expensive use.&lt;/li&gt;
		&lt;li&gt;E Unit &#8211; Dave found this somewhat cumbersom. Straight pattern matching was easier. Didn&#8217;t seem to have a huge value add. Would like to see how he can use differen&#8217;t naming conventions to automate and have some sort of runner. Find *.erl run anything named test and report if it doesn&#8217;t return a pass. Gen server seems easily testible by calling handler methods.&lt;/li&gt;
	&lt;/ul&gt;&lt;/h3&gt;


	&lt;h3&gt;Diagraming conventions for Erlang?
Not currently.&lt;/h3&gt;


	&lt;h3&gt;Petri Nets or activity diagrams?&lt;/h3&gt;


	&lt;p&gt;Message sequence diagrams are used frequently. Joe is not a fan of drawing programs. Although, message sequence diagrams are extremely useful (Y = Time. X = Process). Joe is writing a program for animating Erlang.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;erlang:trace(Pid, [Msgs, Call, list of things to trace]). &lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This process get&#8217;s lots of messages. Joe is writing a program that stores these in a file and then animates the activities of these processes over time. Working with a games developer to animate this.&lt;/p&gt;


	&lt;h3&gt;Erlyweb?&lt;/h3&gt;


	&lt;p&gt;Joe &#8211; hasn&#8217;t spent much time with this and &lt;span class=&quot;caps&quot;&gt;YAWS&lt;/span&gt; (Yet Another Web Server).&lt;/p&gt;


	&lt;h3&gt;Erlang graphic user interface?&lt;/h3&gt;


	&lt;p&gt;Lots written. Tend to be hard to use &#8211; wswidgets being used for 3D process modeling. Interfaces to &lt;span class=&quot;caps&quot;&gt;SDL&lt;/span&gt;, GTK, Cairo, etc. &lt;span class=&quot;caps&quot;&gt;TCL&lt;/span&gt; library is the mainly supported one that works everywhere. Lots of people have been doing GUIs in web browsers &#8211; action script 3 and flex. Can also use flex2 as a device driver for Erlang &#8211; e.g. video stream. Makes it possible to make these applications more portable. Therefore can use Flash as a device driver. Drop dead beautiful &#8211; flash with &lt;span class=&quot;caps&quot;&gt;AIR&lt;/span&gt; may be the way to go.&lt;/p&gt;


	&lt;h3&gt;Redeployable packaging?&lt;/h3&gt;


	&lt;p&gt;Erlang is packaged for win, linux, and mac. &lt;span class=&quot;caps&quot;&gt;CEAN&lt;/span&gt; (Comprehensive Erlang Application &#8230;) ... Wings. Martin Logan, faxian. Martin Logan also has generator applications for Erlang (Erlware?).&lt;/p&gt;


	&lt;h3&gt;Good frameworks for doing ontological modeling and reasoning?&lt;/h3&gt;


	&lt;p&gt;Expert systems shell. University of Corona. Reported at the Erlang user conferences. Multi agent listener but not generalized to owl.&lt;/p&gt;


	&lt;h3&gt;List of reference projects?&lt;/h3&gt;


	&lt;p&gt;No canonical reference. But check trapexit.org.&lt;/p&gt;


	&lt;h3&gt;Where is Erlang center of gravity?&lt;/h3&gt;


	&lt;p&gt;Attracting lots of interest in Financials &#8211; especially in London. Finance industry targeted conference coming up in London. Much bank interest &#8211; extreme real time demands. Number of pure erlang based trading companies. Debt buying company &#8211; purchases debt and collect. Buys these debts a few moments after the transaction. Mnesia founder involved.&lt;/p&gt;


	&lt;h3&gt;Future changes &#8211; where is it going?&lt;/h3&gt;


	&lt;p&gt;Not many changes to the language. Millions of legacy lines of code needed for compatibility &#8211; syntax unlikely to change. But, implementation speed optimization and multi-processor support is likely to improve.&lt;/p&gt;


	&lt;h3&gt;Embedded Erlang?&lt;/h3&gt;


	&lt;p&gt;4MB probably the minimum size. Used to be 640k but may be a better domain for C.&lt;/p&gt;


	&lt;h3&gt;String localization &#8211; &lt;span class=&quot;caps&quot;&gt;UTF8&lt;/span&gt;?&lt;/h3&gt;


	&lt;p&gt;In a sense it is solved. In another sense it is not solved. In Erlang there is no string type &#8211; it is a list of integers. &lt;span class=&quot;caps&quot;&gt;UTF8&lt;/span&gt; is an integer and can be stored. However, the interpreter from text to binary and back is not written. However, it is not difficult. It is in fact a library change.&lt;/p&gt;


	&lt;h3&gt;What is a good candidate for Erlang?&lt;/h3&gt;


	&lt;p&gt;Telecomm applications, lots of small processes. Individual processes have low ammount of short computations. Good at coordination, concurrency, switching. Not good at matrix multiplication, gif encoding etc. Financial applications fit the erlang profile.&lt;/p&gt;


	&lt;h3&gt;Libraries for interfacing to &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; databases?&lt;/h3&gt;


	&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;ODBC&lt;/span&gt; libraries. Could just open a socket to the DB. Java has a good arbitrary support for sockets. Check Yariv&#8217;s Erly web implementation.&lt;/p&gt;


	&lt;h2&gt;Summary&lt;/h2&gt;


	&lt;p&gt;Fault tolerance drives a lot of the design decisions behind Erlang.
Simple functional language.
Function selection is by pattern matching
Variables are immutable&lt;/p&gt;


Concurrent Erlang
	&lt;ul&gt;
	&lt;li&gt;3 primitives: Spawn, Send, Receive &#8211; very simplified from Object Oriented model.&lt;/li&gt;
		&lt;li&gt;register / unregister can be used to associate a name with a process&lt;/li&gt;
	&lt;/ul&gt;


Fault Tolerant
	&lt;ul&gt;
	&lt;li&gt; catch .. throw, try &#8230; catch &#8230; end&lt;/li&gt;
		&lt;li&gt;link, process_flag(trap_exit, true)&lt;/li&gt;
	&lt;/ul&gt;


Distributed Erlang
	&lt;ul&gt;
	&lt;li&gt;spawn(Node, Mod, Func, Args)&lt;/li&gt;
		&lt;li&gt;or explicit term passing&lt;/li&gt;
	&lt;/ul&gt;


Benefits of Erlang:
	&lt;ul&gt;
	&lt;li&gt;Technologies becoming more concurrent and people are looking for solutions to that probelm. Cooperative applications, etc. Erlang is a working solution. Not operating system dependent. 20 &#8211; 30 years of experience with fault tolerance measured in years.&lt;/li&gt;
		&lt;li&gt;Multi-Core ready&lt;/li&gt;
		&lt;li&gt;Processes in the language&lt;/li&gt;
	&lt;/ul&gt;


Some projects to research:
	&lt;ul&gt;
	&lt;li&gt;&lt;span class=&quot;caps&quot;&gt;AXD301&lt;/span&gt; &#8211; biggest Erlang and Functional programming ever made 60 programmers x 3 years. Runs backbone of british telecom. Market leading in that sector. Peaked at 9&#215;9s of reliability. This wasn&#8217;t a commercial project on 2-3% of the market.&lt;/li&gt;
		&lt;li&gt;Kreditor (Kreditor.se) &#8211; buys debt. #2 IP startup in sweden. $150 year. Self funded. Founded by alumni of Blue Tail.&lt;/li&gt;
		&lt;li&gt;SimpleDB (Amazon)&lt;/li&gt;
		&lt;li&gt;CouchDB (text db)&lt;/li&gt;
		&lt;li&gt;MociWeb (Mochimedia)&lt;/li&gt;
		&lt;li&gt;Ejabberd (jabber server &#8211; by extension Twitter?)&lt;/li&gt;
		&lt;li&gt;ErlyWeb (Erlang web framework inspired by Rails)&lt;/li&gt;
	&lt;/ul&gt;
          </content>  </entry>
  <entry xml:base="http://www.aquabu.com/">
    <author>
      <name>Noah</name>
    </author>
    <id>tag:www.aquabu.com,2008-02-14:11</id>
    <published>2008-02-14T19:07:00Z</published>
    <updated>2008-02-19T00:13:25Z</updated>
    <category term="Class &amp; Event Notes"/>
    <category term="Erlang"/>
    <link href="http://www.aquabu.com/2008/2/14/erlang-pragmatic-studio-day-2-notes" rel="alternate" type="text/html"/>
    <title>Erlang Pragmatic Studio - Day 2 Notes</title>
<content type="html">
            Erlang Pragmatic Studio with Joe Armstrong &amp; Dave Thomas - Chicago, Feb 13-25, 2008. 

h2. Day 2 - Feb 14,2008.

Here are my notes from Day 2 of the Pragmatic Studio Erlang course. Note that these notes are only vaguely edited. Be aware that these are notes so there are certainly typos and errors.

h2. Concurrency

review of an implementation of map:
&lt;pre&gt;&lt;code&gt;map(F,[]) -&gt; [];
map(F,[H|T]) -&gt;
	[F(H) | map(F,T)].&lt;/code&gt;&lt;/pre&gt;
	
Erlang is a concurrent language and a functional programming language second. Why concurrent? The world is concurent - external world &amp; computing world. Often you inherit all the limits of the operating system in regards to processes. Erlang is not bound by the operating system. The emulator handles concurrency.

* Programming concurrent activities in sequential languages is artifically difficult.
* Shared memory (locks, mutexes, coroutines, processes, thread, deadlock, livelock, failure, thread-safe)
* Message passing

Web servers must spawn a new thread for every request. In Erlang when one thread crashes, only that one crashes not the whole system. This is not the case in other environments. Isolation of components is the key to the pattern.

Dave:
* process = object
* message = method
* can apply the same techniques in OOD. The small talk paradigm for passing messages unifies this (to a certain extent).

Joe: Alan Kay in his origination of object orientation, messaging was more primary. The problem with a function call is that the message must return to the caller.

Concurrency in other languages
* can only create very small number of processes
* heavy weight
* only supports message passing and not the kind of error handling semantics that Erlang has

If you know the name of a process, you can send it a message:
* Pid ! Message
* If Pid is hidden you cannot send a message to the process

Message arrives in a mailbox (like email, can be stacked). You never know if a message arrives (if you want to be sure send a message back).

Only three primitives for turning sequential program in to concurrent code:
* spawn - create new process
* send
* receive - try to match pattern


h2. Spawn

Three syntaxes:
* spawn(fun foo/0) - runs foo/0 in a new process
* spawn(fun() -&gt; ... end) - spawns and inline fun
* spawn(Mod, Func, [Arg1, Arg2, Argm]) performs

apply(Mod, Fun, [Arg1, Arg2, ..., Argn]) in a new process.

In Pid1 evaluate Pid2 = spawn(fun() -&gt; ... end)

h2. Send

* Pid ! Msg
* returns the message (e.g. A ! B ! C ! Msg)

h2. Receive

&lt;pre&gt;&lt;code&gt;same as a case statement:
receive -&gt;
	pattern
	...
end&lt;/code&gt;&lt;/pre&gt;

self() is the pid of the current process.
including it in a message allows the recipient to respond to us.

&lt;pre&gt;&lt;code&gt;% receive whatever message comes first
receive
	Msg -&gt;
	...
end&lt;/code&gt;&lt;/pre&gt;

% receive messages in order
&lt;pre&gt;&lt;code&gt;receive
	foo -&gt; true
end,
receive
	bar -&gt; true % leave bar in the mailbox until foo is received
end&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code&gt;B!{transfer,self()}

receive
	{transfer, A} -&gt;
		C ! {transfer, A}
	end
	
receive
...&lt;/code&gt;&lt;/pre&gt;

promise and yield are non-primitive abstractions.

Are we hiding the wrong thing? Should we expose the protocol?
Pros
* Hides message structure
* Makes module reusability easy
* we know how to define APIs

Cons
* More code
* We don't know how to describe protocols
* Protocols do not exist in Erlang as first class objects

Key factor minimizing cross machine boundary cost.

Abstracting server functionality
* run a server that can run in the background
* needs some kind of state
* pass in state at the start
* in case of counter pass in 0
* then cast

Mutate state through recursion. Two more items.

Registered processes. 
* Any program that wants to send a message to a Pid must have Pid in a local variable.
* Use register / unregister
* register(name, Pid), name ! Term sens a message to Pid
* whereis(Name) returns Pid or undefined
* mind the propigation delay

Example of register:
register(counter, spawn(fun() -&gt; counter(1)end)).

From shell see all the registered processor with:
regs().

Registered names are global resources. If two processes register with the same name one will fail.

receive... after

wait for a certain period of time - timeouts
&lt;pre&gt;&lt;code&gt;receive
	pattern1 -&gt; ...
	pattern2 -&gt; ...
after
	TimeInMilliseconds -&gt; %can be atom {infinity}
		Actions
end.&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code&gt;Pid = spawn(fun() -&gt; wait() end),
Pid ! {do, F}

wait() -&gt;
	receive
		{do,F} -&gt;
			F()
	end.&lt;/code&gt;&lt;/pre&gt;	

Security risks.

SMP and Processes
* Erlang can take advantage of multiple processes.
* Enable using -smp +S n options only if compiled with correct flags

all concurrent behaviour is programmed with the following (instead of blocks, mutexes, threads, etc...)
* spawn
* send
* receive

* default max processes is 327xx
* increase processes with: erl +P 1000000000
* once it passes 

processes:max(1).

&lt;pre&gt;&lt;code&gt;MACRO: Debugging
-define(DEBUGGING,true).
-ifdef(DEBUGGING).
-define(DEBUG(X),X).
-else
-define(DEBUG(X),void).
-endif

?DEBUG(io:format(&quot;Found:~p~n&quot;),[Files]),
...&lt;/code&gt;&lt;/pre&gt;

Two ways to do distributed computation:
* distributed Erlang as cluster with one name space. Consider different machines may have different versions of Erlang. Getting the right code onto the right machines may also cause issues. Fully connected machines don't scale well - &gt; 90 machines gets difficult.
* Explicit sockets. No issues of ownership. More easily scalable as other issues are encapsulated - memory is fully not shared.

h2. Client

&lt;pre&gt;&lt;code&gt;{ok, Socket} = gen_tcp:connect(Host, Port, [Options]),
	ok = gen_tcp:send(Socket,Data),
	...
receive
	{tcp, Socket, Data} -&gt;
		%% do something with the data
		...
	{tcp_closed, Socket} -&gt;
		%% take care of this ...&lt;/code&gt;&lt;/pre&gt;
		
How does TCP work?
* server needs to be able to hand multiple clients trying to connect to it. The protocol is slightly different.
* c level api - listen(ip, port);

all that listen does is say I will support people on this given port. call accept and turn it into an IO object that you can use. Typically each connection spawns a different process.

a single-shot server accepts one connection: start_server()

&lt;pre&gt;&lt;code&gt;{ok, Listen} = gen_tcp:listen(Port, ...)
spawn(fun() -&gt; par_loop(Listen) end).

par_loop(Listen) -&gt;
	{ok, Socket} = gen_tcp:accept(Listen),
	spawn(fun() -&gt; par_loop(Listen) end),
	loop(Socket).&lt;/code&gt;&lt;/pre&gt;	
	
Packet lengths:
* A 4 byte length header is automatically added/removed by the system when calling gen_tcp:send and messages are assembled to the correct length before {tcp, Sock, Data} message are sent to the controlling process.

&lt;pre&gt;&lt;code&gt;gen_tcp:connect(Host, Port, [..., {packet,4, ...}])
gen_tcp:listen ....

Sending Erlang terms
gen_tcp:send(Socket, term_to_binary(Term))...

receive
	{tcp, Socket, Data} -&gt;
		Term = binary_to_term(Data),
		...&lt;/code&gt;&lt;/pre&gt;
		
The Middle Man Pattern:
- Middle man has to be written for a particular protocol.
- like unix filters (Dave) - piping

&lt;pre&gt;&lt;code&gt;loop(Pid,Socket) -&gt;
	receive
		{Pid,Msg} -&gt;
			gen_tcp:send(Socket, term_to_binary(Msg)),
			loop(Pid, Socket);
		{tcp, Socket, Data} -&gt;
			Pid ! binary_to_term(Data),
			loop(Pid, Socket);
		{'Exit',Pid} -&gt;
			gen_tcp:close(Socket);
		{tcp_closed, Socket} -&gt;
			exit(Pid, socket_closed)
		end.&lt;/code&gt;&lt;/pre&gt;		

use term_to_binary and binary_to_term.

when to use:
* , separate individual expressions (smallest scope)
* ; pattern matching
* . at end of function (biggest scope)

normally you don't parallelize file read/write access - another case of shared memory.
&lt;pre&gt;&lt;code&gt;% from simple_coordinate_server.erl
% pass in milliseconds or infinity to sleep forever
sleep(T) -&gt;
    receive
    after T -&gt;
	    void 
    end.

gen_txp:listen(0 ...) % gets a free port&lt;/code&gt;&lt;/pre&gt;

Concurrent patterns:
* client server model is the most used pattern when writing concurrent programs. 
* can also use worker manager pattern. 
* or finite state machine.

OTP course would require Erlang background.

&lt;pre&gt;&lt;code&gt;try
...
catch
	exit:Why -&gt;
		From ! {Name, die, why}, % die and tell calling process
		loop(State)
end&lt;/code&gt;&lt;/pre&gt;

delegation is a nice place to do load balancing:
client, delegator, responder

Erlang was developed in a trusted environment, many of the techniques are applicable to this environment.

This is an empty server that will run anything passed to it (guest account slightly safer but still incredibly permissive):
&lt;pre&gt;&lt;code&gt;Pid = spawn(fun() -&gt; wait() end),
...

Pid ! {do, F}
...
wait() -&gt;
	receive
		{do, F} -&gt;
			F()
		end&lt;/code&gt;&lt;/pre&gt;

Many variations of concurrency:
* Resource managing
* Mobile Code	
* Mobile code with transactions

	
Systems can be specified functionally. 
* Systems have non-functional behavior (scalability, fault tolerance) - usually defined very weakly.
* These are usually vaguely defined.
* In Erlang these things can be specified clearly.

In Erlang you often write functional and non-functional code separately. These can then be used independently. Can abstract things like scalable fault-server and fault tolerant processing. Cool!

You can say how many failed processes are allowable in a given period. This can be specified programatically or in product requirements. e.g. if there are three machines and one machine fails and data is lost it is warranted, but if two machines die and data is lost it is out of warranty.

h2. links

links define error propagation paths
If A is linked to B then:
* If A fails then B will be notified
* If B fails then A will be notified

Signals and Messages:
* messages are sent with send
* signals are sent when processes die. Signals are not messages.
* if process A is linked to B it A will be sent an exit signal if B dies.

layers to make the system fault tolerant:
* application level
* level 2 - trap exits
* level 1 - core level to trap exits

considerations:
* if you do spawn followed by link the process might die before you get to the link statement
* spawn_link is like spawn followed by link only the two are performed atomically.
* trap_exit

principles of remote error handling
* fault tolerant systems need two separate computers
* let one process do the work
* let some other process fix the error, even remotely. If a whole machine is crashed it must be fixed remotely. This pattern should also be applied on a local system - cross machine boundary should be handled in the same manner for scalability.
* exit signals are like uncaught exceptions that escape from the process
* failing processes should fail early

How do you know when a machine fails? In distributed Erlang, there should be a heartbeat process that is sent between machines (1-10 times a second).

use halflink for monitoring.

Link methods:
* link
* unlink
* process_flag(trap_exit, true) - tap exits
* {'EXIT',Pid, Why} - message sent when a process dies
These methods are orthogonal to spawn, send, receive
          </content>  </entry>
  <entry xml:base="http://www.aquabu.com/">
    <author>
      <name>Noah</name>
    </author>
    <id>tag:www.aquabu.com,2008-02-14:10</id>
    <published>2008-02-14T02:26:00Z</published>
    <updated>2008-05-27T07:33:08Z</updated>
    <category term="Class &amp; Event Notes"/>
    <category term="Erlang"/>
    <link href="http://www.aquabu.com/2008/2/14/erlang-pragmatic-studio-notes" rel="alternate" type="text/html"/>
    <title>Erlang Pragmatic Studio - Day 1 Notes</title>
<content type="html">
            &lt;p&gt;Erlang
Joe Armstrong &#38; Dave Thomas
Chicago, Feb 13-25, 2008&lt;/p&gt;


	&lt;p&gt;Here are my notes from Day 1 of the Pragmatic Studio Erlang course. Note that these notes are only vaguely edited. Be aware that these are notes so there are certainly typos and errors.&lt;/p&gt;


	&lt;h2&gt;Overview&lt;/h2&gt;


	&lt;p&gt;&#8220;First public Erlang course in US.&#8221; Joe invented Erlang in 1986.&lt;/p&gt;


Fault tolerant systm:
	&lt;ul&gt;
	&lt;li&gt;Two computers, no shared data. Needs to be isolated computation.&lt;/li&gt;
		&lt;li&gt;Concurrent &#38; distributed by default.&lt;/li&gt;
		&lt;li&gt;Then becomes scalable.
Not sharing data key for fault tolerance.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Next to tackle &#8211; low power computing. Lots to tackle. Computing and &lt;span class=&quot;caps&quot;&gt;MIDI&lt;/span&gt; messaging interest.&lt;/p&gt;


Uses:
	&lt;ul&gt;
	&lt;li&gt;Faster rules engine. Better distributed system than Java. Distributed locking across nodes is really error prone.&lt;/li&gt;
		&lt;li&gt;&lt;span class=&quot;caps&quot;&gt;P2P&lt;/span&gt;. Center server is used to coordinate edge &#8211; erlang.&lt;/li&gt;
		&lt;li&gt;Internet worm emulator. Evaluate security.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Functional programming overlap with Ocaml, Haskel, Erlang family. Central idea of non-mutable state is critical to all functional languages &#8211; and in stark contrast to object oriented programming.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Day 1 &#8211; Sequential programming&lt;/li&gt;
		&lt;li&gt;Day 2 &#8211; Parallel programming&lt;/li&gt;
		&lt;li&gt;Day 3 &#8211; Distributed programming and &lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt;&lt;/li&gt;
	&lt;/ul&gt;


Pairing suggested for the labs. 
One core concept for the labs.
	&lt;ul&gt;
	&lt;li&gt;Write a simple distributed client-server.&lt;/li&gt;
		&lt;li&gt;Write a &lt;span class=&quot;caps&quot;&gt;DNS&lt;/span&gt; like server resolver.&lt;/li&gt;
		&lt;li&gt;Write a universal server and register it with &lt;span class=&quot;caps&quot;&gt;DNS&lt;/span&gt;.&lt;/li&gt;
		&lt;li&gt;Send code for a web server to the universal server and tell it to execute it (mobile code)&lt;/li&gt;
		&lt;li&gt;Correct an error in the code and fix the error without stopping the server&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Going from one to many machines can take hours to solve (e.g. sorting out why packets aren&#8217;t making it through firewalls).&lt;/p&gt;


	&lt;h2&gt;The erl shell&lt;/h2&gt;


Erlang shell:
More a controlling environment than it is a full fledged interpreter (in comparison to say ruby&#8217;s &lt;span class=&quot;caps&quot;&gt;IRB&lt;/span&gt;).
How to get out:
	&lt;ul&gt;
	&lt;li&gt;unix &lt;sup&gt;\ aborts everything&lt;/li&gt;
		&lt;li&gt;&lt;/sup&gt;G =&amp;gt; submode =&amp;gt; press q&lt;/li&gt;
		&lt;li&gt;^C &#8211; a for abort&lt;/li&gt;
	&lt;/ul&gt;


When you type ^G it&#8217;s like ssh:
	&lt;ul&gt;
	&lt;li&gt;Can jump to different shells. It&#8217;s distributed.&lt;/li&gt;
		&lt;li&gt;type h for help&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Shell has standard keybindings &#8211; has emacs keybindings as well.&lt;/p&gt;


	&lt;h2&gt;Data Types and Assignment&lt;/h2&gt;


	&lt;p&gt;Integer arithmetic in Erlang is very good. However, if you want extremely fast integer arithmetic you would writ it in C.&lt;/p&gt;


	&lt;p&gt;X = 1.&lt;/p&gt;


	&lt;p&gt;Works differently than object oriented languages. Once value is assigned it cannot be changed. Variables are unbound or bound. Thread safety is anathema to Erlang. This is because there is no mutable state and semiphores are not necessary for protecting them.&lt;/p&gt;


	&lt;p&gt;In Erlang X=1 is a pattern matching in operator (not traditional assignment).&lt;/p&gt;


	&lt;p&gt;&#8221;=&#8221; is a very conventionally overloaded opperator. = was chosen for ease of use, but it is not used as it is in other languages.&lt;/p&gt;


	&lt;p&gt;Worker supervisor model of programming. Assign process to do something. If it fails get some other process to do it. Likewise if pattern fails, it just throws an error. Not like the constant error checking in mutable state applications.&lt;/p&gt;


	&lt;p&gt;Note: pattern matching errors in Erlang 5.6 are nicer than in 5.5.&lt;/p&gt;


	&lt;p&gt;Variables uppercase: X, Y, Bob&lt;/p&gt;


Numbers:
	&lt;ul&gt;
	&lt;li&gt;Integers &#8211; 1 34 &lt;/li&gt;
		&lt;li&gt;Base &#8211; 2 2#101010&lt;/li&gt;
		&lt;li&gt;Base &#8211; 8 8#0723&lt;/li&gt;
		&lt;li&gt;Floats &#8211; 12.34e-07&lt;/li&gt;
	&lt;/ul&gt;


Atoms:
	&lt;ul&gt;
	&lt;li&gt;lowercase&lt;/li&gt;
		&lt;li&gt;true false hellow_world&lt;/li&gt;
		&lt;li&gt;they evaluate to themselves&lt;/li&gt;
	&lt;/ul&gt;


In Erlang &lt;strong&gt;everything&lt;/strong&gt; is a 32 bit or 64 bit word. Atoms are stored once in memory (like ruby symbols). For comparison:
	&lt;ul&gt;
	&lt;li&gt;X = joe.&lt;/li&gt;
		&lt;li&gt;Y = joe.&lt;/li&gt;
		&lt;li&gt;X = Y. %These point to the same memory location&lt;/li&gt;
	&lt;/ul&gt;


Tuples:
	&lt;ul&gt;
	&lt;li&gt;{food, egg}&lt;/li&gt;
		&lt;li&gt;can be nested {person, {firstname, &#8220;joe&#8221;},{lastname,&#8221;armstrong&#8221;}}&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Bind multiple variables: {A, B} = {1,2}.&lt;/p&gt;


	&lt;p&gt;In a tuple, there can be multiple occurrences of a variable. This throws an error since C will be bound to 1. C will still be unbound &#8211; the whole thing fails:
{C, D, C } = {1, 2, 3}.&lt;/p&gt;


	&lt;p&gt;Atoms can also be matched: {person, Name} = {person, &#8220;Dave&#8221;}.&lt;/p&gt;


	&lt;p&gt;f(). %forget all bound variables&lt;/p&gt;


	&lt;p&gt;Note: you may get an error in the book if you already have a variable bound and attempt to reuse it in another example.&lt;/p&gt;


h2. Lists
List are used in 95% of your functions.
&lt;pre&gt;&lt;code&gt;[1,2,3,4]
[] %the empty list

A = [1,2,3].
[C, D, E] = A.&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Lists are a naturally recursive data structure. Lists can be expressed as a head and a tail: [H | T]&lt;/p&gt;


	&lt;p&gt;Head is something, tail is always &lt;strong&gt;a list&lt;/strong&gt;. In C this would be a linked list. This notation originated in Prolog. Much of Erlang originated in Prolog &#8211; in fact the earliest versions of Erlang were implemented in Prolog.&lt;/p&gt;


Why Lists and Tuples? (these are the glue in the language):
	&lt;ul&gt;
	&lt;li&gt;Lists are for variable number of things. Lists are traversed and the first element is the only one that can be efficiently accessed through head and tail notation.&lt;/li&gt;
		&lt;li&gt;Tuples are a container for a fixed number of things.&lt;/li&gt;
	&lt;/ul&gt;


Construct a new list (on the right hand side of the operator it&#8217;s constructor):
&lt;pre&gt;&lt;code&gt;A = [1,2,3].
B = [ fred | A].&lt;/code&gt;&lt;/pre&gt;

Deconstruct a list using pattern matching (on the left hand side of the operator it&#8217;s a deconstructor):
&lt;pre&gt;&lt;code&gt;[ H | T ] = A.
H = 1.
T = [2,3].&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Head of the list could have multiple values: [ H1, H2 | T] = [1,2,3].&lt;/p&gt;


	&lt;p&gt;Compiler optimizes your code so that it is only patterns. If and case expressions are eliminated.&lt;/p&gt;


h2. Strings
&lt;pre&gt;&lt;code&gt;Strings are actually integers:
A = &quot;Dave&quot;.
[H | T] = A.
H = 68.&lt;/code&gt;&lt;/pre&gt;

Lists of numbers will be returned as characters:
&lt;pre&gt;&lt;code&gt;[65, 66, 67].
=&gt; &quot;ABC&quot;&lt;/code&gt;&lt;/pre&gt;

&#8221;_&#8221; matches anything and doesn&#8217;t bind:
&lt;pre&gt;&lt;code&gt;A = {name, &quot;Dave&quot;, state, &quot;TX&quot;}.
{ name, Name, _, _} = A.&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;++ concatenates lists.&lt;/p&gt;


Interesting list length counts:
&lt;pre&gt;&lt;code&gt;A = &quot;hi,&quot;.
B = &quot;there&quot;.
length([A,B]).
=&gt; 2
length(A ++ B).
=&gt; 8
length([A | B]).
=&gt; 6
[A | B]
=&gt; [&quot;hi,&quot;,116,104,101,114,101] %&quot;hi&quot; is a nested list&lt;/code&gt;&lt;/pre&gt;

	&lt;h2&gt;Modules&lt;/h2&gt;


Useful shell commands:
&lt;pre&gt;&lt;code&gt;help(). %show all shell commands
b(). %display all bindings
v(N) %value of a query. Type the number of the prompt or -X and it will return the value at that point in your program.&lt;/code&gt;&lt;/pre&gt;

creating a module:
&lt;pre&gt;&lt;code&gt;-module(dave).
-compile(export_all). %export all of the &lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Functions are defined by their name and &#8220;arity&#8221; (number of arguments that they take). Semicolons separate pattern matches and the functionality that should be applied. In erlang there is a tendency to write patterns rather than to write conditional loops.&lt;/p&gt;


Example of sum function with a test:
&lt;pre&gt;&lt;code&gt;-module(dave).
-compile(export_all).

test() -&gt;
    6 = sum([1,2,3]),
    10 = sum([1,2,3,4]),
    0 = sum([]),
    yes.

sum([H | T]) -&gt;
    H + sum(T);
sum([]) -&gt;
    0.&lt;/code&gt;&lt;/pre&gt;

Why doesn&#8217;t erlang have a loop? 
	&lt;ul&gt;
	&lt;li&gt;Joe: you don&#8217;t need it. &lt;/li&gt;
		&lt;li&gt;Dave: A loop usually includes a variable with state. Implicitly this has mutable variables.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Could write a test that runs all test methods found in each module and make sure final atom &#8220;hooray&#8221; or &#8220;yes&#8221; is returned.&lt;/p&gt;


	&lt;p&gt;Erlang idiom is not to do defensive error case checks. Program for the happy case in Erlang. Then the question is what are you going to do with errors?&lt;/p&gt;


The set of things that can happen outside of the specifications is nearly infinite. In Erlang only code that is specified and nothing else. If it goes outside, let it crash. This is very different than defensive programming.
	&lt;ul&gt;
	&lt;li&gt;Let it crash.&lt;/li&gt;
		&lt;li&gt;Crash early. If it crashes early it will be available in the logs and the state will be traceable.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;On shipping, wrap the errors.&lt;/p&gt;


	&lt;p&gt;In a concurrent world there are many more things that can go wrong and are not reproducible. Joe&#8217;s thesis was on postmortem analysis of bugs.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;%%File name is amod.erl
-module(amod).
%%these can be called from outside the module
-export([foo/1,bar/2]).&lt;/code&gt;&lt;/pre&gt;

compile from command line:
&lt;pre&gt;&lt;code&gt;erlc amodule.erl
=&gt; outputs amodule.beam&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;run it: erl -noshell -pa /path/to/code -s Mod Func Args&lt;/p&gt;


	&lt;p&gt;erl has about 60 flags.&lt;/p&gt;


erl scripting
&lt;pre&gt;&lt;code&gt;escript:
/#!/opt/local/bin/escript
main(List) -&gt; %%main is first function called?
    ...&lt;/code&gt;&lt;/pre&gt;

Code paths etc:
&lt;pre&gt;&lt;code&gt;./erlang or ${HOME}/.erlang
code:add_patha(&quot;/path/&quot;).&lt;/code&gt;&lt;/pre&gt;

loads using undefined function &#8211; done using an exception.
Tail recursive variable that carries through the state.
Equivalent of System.out.printline:
&lt;pre&gt;&lt;code&gt;    io:format(&quot;Hello from: ~p~n&quot;,[:file:get_cwd()]).&lt;/code&gt;&lt;/pre&gt;

	&lt;ul&gt;
	&lt;li&gt;&lt;sub&gt;p&lt;/li&gt;
		&lt;li&gt;&lt;/sub&gt;n&lt;/li&gt;
	&lt;/ul&gt;


If you installed from macports you will have the man page. If you compile you will have to add them in explicitly. View man pages like this:
&lt;pre&gt;&lt;code&gt;erl -man io&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Erlang is also on the website. May be faster than local man pages.
erlang.org/doc/man&lt;/p&gt;


datatypes:
	&lt;ul&gt;
	&lt;li&gt;integers&lt;/li&gt;
		&lt;li&gt;floats&lt;/li&gt;
		&lt;li&gt;atoms&lt;/li&gt;
	&lt;/ul&gt;


structured datatypes:
	&lt;ul&gt;
	&lt;li&gt;lists&lt;/li&gt;
		&lt;li&gt;tuples&lt;/li&gt;
	&lt;/ul&gt;


	&lt;h2&gt;Functions&lt;/h2&gt;


	&lt;p&gt;&#8221;-&amp;gt;&#8221; translates to &#8220;returns&#8221;.&lt;/p&gt;


&lt;span class=&quot;caps&quot;&gt;BIF&lt;/span&gt; means &#8220;Built in Function&#8221;. Allows you to get out of Erlang and closer to the metal. These are not written in Erlang. Can see them in:
	&lt;ul&gt;
	&lt;li&gt;erl -man erlang&lt;/li&gt;
		&lt;li&gt;or in the book&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Any term can be serialized into binary. Joe: This is very optomized, a highlight of the language:
gen_tcp:send(Socket, term_to_binary(Term)), ...&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;receive
    {tcp_data, Socket, Bin} -&gt;
        Term = binary_to_term(Bin)
end.&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;tuple_to_list sometimes erlang:now().
In theory erlang: BIFs are implemented in erlang. But it turns out that this is not completely true. This is quirky.&lt;/p&gt;


Select a function to apply (Erlang meta-programming anyone?):
&lt;pre&gt;&lt;code&gt;apply(Mod, Func, [Arg1, Arg2, ...]). %this is like a ruby send
M = list_to_atom(&quot;erlang&quot;). %string is in fact a list&lt;/code&gt;&lt;/pre&gt;

Funs, a bit like calling a lambda:
&lt;pre&gt;&lt;code&gt;Double = fun(X) -&gt; 2*X end. %define a function and then use it as a parameter!
lists:map(Double,[1,2,3,4])
lists:map(fun(X) -&gt; 2*X end, [1,2,3,4]). %more idiomatic&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;There is no currying.&lt;/p&gt;


	&lt;p&gt;Writing your own control structures is common. For is not built into the language. See Joe&#8217;s Erlang book.&lt;/p&gt;


Funs can return funs:
&lt;pre&gt;&lt;code&gt;MakeAdder = fun(Inc) -&gt; fun(X) -&gt; X + Inc end end. %this is a closure
Add5 = MakeAdder(5).
Add5(10).&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Can write programs that only generate closures and evaluate them later (lazy evaluation). This is a technique that haskell programmers often use.&lt;/p&gt;


Some of the most useful functions are:
	&lt;ul&gt;
	&lt;li&gt;map &#8211; takes a list produces a list of values.&lt;/li&gt;
		&lt;li&gt;fold &#8211; takes a list and produces a single value.&lt;/li&gt;
	&lt;/ul&gt;


&lt;pre&gt;&lt;code&gt;lists:map(fun(X)-&gt; X* X end, [1,2,3]).
=&gt; [1,4,9]&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Joe calls this list at a time programming. Makes programs much shorter.&lt;/p&gt;


foldl
	&lt;ul&gt;
	&lt;li&gt;applies a function to every element in a list, updating an accumulator as it goes. Returns a single value (the accumulator).&lt;/li&gt;
		&lt;li&gt;lists:foldl(Fun, Acc0, List)&lt;/li&gt;
		&lt;li&gt;great for summing&lt;/li&gt;
	&lt;/ul&gt;


write sum using an accumulator (may be a bug/typo here):
&lt;pre&gt;&lt;code&gt;sum(L) -&gt; sum(L,0).

sum([H|T], Sum) -&gt;
    Sum1 = Sum + H, %tail recursion is applied to Sum1 since it is called once and never accessed again &#38; garbage collected.
    Sum(T, Sum1);
sum([], Sum) -&gt;
    Sum. %at the end just return the accumulation value&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Custom folds are extremely useful&#8230;.&lt;/p&gt;


List Comprehensions:
	&lt;ul&gt;
	&lt;li&gt;come from set theory&lt;/li&gt;
		&lt;li&gt;eg {x : x is even} &#8211; the set of all even numbers&lt;/li&gt;
		&lt;li&gt;in erlang the colon is written as ||&lt;/li&gt;
		&lt;li&gt;[X || X &amp;lt;- L, (X rem 2) =:= 0]. % same as {x : x is even}&lt;/li&gt;
		&lt;li&gt;the above should be more efficient than map &#8211; compiler is optimized for list comprehensions.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;[ Constructor || Pattern &amp;lt;- List, Predicate, ... ]&lt;/p&gt;


Generate the cartesian project:
&lt;pre&gt;&lt;code&gt;[ {X, Y} || X &amp;lt;-&gt;&amp;lt;-&gt;&lt;/code&gt;&lt;/pre&gt;

The quicksort list comprehension:
&lt;pre&gt;&lt;code&gt;-module(qsort).
-export([sort/1]).

sort([Pivot]|T) -&gt; %arbitrarily choose first element as pivot
  sort([X || X &amp;lt;- t&gt;&amp;lt; Pivot])
    ++[Pivot]++
    sort([X || X &amp;lt;- t&gt;= Pivot]);
sort([]) -&gt;
    [].&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Note that the quicksort function in Erlang actually is quite a bit longer than this and handles lost of special cases.&lt;/p&gt;


Accumulators pass extra argument(s) into the function:
&lt;pre&gt;&lt;code&gt;-module(accum).

evens_and_odds(L) -&gt; evens_and_odds(L,[],[]).

evens_and_odds([H|T,E,O]) when H rem 2 =:= 0 -&gt; evens_and_odds(T, [H|E], 0); %gaurd in the first clause
evens_and_odds([H|T, E, O]) -&gt; evens_and_odds(T,E,[H | O]); %no gaurd in the second clause
evens_and_odds([], E, O) -&gt; {E, O}.&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Adding gaurds allows the compiler to compile better.
Erlang has multiple entry points to the function unlike Java and C. If you write your code without multiple entry points and with an if statement, you don&#8217;t have the opportunity to reorder the clauses. If you write this with an if statement you have done the compilers job. Better not to.&lt;/p&gt;


There are a few equality operators. Only different when comparing floating point numbers:
&lt;pre&gt;&lt;code&gt;* == %fuzzy equal for integers and floats comparison
* =:=&lt;/code&gt;&lt;/pre&gt;

Can&#8217;t call functions in gaurds. Know that they can be parsed into optimal decision trees.
	&lt;ul&gt;
	&lt;li&gt;Single Gaurd&lt;/li&gt;
		&lt;li&gt;Full Guard &#8211; comma seperated list of single guard conditions.&lt;/li&gt;
	&lt;/ul&gt;


Gaurds sequence:
	&lt;ul&gt;
	&lt;li&gt;Commas are interpreted as and.&lt;/li&gt;
		&lt;li&gt;Semicolons are interpreted as or.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;It&#8217;s messy to write a new function for every condition. This is reduced by writing guards:&lt;/p&gt;


	&lt;h2&gt;case and if&lt;/h2&gt;


&lt;pre&gt;&lt;code&gt;%case statement
goto_work(Day)-&gt;
    case classify(Day) of
        weekday -&gt; true; %could add a gaurd after weekday
        weekend -&gt; false
    end.

goto_work(Day) -&gt;
    if
        Day =:= monday -&gt; true;
        Day =:= tuesday -&gt; false;
        true -&gt; false
    end.&lt;/code&gt;&lt;/pre&gt;

compiler reduces case and if to:
&lt;pre&gt;&lt;code&gt;f(pattern1) -&gt;
    action;
f(pattern2) -&gt;
    action2.&lt;/code&gt;&lt;/pre&gt;

Booleans:
	&lt;ul&gt;
	&lt;li&gt;true and false are atoms&lt;/li&gt;
		&lt;li&gt;these are by convention only &#8211; but this is strongly recommended convention&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Given a temperature that returns hot or cold. It is better to use is_hot and is_cold and return true and false. This will allow you to use functions that work with booleans. e.g. lists:filter(Fun, L) -&amp;gt; L&#8217;&lt;/p&gt;


	&lt;p&gt;Tame Large Tuples with Records. These expand into tuples. Joe thinks this syntax may improve (e.g. he says &#8220;it sucks&#8221;).&lt;/p&gt;


Example:
&lt;pre&gt;&lt;code&gt;%Define a record - module syntax only. Won't work on the command line.
-record(person, {name, firstname, lastname, age, sex, weight, height})

X = #person{name=&quot;jane&quot;,age=32, sex=female} %%creates a new record
birthday(#person{age=N} = X) -&gt;
    X#person{age = N+1}&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;There are quite a few functions that return tuples that are generated by records. These are records that are returned as tuple. If you include a specific header file it will be interpreted as a record.&lt;/p&gt;


	&lt;p&gt;It&#8217;s better not to use records for short tuples. This is the idiom.&lt;/p&gt;


Not mentioned here:
	&lt;ul&gt;
	&lt;li&gt;macros &#8211; first introduced for language customization (internationalization), then grew a bit in scope.&lt;/li&gt;
		&lt;li&gt;parse transforms &#8211; for implementing extensions to the language&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Exceptions.&lt;/p&gt;


Overview:
	&lt;ul&gt;
	&lt;li&gt;exceptions are generated when errors occur&lt;/li&gt;
		&lt;li&gt;exceptions can be generated by a program: throw, exit, error.&lt;/li&gt;
		&lt;li&gt;convert exceptions into values&lt;/li&gt;
	&lt;/ul&gt;


&lt;pre&gt;&lt;code&gt;X=atom_to_list(123).
error...
X. % is unbound&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;catch F(X) returns F(X) if no exception is generated while evaluating the function. if the error is thrown returns a tuple.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;some_function(X, Y) -&gt;
  case something_that_might_crash(X,Y) of
    {'EXIT',Why} -&gt;
        %% handle error;&lt;/code&gt;&lt;/pre&gt;
...

Or try catch. Gives more fine grained control.
&lt;pre&gt;&lt;code&gt;start(Arg1, Arg2) -&gt;
    try
        begin
            ok = start_server(),
            L1 = dothis(Arg1),
        end
    end
    catch
        exit: Pattern -&gt;
        ...
    after
        ...
    end.&lt;/code&gt;&lt;/pre&gt;

Abnormal termination with throw:
&lt;pre&gt;&lt;code&gt;..
    catch
        throw:X -&gt;
            X
    end.&lt;/code&gt;&lt;/pre&gt;

Use exceptions for &#8220;impossible&#8221; situations &#8211; thinking defensively goes awry. Everything has return value so the above actually returns the tuple {ok}. This takes a correct crash and returns something incorrect:
&lt;pre&gt;&lt;code&gt;opcode(load) -&gt; 1;
opcode(store) -&gt; 2;
opcode(X) -&gt; %%invalid: what I do now?
    io:format(&quot;bad opcode:~p~n&quot;,[X]).&lt;/code&gt;&lt;/pre&gt;

a &#8220;correct solution&#8221; to the above:
&lt;pre&gt;&lt;code&gt;opcode(load) -&gt; 1;
opcode(store) -&gt; 2;
opcode(X) -&gt; exit({ebadOpCode, X}).&lt;/code&gt;&lt;/pre&gt;

Solution two: do nothing (best):
&lt;pre&gt;&lt;code&gt;opcode(load) -&gt; 1;
opcode(store) -&gt; 2.&lt;/code&gt;&lt;/pre&gt;

Turn values into exceptions:
&lt;pre&gt;&lt;code&gt;exitIfCannotOpenFile(File) -&gt;
    case file:open(File, [read]) of
        {ok, Pid} -&gt;
            Pid;
        {error, Why} -&gt;
            exit(Why)
    end.&lt;/code&gt;&lt;/pre&gt;

Improving and error:
&lt;pre&gt;&lt;code&gt;readConfigFile -&gt;
    case file:consult(File) of
        {ok, Stuff} -&gt; Stuff;
        {error, enoent} -&gt; exit({eBadConfigFile, File})
    end.&lt;/code&gt;&lt;/pre&gt;

Three ways of generating exceptions:
	&lt;ul&gt;
	&lt;li&gt;exit &#8211; punish the programmer&lt;/li&gt;
		&lt;li&gt;throw&lt;/li&gt;
		&lt;li&gt;error&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Alternative to defensive programming &#8211; one big catch at the top of the program. Crash early, crash often.&lt;/p&gt;


Main sequential libraries:
	&lt;ul&gt;
	&lt;li&gt;lists.erl &#8211; list manipulation. Extremely common, should be familar with these. There are lots of these. Joe says about 15 are in common use, then diminishing returns after that.&lt;/li&gt;
		&lt;li&gt;file.erl&lt;/li&gt;
		&lt;li&gt;ets.erl &#8211; Hash tables in memory.&lt;/li&gt;
		&lt;li&gt;dets.erl &#8211; Persistent hash tables on disk&lt;/li&gt;
		&lt;li&gt;dict.erl &#8211; fast key-value dictionary&lt;/li&gt;
		&lt;li&gt;io.erl and io_lib.erl&lt;/li&gt;
		&lt;li&gt;filename.erl&lt;/li&gt;
		&lt;li&gt;filelib.erl&lt;/li&gt;
		&lt;li&gt;file.erl&lt;/li&gt;
	&lt;/ul&gt;


Above the &lt;span class=&quot;caps&quot;&gt;BIF&lt;/span&gt; level you can actually read the source code of the lbiraries
	&lt;ul&gt;
	&lt;li&gt;find using code:which(lists)&lt;/li&gt;
		&lt;li&gt;mac location of libraries (for maports install)/opt/local/lib/erlang/lib/stdlib-1.15&lt;/li&gt;
		&lt;li&gt;have a look at /opt/local/lib/erlang/lib/stdlib-1.15/src&lt;/li&gt;
	&lt;/ul&gt;


	&lt;h2&gt;Three common patterns&lt;/h2&gt;


Pattern 1 &#8211; Terminate after value is found
&lt;pre&gt;&lt;code&gt;do(..., [H,T], ...)     -&gt; ...;     % found pattern, terminate recursion return value
do(...,[_|T], ...)     -&gt; do(...,T);     % pattern not found, pass in tail and recurse further
do(..., [], ...) -&gt; ... .             % end of recursion return value&lt;/code&gt;&lt;/pre&gt;

example of pattern 1:
&lt;pre&gt;&lt;code&gt;member(H, [H|T]) -&gt; true;
member(H, [_|T]) -&gt; member(H,T);
member(H, []) -&gt; false.&lt;/code&gt;&lt;/pre&gt;

Pattern 2 &#8211; Build lists as the output of a recursion.
&lt;pre&gt;&lt;code&gt;filter(Fun, [H|T]) -&gt;     %grab a function and a list split into head and tail
    case Fun(H) of        %evaluate function on head
        true -&gt; [H|filter(Fun,T)];    % if true, return append head to the filter called on tail
        false -&gt; filter(Fun, T)        % if false, return apply filter to tail without appending
    end;
filter(Fun,[]) -&gt; []. % if passed the empty set return the empty set&lt;/code&gt;&lt;/pre&gt;

Pattern 3 &#8211; Build results in reverse order in an accumulator
&lt;pre&gt;&lt;code&gt;partition(Fun, L) -&gt; partition(Fun,L,[],[]). %delegate public function to another function

partition(Fun,[H|T],Yes,No) -&gt; %take a list of a function, split list, Yes list, and No list
    case Fun(H) of               %apply function
        true     -&gt; partition(Fun,T,[H | Yes], No); %if true recurse Fun, Tail, Add head to Yes, and No.
        false     -&gt; partition(Fun,T,Yes, [H | No]) %if false recurse Fun, Tail, Yes, Add head to No.
    end;
partition(Fun,[],Yes,No)-&gt; 
    {reverse(Yes),reverse{No}}. %if list is empty pass back the reversed list&lt;/code&gt;&lt;/pre&gt;

Methods of reversing a list:
&lt;pre&gt;&lt;code&gt;[H | Yes] %adding and needs reverse
Yes ++ [H] %right order but slower than reversing at the end&lt;/code&gt;&lt;/pre&gt;

Remember, all of these can be done with pattern matching:
	&lt;ul&gt;
	&lt;li&gt;validation&lt;/li&gt;
		&lt;li&gt;deconstructing lists&lt;/li&gt;
		&lt;li&gt;case statements&lt;/li&gt;
		&lt;li&gt;list comprehensions&lt;/li&gt;
	&lt;/ul&gt;


They all use this one form:
&lt;pre&gt;&lt;code&gt;f(Pattern1) -&gt;
    action1;
f(Pattern2) -&gt;
    action2.&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://www.aquabu.com/">
    <author>
      <name>Noah</name>
    </author>
    <id>tag:www.aquabu.com,2008-02-04:9</id>
    <published>2008-02-04T17:47:00Z</published>
    <updated>2008-02-04T17:48:30Z</updated>
    <link href="http://www.aquabu.com/2008/2/4/build-ruby-audio-apis-hack-session-notes" rel="alternate" type="text/html"/>
    <title>Build Ruby Audio APIs Hack Session Notes</title>
<content type="html">
            Thursday Jan 24th 2008 the Bay Area Computer Music Technology Group met for our &quot;Build Ruby Audio APIs Hack Session at Internaut Design.&quot;:http://electronicmusic.meetup.com/152/calendar/6806947/ Thanks to David Lowenfels at Internaut for hosting! 

Our focus was identifying existing Ruby Audio APIs for extension, and determining what would be the most useful approaches and protocols.

We identified four layers to explore and technologies that could be extended or bridged for each layer:
* Raw DSP
* Sound design
* Composition layer
* Inter-application communication

The first three layers essentially map to timescale and the fourth to inter-application protocols. The programatic structures would however be different for each of these time scales.

h2. DSP Layer

For raw DSP there was an agreement that Ruby interpreters would not be directly fast enough to run serious DSP code and would likely need to rely on C libraries. We discussed generating csound orchestra files using erb and wrapping the csoundapi. I have been making a preliminary pass at writing a csound orchestra file writer that uses ERB that I will be posting to a public subversion repository in the near future.

David Lowenfels was interested in writing DSP code directly in Ruby and did some further inquiry into SND/Ruby that seemed very promising see the email below. The other alternative was directly accessing portaudio through ruby. A wrapper for this already exists but doing DSP this way seemed unlikely to run quickly or be a truly productive line of inquiry. There was some speculation on whether a Ruby interpreter would ever be fast enough to run DSP code but we came to no conclusion on this. Currently it looks like YARV is the fastest interpreter (needs verification).

SWIG was mentioned in a number of contexts as being an excellent way to bridge C libraries to Ruby (as well as an array of other languages).

h2. Sound Design Layer

VST and Audio unit wrappers could presumably be easily written. Tim Thompson's experience writing a Python API for VST was that working with VST would be succesful and strightforward. Audio units are probably well documented. We didn't explore too far into this territory.

Using ruby in Max/MSP through jRuby and the JVM seems possible and straightforward if someone has the will to document the setup this could be fairly widely used. I am pretty sure it could be successful as I have used Jython with Max/MSP and it worked. There was also a suggestion to use jRuby and jMax, but extending max/msp elicited more enthusiasm and would potentially be of wider use due to installed user base.

Wrappers for csound would also be useful for sound design.

h2. Composition Layer

Ruby is an excellent base language to extend for Domain Specific Languages (e.g. rails). A composition framework or DSL could be written on top of any of the technologies discussed for the DSP or Sound Design Layer. However, the most interesting approach (for me anyway) would be writing the composition layer to talk to OSC, thus providing the most portability across audio engines.

h2. Inter-application communication

Making use of the Ruby OSC wrapper seemed like a very productive path. We discussed using OSC to expose the object space of a system. We also decided it would be useful to perform some stress testing for the existing Ruby OSC library (rosc) and optomize OSC library if necessary. OSC would provide interactions with the most platforms of interest and would allow the creation of a more high level API or domain specific language written in Ruby.

h2. Future Events

At the end of the evening we reviewed the hack session itself. From this discussion it is likely that future hack sessions for the Bay Area Computer Music Technology Group will be organized around OSC and interactions with multiple languages including Ruby. It also seems that a more general Audio Open Source development hack session would be of interest. There may also be more Ruby focused hack sessions, or a Ruby focused sub group within a larger hack session.


h2. Useful Links

Wrapping Csound:
* &quot;Interesting Csound API info here&quot;:http://lac.zkm.de/2006/papers/lac2006_victor_lazzarini.pdf (PDF)

Using the Portaudio Ruby wrapper:
* &quot;Wrapper&quot;:http://samesimilar.textdriven.com/
* &quot;Portaudio Site&quot;:http://www.portaudio.com (was down last time I checked)

Using OSC from Ruby to control audio applications: 
* &quot;An existing ruby OSC library - ROSC&quot;:http://hans.fugal.net/src/rosc/)

SNDs Ruby functionality: 
* &quot;About SND&quot;:http://ccrma.stanford.edu/software/snd/snd/snd.html
* &quot;SND &amp; Ruby&quot;:http://ccrma.stanford.edu/software/snd/snd/grfsnd.html#sndandruby

h2. Further Developments

David post meeting interchange about SND/Ruby:

seems like my intended work with ruby-clm is basically already done! :)
I'll report back when I have generated some audio examples.

-David

Begin forwarded message:

&gt; Hi, David!  Thanks for your interest in Snd/Ruby.
&gt;
&gt;&gt; I'm excited to see that I could generate audio with ruby. How does
&gt;&gt; this fit in with Snd?
&gt;
&gt; If you configure Snd with ./configure --with-ruby, you will end up
&gt; with a new Snd program using extension language Ruby.  You can type in
&gt; Ruby/CLM/Snd commands in the Snd listener, eg:
&gt;
&gt;   x = 1 + 2
&gt;   os = make_oscil
&gt;   os.call(0, 0)
&gt;   os.frequency = 330
&gt;
&gt; etc.  There exists a bunch of *.rb files which are mainly translations
&gt; of the corresponding Scheme files.  Snd's documentation tries to show
&gt; the similarities between Scheme and Ruby Snd/CLM code.
&gt;
&gt;&gt; Is it possible to run ruby-clm outside of snd?
&gt;
&gt; Get the file ftp://ccrma-ftp.stanford.edu/pub/Lisp/sndlib.tar.gz and
&gt; configure it with ./configure --with-ruby.  If you install sndlib.so
&gt; in ${RUBY_SITE_ARCHLIB} you will be able to require it like all other
&gt; libraries with 'require &quot;sndlib&quot;'.  The contents of sndlib is nearly
&gt; the same like Lisp CLM which is described in snd/sndclm.html.  A few
&gt; *.rb files are included in sndlib.tar.gz which may help a little.
&gt;
&gt; If you have more question please don't hesitate to ask them by email,
&gt; or even through the clm mailing list.
&gt;
&gt; Mike
          </content>  </entry>
  <entry xml:base="http://www.aquabu.com/">
    <author>
      <name>Noah</name>
    </author>
    <id>tag:www.aquabu.com,2008-01-18:8</id>
    <published>2008-01-18T22:07:00Z</published>
    <updated>2008-01-18T22:24:07Z</updated>
    <category term="ChucK"/>
    <category term="Class &amp; Event Notes"/>
    <link href="http://www.aquabu.com/2008/1/18/chuck-workshop-with-ge-wang-ccrma-notes" rel="alternate" type="text/html"/>
    <title>ChucK Workshop With Ge Wang @ CCRMA - Notes!</title>
<content type="html">
            &lt;p&gt;Below are the notes that I took at the &lt;a href=&quot;http://electronicmusic.meetup.com/152/calendar/6710874/&quot;&gt;ChucK audio programming workshop&lt;/a&gt; lead by Ge Wang at Stanford &lt;span class=&quot;caps&quot;&gt;CCRMA&lt;/span&gt; on Sunday Jan 13, 2008. Ge Wang and I organized this event through the Bay Area Computer Music Technology group. Many thanks to Ge for teaching this! You can join the &lt;a href=&quot;http://electronicmusic.meetup.com/152/&quot;&gt;Bay Area Computer Music Technology&lt;/a&gt; group if you would like to be informed of the advanced workshop, which should happen in another few months.&lt;/p&gt;


	&lt;p&gt;These are &lt;strong&gt;raw notes&lt;/strong&gt;. If you notice any egregious typos let me know and I will correct them. Happy ChucKing&#8230;&lt;/p&gt;


	&lt;h2&gt;ChucK Workshop Notes &#8211; Lead by Ge Wang &#8211; Sun, Jan 13th, 2008&lt;/h2&gt;


	&lt;p&gt;Code == musical instrument
On the fly programming&lt;/p&gt;


	&lt;p&gt;Audicle is fun and crazy to use, but it&#8217;s still a line of research. MiniAudicle is more usable.&lt;/p&gt;


Language design solutions:
	&lt;ul&gt;
	&lt;li&gt;Make time itself computable&lt;/li&gt;
		&lt;li&gt;Synchronize events&lt;/li&gt;
		&lt;li&gt;Flexible and Readable &#8211; optimize later&lt;/li&gt;
	&lt;/ul&gt;


	&lt;h2&gt;The &lt;span class=&quot;caps&quot;&gt;CHUCK&lt;/span&gt; Operator&lt;/h2&gt;


this is the chuck operator: 
&lt;pre&gt;&lt;code&gt;=&gt;&lt;/code&gt;&lt;/pre&gt;

simple chuck:
&lt;pre&gt;&lt;code&gt;x =&gt; y;&lt;/code&gt;&lt;/pre&gt;
action depends on what x and y are.

chain chuck:
&lt;pre&gt;&lt;code&gt;w =&gt; x =&gt; y =&gt; z;&lt;/code&gt;&lt;/pre&gt;
operations run from left to right. This is backwards from other langs like (c/c++, Java)

nested (do what&#8217;s in brackets first):
&lt;pre&gt;&lt;code&gt;w =&gt; (x =&gt; y) =&gt; z&lt;/code&gt;&lt;/pre&gt;

unchuck:
&lt;pre&gt;&lt;code&gt;x =&amp;lt; y =&amp;lt; z;&lt;/code&gt;&lt;/pre&gt;

feedback:
&lt;pre&gt;&lt;code&gt;x =&gt; y =&gt; x&lt;/code&gt;&lt;/pre&gt;

	&lt;h2&gt;Time and Basic Usage&lt;/h2&gt;


	&lt;p&gt;dur is a native type&lt;/p&gt;


Basic code test:
&lt;pre&gt;&lt;code&gt;SinOsc foo =&gt; dac;&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Generate a sine wave.  foo instanciates SineOsc. foo is like a patchcord.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;440 =&gt; foo.freq;&lt;/code&gt;&lt;/pre&gt;

To make sound you must advance time:
&lt;pre&gt;&lt;code&gt;1::second =&gt; now;&lt;/code&gt;&lt;/pre&gt;

comments:
&lt;pre&gt;&lt;code&gt;//this is a comment - same as Java or C/C++
/* can also use c style comments */&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;There are emacs keybindings for chuck. Others?
Coloring in MiniAudicle.&lt;/p&gt;


	&lt;p&gt;default intensity is 1.
chuck will not advance time for you. you must advance time.&lt;/p&gt;


basic program:
&lt;pre&gt;&lt;code&gt;//----------------
SinOsc foo =&gt; dac;
.5 =&gt; foo.gain;

//advance time!
220 =&gt; foo.freq;
.5::second =&gt; now;&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;windows and linux versions work. Mac &lt;span class=&quot;caps&quot;&gt;OS X&lt;/span&gt; is the easiest to use.&lt;/p&gt;


MiniAudicle:
	&lt;ul&gt;
	&lt;li&gt;start virtual machine&lt;/li&gt;
		&lt;li&gt;add shread to run. can add multiple threads.&lt;/li&gt;
		&lt;li&gt;replace thread, replaces old thread code, replace just replaces the latest thread&lt;/li&gt;
		&lt;li&gt;remove thread removes thread on top&lt;/li&gt;
		&lt;li&gt;watch for errors in the console monitor&lt;/li&gt;
	&lt;/ul&gt;


for os x users only go to
	&lt;ul&gt;
	&lt;li&gt;miniAudicle =&amp;gt; prefereces&lt;/li&gt;
		&lt;li&gt;enable on-the-fly programming visual feedback (show you gostbusters x when there is an error)&lt;/li&gt;
	&lt;/ul&gt;


random number program:
&lt;pre&gt;&lt;code&gt;SinOsc foo =&gt; dac;
.5 =&gt; foo.gain;

//advance time!
while ( true )
{
    //choose frequency randomly
    Std.rand2f(30,1000) =&gt; foo.freq;
    100::ms =&gt; now;
}&lt;/code&gt;&lt;/pre&gt;

consider:
	&lt;ul&gt;
	&lt;li&gt;3/2 does integer division not floating point division&lt;/li&gt;
		&lt;li&gt;3/2 = 1&lt;/li&gt;
		&lt;li&gt;3./2 or 3.0/2 = 1.5&lt;/li&gt;
	&lt;/ul&gt;


&lt;pre&gt;&lt;code&gt;100::ms =&gt; now; //does not use system clock
//puts code processing to sleep while stepping forward&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;if code needed to advance time is not computable in the given time you will have dropouts
you can compute in &#8220;real time&#8221; or &#8220;off line&#8221;&lt;/p&gt;


	&lt;p&gt;can inject code into running threads, but this is programmatic rather than a built in part of the virtual machine.&lt;/p&gt;


time constructs:
&lt;pre&gt;&lt;code&gt;1::week //stopped here because month and week are variable size
1::day
1::hour
1::minute
1::second
1::ms
1::samp&lt;/code&gt;&lt;/pre&gt;

could do:
&lt;pre&gt;&lt;code&gt;52::week =&gt; dur year; //define 1::year&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;up next: sounds, parallel, controllers&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;SinOsc foo =&gt; dac; //foo is local to this shred&lt;/code&gt;&lt;/pre&gt;

	&lt;h2&gt;Links on the ChucK Language&lt;/h2&gt;


	&lt;ul&gt;
	&lt;li&gt;home page for ChucK is: &lt;a href=&quot;http://chuck.cs.princeton.edu&quot;&gt;chuck.cs.princeton.edu&lt;/a&gt; &#8211; download chuck, miniAudicle&lt;/li&gt;
		&lt;li&gt;&lt;a href=&quot;http://chuck.cs.princeton.edu/doc/language&quot; title=&quot;almost completely up to date&quot;&gt;language specs&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Check out the unit generators listing in the specs. Eventually users will be able to develop their own unit generators.&lt;/p&gt;


	&lt;p&gt;BiQuad is all you need to implement a filter of any complexity (due to re-factoring).
ResonZ is (probably) the same as reson~ in max/msp&lt;/p&gt;


	&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;STK&lt;/span&gt; unit generators are in chuck: Envelope, &lt;span class=&quot;caps&quot;&gt;ADSR&lt;/span&gt;, Delay, etc.
&lt;span class=&quot;caps&quot;&gt;STK&lt;/span&gt; instruments unit generators
BandedWG &#8211; tibetan bowls, and bowed instruments
Bowed &#8211; cool but not realistic
Flute &#8211; excellent model but hard to control (lots of params)
Shakers &#8211; awesome, 22 presets maracas to points in (statistical method for modeling particle systems &#8211; from Peri Cooks Physm)&lt;/p&gt;


&lt;a href=&quot;http://smelt.cs.princeton.edu&quot; title=&quot;SMELT&quot;&gt;Small Musically Expressive Laptop Toolkit&lt;/a&gt; 
	&lt;ul&gt;
	&lt;li&gt;has some great chuck code&lt;/li&gt;
		&lt;li&gt;keyboard and mouse events&lt;/li&gt;
	&lt;/ul&gt;


	&lt;h2&gt;More Complex Sounds (than a sine wave)&lt;/h2&gt;


Basic Harpsichord sounding instrument with verb code:
&lt;pre&gt;&lt;code&gt;StifKarp foo =&gt; JCRev r =&gt; dac;
.1 =&gt; r.mix;  //set reverb ammount 0 is none, generally params are 0 -1

//advance time!
while ( true )
{
    //choose frequency randomly
    Std.rand2(30,90) =&gt; Std.mtof =&gt; foo.freq;

    //note on!
    1 =&gt; foo.noteOn;
    //advance time
    500::ms =&gt; now;
}&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;There are 50,000 line chuck apps out there!&lt;/p&gt;


Ge&#8217;s Q&#38;A:
	&lt;ul&gt;
	&lt;li&gt;Object system is still not fully implemented.&lt;/li&gt;
		&lt;li&gt;Laptop orchestra recordings are on line (but this doesn&#8217;t give 90 discrete channels of sound that you would get at a concert) &#8211; good chuck examples&lt;/li&gt;
		&lt;li&gt;There are some good non-PLork chuck pieces as well&lt;/li&gt;
		&lt;li&gt;Wiki and forum also has chuck code.&lt;/li&gt;
		&lt;li&gt;Piece called &#8220;TBA&#8221; &#8211; first instance of orchestral livecoding&lt;/li&gt;
		&lt;li&gt;Coding as a gesture. What are the aesthetic of laptop performance beyond the glow of the laptop on the face.&lt;/li&gt;
		&lt;li&gt;The score as a page of code that is going to be typed&#8230;&lt;/li&gt;
		&lt;li&gt;Livecoding communication with comments&lt;/li&gt;
		&lt;li&gt;Rehearsal becomes important&#8230; knowing what kind of sounds you can get out of your algorithms becomes tremendously important.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;h2&gt;Synchronization&lt;/h2&gt;


	&lt;p&gt;There is a rudimentary include system in chuck. Somewhat goofy at this point.
Can do code injection on a running process.&lt;/p&gt;


	&lt;p&gt;Chuck model, no control rate. There is only the advancement of time.
Can create your own control signals if desired (e.g. Math.sine )&lt;/p&gt;


	&lt;p&gt;By default most oscilators have only one input for audio, but have many inputs for control.
run three threads on the command line&lt;/p&gt;


	&lt;p&gt;Time based concurrency:
&amp;gt; chuck moe larry curly&lt;/p&gt;


	&lt;p&gt;If they are started concurrently they are all controlled off of the same clock and time is advanced in a concurrent fashion.&lt;/p&gt;


	&lt;p&gt;By the way the full audicle application is really amazing. Very visually interesting&#8230;
World of warcraft style online live coding on the horizon. Share algorithms etc.&lt;/p&gt;


Very important synchronization code convention. You can synchronize applications by using this code in all running synchronized shreds. Uses modulo to quantize events:
&lt;pre&gt;&lt;code&gt;.5::second =&gt; dur T;
T - (now % T) =&gt; now; //take now and use modulo&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;All threads have access to the same system clock
To synchronize threads add the above code to the head of threads to be synchronized&lt;/p&gt;


	&lt;h2&gt;Input devices&lt;/h2&gt;


	&lt;p&gt;Use:
Hid (HidIn deprecated) &#8211; get keyboard input&lt;/p&gt;


	&lt;p&gt;Chuck team has an interest in Commodity devices and using them for music making.&lt;/p&gt;


	&lt;p&gt;Chuck is open source and released under &lt;span class=&quot;caps&quot;&gt;GPL&lt;/span&gt;.
Note some of the waveguides may have patents associated with the algorithms. Patents expire shortly (they are from the early eighties). For the most part you are fine, you may want to check if you are distributing to millions of people.&lt;/p&gt;


	&lt;h2&gt;Karplus Strong Implementation&lt;/h2&gt;


	&lt;p&gt;Karplus and Strong Plucked string filter
- Researchers at Stanford attempting to synthesize drum timbres
- Julius Smith and David Jaffe(?) were inspired to look further
- higher frequencies die off faster than lower frequency&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;//basic karplus strong algorithm
//although they used noise rather than an impulse
//feedforward
Impulse i =&gt; Delay d =&gt; dac;

//feedback with gentle lowpass
d =&gt; OneZero lowpass =&gt; d;

//generate the impulse
.5 =&gt; i.next;

//set the delay length
500::samp =&gt; d.delay; //this length controls the pitch

//dissipation
.99 =&gt; lowpass.gain;

//advance time
4::second =&gt; now;&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;You can currently package code such as the above into a chuck object. But currently you cannot currently make something like this into a unit generator.&lt;/p&gt;


look into the deep folder for interesting code examples:
	&lt;ul&gt;
	&lt;li&gt;&lt;span class=&quot;caps&quot;&gt;THX&lt;/span&gt; emulater (amusing)&lt;/li&gt;
		&lt;li&gt;unclap.ck &#8211; Steve Reich clapping music realization&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;can talk to other applications using &lt;span class=&quot;caps&quot;&gt;OSC&lt;/span&gt; bindings.&lt;/p&gt;


	&lt;h2&gt;The Future&lt;/h2&gt;


	&lt;p&gt;Another chuck workshop digesting advanced material is in the works for about two months from now through the &lt;a href=&quot;http://electronicmusic.meetup.com/152/&quot;&gt;Bay Area Computer Music Technology Group&lt;/a&gt;&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.aquabu.com/">
    <author>
      <name>Noah</name>
    </author>
    <id>tag:www.aquabu.com,2007-10-22:7</id>
    <published>2007-10-22T17:22:00Z</published>
    <updated>2007-10-22T17:37:29Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.aquabu.com/2007/10/22/directed-graphs-in-ruby-on-rails" rel="alternate" type="text/html"/>
    <title>Directed Graphs in Rails (Self Refering Many-To-Many Joins)</title>
<content type="html">
            &lt;p&gt;Here is a simple example of a complex subject. An uncluttered implementation of a directed graph implemented in Rails (adaptable to other Ruby ActiveRecord projects).&lt;/p&gt;


	&lt;p&gt;Let the directed graph consist of &lt;strong&gt;nodes&lt;/strong&gt; and &lt;strong&gt;links&lt;/strong&gt;. Links join nodes together and are directed &lt;strong&gt;from&lt;/strong&gt; a node &lt;strong&gt;to&lt;/strong&gt; another node. Links also have a &lt;strong&gt;value&lt;/strong&gt; associated with them.&lt;/p&gt;


	&lt;p&gt;I should mention that in standard digraph terminology nodes are called vertices, and edges are called links. I chose the terms nodes and links as I think it makes for easier reading.&lt;/p&gt;


	&lt;h2&gt;Build The Model&lt;/h2&gt;


Create the tables like this:
&lt;pre&gt;&lt;code&gt;$ script/generate model node name:string
$ script/generate model link from_id:integer to_id:integer value:integer
$ rake db:migrate&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The migration table definitions should look like this (surrounded by the usual migration code):&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;create_table :nodes do |t|
    t.column :name, :string
end

create_table :links do |t|
    t.column :from_id, :integer
    t.column :to_id, :integer
    t.column :value, :integer
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Create the Node and Link models like this:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;class Link &amp;lt; ActiveRecord::Base
    belongs_to :from_node, :foreign_key =&gt; &quot;from_id&quot;, :class_name =&gt; &quot;Node&quot; 
    belongs_to :to_node, :foreign_key =&gt; &quot;to_id&quot;,   :class_name =&gt; &quot;Node&quot; 
end

class Node &amp;lt; ActiveRecord::Base
    has_many :to_links, :foreign_key =&gt; 'from_id', :class_name =&gt; 'Link' #tricky!
    has_many :to_nodes, :through =&gt; :to_links  

    has_many :from_links, :foreign_key =&gt; 'to_id', :class_name =&gt; 'Link' #tricky!
    has_many :from_nodes, :through =&gt; :from_links
end&lt;/code&gt;&lt;/pre&gt;

	&lt;h2&gt;What&#8217;s Going On In The Model?&lt;/h2&gt;


	&lt;p&gt;Did you see the lines labeled &#8220;tricky!&#8221; in the Node model. Why does &lt;strong&gt;has_many :to_links&lt;/strong&gt; reference &lt;strong&gt;:foreign_key =&amp;gt; &#8220;from_id&#8221;&lt;/strong&gt;? Well, lets check it out.&lt;/p&gt;


	&lt;p&gt;Here is a diagram I created to explain directed relationships from one node to another node and how they are defined in the model. &lt;em&gt;(Whispered aside: if you are feeling frisky and you want to compute the nodes that go to a given node rather than from it, you will have to traverse the diagram in reverse.)&lt;/em&gt;&lt;/p&gt;


	&lt;p&gt;&lt;img src=&quot;http://www.aquabu.com/assets/2007/10/21/digraph_relationships_02.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;


Essentially we traverse in this order:
	&lt;ol&gt;
	&lt;li&gt;From the &lt;strong&gt;:from_node&lt;/strong&gt; via the &lt;strong&gt;from_id&lt;/strong&gt; to the &lt;strong&gt;:to_link&lt;/strong&gt;&lt;/li&gt;
		&lt;li&gt;From the &lt;strong&gt;:to_link&lt;/strong&gt; via the &lt;strong&gt;to_id&lt;/strong&gt; to the &lt;strong&gt;:to_node&lt;/strong&gt;&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;Here&#8217;s the corresponding code from the Node model that makes this hapen.&lt;/p&gt;


1) Traverse from the &lt;strong&gt;:from_node&lt;/strong&gt; via the &lt;strong&gt;from_id&lt;/strong&gt; to the &lt;strong&gt;:to_link&lt;/strong&gt;
&lt;pre&gt;&lt;code&gt;has_many :to_links, :foreign_key =&gt; 'from_id', :class_name =&gt; 'Link' &lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;2) Traverse &lt;strong&gt;:to_link&lt;/strong&gt; via the &lt;strong&gt;to_id&lt;/strong&gt; to the &lt;strong&gt;:to_node&lt;/strong&gt;&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;has_many :to_nodes, :through =&gt; :to_links&lt;/code&gt;&lt;/pre&gt;

&#8220;Where are the :from_node and :to_node terms defined?&#8221; you may ask. They are defined in the Link model along with the belongs_to statements.
&lt;pre&gt;&lt;code&gt;belongs_to :from_node, :foreign_key =&gt; &quot;from_id&quot;, :class_name =&gt; &quot;Node&quot; 
belongs_to :to_node, :foreign_key =&gt; &quot;to_id&quot;,   :class_name =&gt; &quot;Node&quot;&lt;/code&gt;&lt;/pre&gt;

	&lt;h2&gt;&lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; Nitty Gritty&lt;/h2&gt;


	&lt;p&gt;Lets disect what all of this means in terms of &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt;. If you are ready to get right to it, you can skip this section and go straight to playing with the model.&lt;/p&gt;


In &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; selecting nodes linked from node &#8220;a&#8221; to node &#8220;b&#8221; would look something like the following. Notice that I alias the node table as both from_node and to_node (the node table plays two roles).
&lt;pre&gt;&lt;code&gt;SELECT to_nodes.name 
FROM nodes from_nodes, nodes to_nodes, links
WHERE from_nodes.id = links.from_id
AND to_nodes.id = links.to_id
AND from_nodes.name = 'a'
AND to_nodes.name = 'b'&lt;/code&gt;&lt;/pre&gt;

Looking at our diagram we see this bit of code from the Node model which defines the relationship from a Node to a Link:
&lt;pre&gt;&lt;code&gt;has_many :to_links, :foreign_key =&gt; 'from_id', :class_name =&gt; 'Link' &lt;/code&gt;&lt;/pre&gt;

This corresponds to this sql statment:
&lt;pre&gt;&lt;code&gt;WHERE from_nodes.id = links.from_id&lt;/code&gt;&lt;/pre&gt;

Looking at our diagram once again we see this bit of code from the Node model which defines the relationship between from a Link to a Node:
&lt;pre&gt;&lt;code&gt;has_many :to_nodes, :through =&gt; :to_links&lt;/code&gt;&lt;/pre&gt;

This corresponds to this sql statement:
&lt;pre&gt;&lt;code&gt;AND to_nodes.id = links.to_id&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;One last note about the naming of the model. Although the symbols do represent an non-arbitrary relationship, the names for :from_nodes, :to_nodes, :from_links, and :to_links are arbitrary. I have selected a naming scheme that I believe reveals the structure of the relationships, but if you find something more suitable to your tastes ActiveRecord won&#8217;t complain.&lt;/p&gt;


	&lt;h2&gt;Playing With The Model&lt;/h2&gt;


	&lt;p&gt;Now that you have grokked that, lets try out the model. Here&#8217;s the diagram of the digraph that we are about to create:&lt;/p&gt;


	&lt;p&gt;&lt;img src=&quot;http://www.aquabu.com/assets/2007/10/22/Nodes.jpeg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;


Open up your rails app console:
&lt;pre&gt;&lt;code&gt;$ script/console&lt;/code&gt;&lt;/pre&gt;

Create some nodes and connect them with links:
&lt;pre&gt;&lt;code&gt;a = Node.create(:name =&gt; &quot;a&quot;)
b = Node.create(:name =&gt; &quot;b&quot;)
c = Node.create(:name =&gt; &quot;c&quot;)
d = Node.create(:name =&gt; &quot;d&quot;)
Link.create(:from_node =&gt; a, :to_node =&gt; b, :value=&gt;7)
Link.create(:from_node =&gt; b, :to_node =&gt; c, :value=&gt;14)
Link.create(:from_node =&gt; c, :to_node =&gt; d, :value=&gt;21)
Link.create(:from_node =&gt; b, :to_node =&gt; d, :value=&gt;28)
Link.create(:from_node =&gt; d, :to_node =&gt; a, :value=&gt;35)&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Now run some queries.&lt;/p&gt;


a links &lt;strong&gt;to&lt;/strong&gt; b:
&lt;pre&gt;&lt;code&gt;a.to_nodes[0].name # returns: b
a.to_nodes.first.name #an alternate, also returns b&lt;/code&gt;&lt;/pre&gt;

b links &lt;strong&gt;to&lt;/strong&gt; c and d:
&lt;pre&gt;&lt;code&gt;b.to_nodes[0].name #returns c
b.to_nodes[1].name #returns d
b.to_nodes.each {|node| puts node.name} # returns: c, d&lt;/code&gt;&lt;/pre&gt;

There is a link to a &lt;strong&gt;from&lt;/strong&gt; d:
&lt;pre&gt;&lt;code&gt;a.from_nodes[0].name # returns: d&lt;/code&gt;&lt;/pre&gt;

One way to retrieve a node two links away:
&lt;pre&gt;&lt;code&gt;a.to_nodes[0].to_nodes[0].name #returns c&lt;/code&gt;&lt;/pre&gt;

Get the value associated with a link between two nodes:
&lt;pre&gt;&lt;code&gt;a.to_links[0].value #returns 7&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;There you go. Now you know how to make directed graphs in Ruby on Rails. Go forth and digraphify.&lt;/p&gt;


	&lt;p&gt;One final note, this posting was inspired by &lt;a href=&quot;http://blog.hasmanythrough.com/2006/4/21/self-referential-through&quot;&gt;Josh Susser&#8217;s post on the same topic.&lt;/a&gt; He has some other great topics on &#8220;has_many :through&#8221; relationships &#8211; in fact that&#8217;s even what his blog is called. I recommend reading his posts if this type of model manipulation strikes your fancy.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.aquabu.com/">
    <author>
      <name>Noah</name>
    </author>
    <id>tag:www.aquabu.com,2007-09-27:6</id>
    <published>2007-09-27T01:59:00Z</published>
    <updated>2007-09-27T02:08:37Z</updated>
    <link href="http://www.aquabu.com/2007/9/27/css-li-elements-in-explorer" rel="alternate" type="text/html"/>
    <title>CSS li Elements in Explorer</title>
<content type="html">
            Quick report. You may have a reason not to do this... but it may just bail you out.
The vertical spacing of li elements can be quite wide on IE.

This looked bad:

&lt;pre&gt;&lt;code&gt;ul{
    padding-left:0;
    margin-left:0;
}

li{
    list-style-type: none;
    list-style-position: inside;
    marker-offset: 3px;
}&lt;/code&gt;&lt;/pre&gt;

Adding this to the li tag fixed it:

*vertical-align: bottom; width: 200px;*

In context it looks like this:
&lt;pre&gt;&lt;code&gt;li{
    list-style-type: none;
    list-style-position: inside;
    marker-offset: 3px;
    vertical-align: bottom;
    width: 200px;
}&lt;/code&gt;&lt;/pre&gt;

Let me know if you run into side effects or if it works for you.
          </content>  </entry>
  <entry xml:base="http://www.aquabu.com/">
    <author>
      <name>Noah</name>
    </author>
    <id>tag:www.aquabu.com,2007-09-24:5</id>
    <published>2007-09-24T17:09:00Z</published>
    <updated>2007-09-26T16:26:20Z</updated>
    <category term="Events"/>
    <link href="http://www.aquabu.com/2007/9/24/bay-area-computer-music-technology-meetup-chuck-python-vst-and-more" rel="alternate" type="text/html"/>
    <title>Bay Area Computer Music Technology Meetup: ChucK, Python VST, and More</title>
<content type="html">
            &lt;p&gt;Bay Area Computer Music Technology Meetup: ChucK, Python &lt;span class=&quot;caps&quot;&gt;VST&lt;/span&gt;, and More&lt;br /&gt;
Thursday, October 4, 2007, 6:00 PM &lt;br /&gt;
Space Gallery, 1141 Polk St , San Francisco, CA &lt;br /&gt;
&lt;a href=&quot;http://electronicmusic.meetup.com/152/calendar/6391689/&quot;&gt;Please &lt;span class=&quot;caps&quot;&gt;RSVP&lt;/span&gt;&lt;/a&gt; &lt;br /&gt;&lt;/p&gt;


	&lt;p&gt;Come meet each other and participate in planned and impromptu presentations (&#8220;lightning talks&#8221;). Presentations will be Ge Wang and Spencer Salazar on ChucK, and Tim Thompson on his Python &lt;span class=&quot;caps&quot;&gt;VST&lt;/span&gt; library. Bring your laptop if you would like to make a short impromptu presentation (projection screen and sound system available) or tell the group about new projects and opportunities.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;&lt;span class=&quot;caps&quot;&gt;PLANNED PRESENTATIONS&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Ge Wang and Spencer Salazar presenting on the &lt;a href=&quot;http://chuck.cs.princeton.edu/&quot;&gt;ChucK&lt;/a&gt;  audio programming language. Ge and Spencer are both Alumni of the Princeton Sound Lab and Ge is joining the faculty at Stanford in the Center for Computer Research in Music and Acoustics (CCRMA). There is a lot of interest forming in the Computer Music community around ChucK and they have been at it&#8217;s originating point. Ge is the chief architect and co-creator of the language and Spencer is the author of several key components, as well as the miniAudicle, an &lt;span class=&quot;caps&quot;&gt;IDE&lt;/span&gt; for enjoyable ChucKing. Ge is also enthusiastic about starting a West Coast Laptop orchestra.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;ul&gt;
	&lt;li&gt;Tim Thompson will be presenting on a library he has written that allows Python programmers to access the vast variety of &lt;span class=&quot;caps&quot;&gt;VST&lt;/span&gt; plugins and synth instruments. He is a former longtime Bell Labs / AT&#38;T / Network Appliance developer who has written a number of computer music frameworks (notably KeyKit). He has more recently been inspired by events such as Burning Man and Woodstockhausen, where his interactive creations include a 12-foot high lyre and an antique radio. Tim&#8217;s &lt;a href=&quot;http://nosuch.com/tjt/index.html&quot;&gt;home page&lt;/a&gt; describes his projects and hosts his web-based algorithmic music toys.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;ul&gt;
	&lt;li&gt;Short &#8220;lightning talk&#8221; style presentations from users who have attended with their laptops. Contacting me in advance for this is not necessary unless you have specific concerns.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;The event will be held at &lt;a href=&quot;http://www.spacegallerysf.com/&quot;&gt;Space Gallery&lt;/a&gt;, 1141 Polk St in San Francisco. There will be a projection screen and stereo sound system. It is an enjoyable space and will be open for people to socialize after the presentations. There will be light food to enjoy while you are talking. Many thanks to Ray at Space Gallery for hosting this meetup!&lt;/p&gt;


	&lt;p&gt;At 9pm the venue will switch gears from Computer Music Technology Group tech talk. Further eclectic inspiration begins at 9pm as professional skateboarders do a short exhibition on a small portable half-pipe skate ramp inside the gallery while CB Records Mophone and friends perform mashup sets of live rock and funk re-mixes. Should be amazing!&lt;/p&gt;


	&lt;p&gt;Polk street also offers a variety of restaurants for after meetup dining and conversing.&lt;/p&gt;


	&lt;p&gt;Looking forward to meeting all of you, and please feel free to contact me about presenting for future events, hosting or sponsoring an event, etc. &#8211; noah [at] listenlabs.com.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;&lt;span class=&quot;caps&quot;&gt;BIOS&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;Ge Wang received his B.S. in 2000 in Computer Science from Duke University and PhD in 2007 studying with Perry Cook in Computer Science at Princeton University, and is an assistant professor at Stanford University in the Center for Computer Research in Music and Acoustics (CCRMA). Ge conducts research in real-time software systems for computer music, programming languages, visualization, new performance ensembles (e.g., laptop orchestras) and paradigms (e.g., live coding), interfaces for human-computer interaction, pedagogical methodologies at the intersection of computer science and computer music. Ge is the chief architect of the ChucK audio programming language and the Audicle environment. He is a founding developer and co-director of the Princeton Laptop Orchestra (PLOrk), and a co-creator of the &lt;span class=&quot;caps&quot;&gt;TAPESTREA&lt;/span&gt; sound design environment. Ge composes and performs via various electro-acoustic and computer-mediated means.&lt;/p&gt;


	&lt;p&gt;Tim Thompson enjoys the creative process of developing artistic software for both music and visuals, often involving the use of unusual controllers. Most widely known as the developer of the KeyKit programming environment for algorithmic and realtime &lt;span class=&quot;caps&quot;&gt;MIDI&lt;/span&gt; experimentation, he has more recently been inspired by events such as Burning Man and Woodstockhausen, where his interactive creations include a 12-foot high lyre and an antique radio. He lives in Silicon Valley and collaborates with local ensembles &lt;a href=&quot;http://dudland.com/&quot;&gt;Dud&lt;/a&gt; and &lt;a href=&quot;http://double-vision.biz/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;DOUBLE VISION&lt;/span&gt;&lt;/a&gt;. &lt;a href=&quot;http://nosuch.com/tjt/index.html&quot;&gt;Tim&#8217;s home page&lt;/a&gt; documents the variety of his activities and allows people to play with web-based algorithmic music toys.&lt;/p&gt;


	&lt;p&gt;Spencer Salazar is a software engineer and music technologist from San Francisco. A cohort of the Princeton Soundlab, he is the main developer of miniAudicle, an integrated development environment for the ChucK audio programming language. He has implemented several components of ChucK proper, and has composed for, performed in, and developed software for the Princeton Laptop orchestra. By day he works on human-computer interface technologies for a Bay Area product development group.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;&lt;span class=&quot;caps&quot;&gt;ABOUT THE BAY AREA COMPUTER MUSIC TECHNOLOGY GROUP&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;Meet other computer music technology enthusiasts for presentations of computer music techniques and technologies (all music genre outputs are welcome). If you are a user of max/msp, csound, ChucK, &lt;span class=&quot;caps&quot;&gt;CLM&lt;/span&gt;, Live, Reaktor, Kyma, &lt;span class=&quot;caps&quot;&gt;JMSL&lt;/span&gt;, SuperCollider, or other computer music synthesis, composition, and live performance software this group is for you. Come to learn new techniques, better the code base, find collaborators, and find out what what&#8217;s happening in studios, labs, and live work spaces around the Bay Area.&lt;/p&gt;


	&lt;p&gt;&lt;a href=&quot;http://electronicmusic.meetup.com/152/&quot;&gt;Join the Bay Area Computer Music Group and &lt;span class=&quot;caps&quot;&gt;RSVP&lt;/span&gt; for the first event here&lt;/a&gt;&lt;/p&gt;
          </content>  </entry>
</feed>
