<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Chewing the Cud (Posts about News)</title><link>https://jamesaimonetti.com/</link><description></description><atom:link href="https://jamesaimonetti.com/categories/news.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><lastBuildDate>Wed, 26 Mar 2025 20:45:29 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Life Update</title><link>https://jamesaimonetti.com/posts/2012/01/26/life-update/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
Updated the blog to run 3.3.1 - lot of cobwebs around these parts.
Hopefully I can be more proactive in blogging about things going on at
work, and perhaps starting to write about what I'm up to personally (not
that I have much of that right now). Maybe my Google stats will jump
over the 0.3 hits I average! Dare to dream!
&lt;/p&gt;</description><category>Fun</category><category>News</category><guid>https://jamesaimonetti.com/posts/2012/01/26/life-update/</guid><pubDate>Thu, 26 Jan 2012 19:38:18 GMT</pubDate></item><item><title>IT Expo</title><link>https://jamesaimonetti.com/posts/2010/10/07/it-expo/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
Just returned from IT Expo West last night. Three days of learning,
hob-nobbing, and talking myself hoarse about the awesomeness that is
&lt;a href="http://2600hz.com"&gt;2600hz&lt;/a&gt;. We got a decent
&lt;a href="http://voip-software.tmcnet.com/topics/voip-software/articles/106898-itexpo-2600hz-project-competes-with-google-voice-skype.htm"&gt;writeup&lt;/a&gt;
posted on TMC's site, met quite a few people, collected beaucoup
business cards, and generally had a fun time hanging with the team.
Super tired but ready to keep building the best hosted PBX software
platform! Bonus: See Darren's awesome (yet mildly awkward)
&lt;a href="http://www.tmcnet.com/tmc/videos/default.aspx?vid=3113"&gt;video
interview&lt;/a&gt;! Also, VoIP service providers looking to offset calling
costs for their business clients can look at
&lt;a href="http://www.2600hz.com/promo/"&gt;PromoCalling&lt;/a&gt; as a way to compete with
Google and Skype's free calling plans.
&lt;/p&gt;</description><category>News</category><guid>https://jamesaimonetti.com/posts/2010/10/07/it-expo/</guid><pubDate>Thu, 07 Oct 2010 13:34:06 GMT</pubDate></item><item><title>Still Kicking</title><link>https://jamesaimonetti.com/posts/2010/09/17/still-kicking/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
I am still alive and well; just busy. I did write a
&lt;a href="http://www.2600hz.org/blog/"&gt;blog entry&lt;/a&gt; for my company,
&lt;a href="http://www.2600hz.com"&gt;2600hz&lt;/a&gt;. More to come…eventually.
&lt;/p&gt;</description><category>News</category><category>Work</category><guid>https://jamesaimonetti.com/posts/2010/09/17/still-kicking/</guid><pubDate>Fri, 17 Sep 2010 13:06:14 GMT</pubDate></item><item><title>Dealing with CouchDB and Couchbeam</title><link>https://jamesaimonetti.com/posts/358/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
I'll put this disclaimer up right away: My experience with
&lt;a href="http://couchdb.apache.org/"&gt;CouchDB&lt;/a&gt; and
&lt;a href="http://benoitc.github.com/couchbeam/"&gt;Couchbeam&lt;/a&gt; is limited so I am
definitely missing out on some knowledge and understanding that would
probably make things easier. I won't detail CouchDB's offerings here as
I have noted above my limited insight into that world. I will try to
comment on using Couchbeam to get documents into and out of CouchDB, as
well as writing a layer to take Couchbeam's document format and cleaning
it up into slightly prettier Erlang data structures (proplists of
binaries just aren't fun to type). The first thing to do is to connect
to your database and get the PIDs for the connection and the DB using
the couchbeam\&lt;sub&gt;server&lt;/sub&gt;:start\&lt;sub&gt;connection&lt;/sub&gt;\&lt;sub&gt;link&lt;/sub&gt;/{0,1} and
couchbeam\&lt;sub&gt;db&lt;/sub&gt;:open\&lt;sub&gt;or&lt;/sub&gt;\&lt;sub&gt;create&lt;/sub&gt;/2. Couchbeam allows you to name the DB
PID so you don't have to carry it around with you; just pass
{DB\&lt;sub&gt;NAME&lt;/sub&gt;\&lt;sub&gt;AS&lt;/sub&gt;\&lt;sub&gt;ATOM&lt;/sub&gt;, "DB\&lt;sub&gt;NAME&lt;/sub&gt;\&lt;sub&gt;AS&lt;/sub&gt;\&lt;sub&gt;LIST&lt;/sub&gt;"} as the second parameter to
open\&lt;sub&gt;or&lt;/sub&gt;\&lt;sub&gt;create&lt;/sub&gt;/2. Now calls can use DB\&lt;sub&gt;NAME&lt;/sub&gt;\&lt;sub&gt;AS&lt;/sub&gt;\&lt;sub&gt;ATOM&lt;/sub&gt; instead of the
DB process's PID to communicate. The biggest stumbling block out of the
gate is the datatypes used by Couchbeam. Look at the API docs and get
familiar with the types you'll be expected to provide and receive from
the calls to the database and views. A document is either a
json\&lt;sub&gt;object&lt;/sub&gt;() or a 2-tuple {DocID, Revision}. A json\&lt;sub&gt;object&lt;/sub&gt; is a
proplist of [{json\&lt;sub&gt;string&lt;/sub&gt;(), json\&lt;sub&gt;term&lt;/sub&gt;()}]. json\&lt;sub&gt;strings&lt;/sub&gt; are simple,
atom or binary. A json\&lt;sub&gt;term&lt;/sub&gt; is an atom, binary, integer, float, a list
of json\&lt;sub&gt;terms&lt;/sub&gt;, or another json\&lt;sub&gt;object&lt;/sub&gt;(). Practically, for my needs,
all keys are binaries and the majority of values are either binaries or
numeric. I do have one place where I need to store a nested
json\&lt;sub&gt;object&lt;/sub&gt;(); wrap it in a 1-tuple as the value and you should be
good.
&lt;/p&gt;

&lt;pre class="example" id="org7f7582e"&gt;
%% nesting a json_object() as a value in a json_object()
ToNest = [{&amp;lt; &amp;gt;, &amp;lt; &amp;gt;}],
[{&amp;lt; &amp;gt;, &amp;lt; &amp;gt;},
 {&amp;lt; &amp;gt;, { ToNest } }].
&lt;/pre&gt;

&lt;p&gt;
Saving a doc is pretty easy; create the proplist and pass it to
couchbeam\&lt;sub&gt;db&lt;/sub&gt;:save\&lt;sub&gt;doc&lt;/sub&gt;/2. One big gotcha when saving a document was
whether the &amp;lt; &amp;gt; key existed. If the \&lt;sub&gt;rev&lt;/sub&gt; key was undefined, a big ugly
bad\&lt;sub&gt;match&lt;/sub&gt; error was thrown with all sorts of chaos going on around it.
&lt;/p&gt;</description><category>News</category><guid>https://jamesaimonetti.com/posts/358/</guid><pubDate>Wed, 05 May 2010 14:29:47 GMT</pubDate></item><item><title>Erlang and Webmachine Tres</title><link>https://jamesaimonetti.com/posts/338/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
In my
&lt;a href="http://jamesaimonetti.com/2010/04/23/erlang-and-webmachine/"&gt;first
post&lt;/a&gt;, I talked about the basic technologies I'd be using, and how to
get an interactive shell started in emacs that started up your
webmachine application. In this third post, I'll talk a little about
building a session handling module. This will hopefully familiarize you
with using redirects and cookies in your webmachine resources and using
Couchbeam to store and retrieve documents. The high-level usage for
sessions I have currently is:
&lt;/p&gt;

&lt;ol class="org-ol"&gt;
&lt;li&gt;User agent accesses a supported resource (probably a browser
accessing the home page initially).

&lt;ul class="org-ul"&gt;
&lt;li&gt;I am defining the is\&lt;sub&gt;authorized&lt;/sub&gt;/2 function in all of my resource
files that correspond to web pages, as well as the
finish\&lt;sub&gt;request&lt;/sub&gt;/2 function.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;
The second parameter in the functions is a record, currently
defined as
&lt;/p&gt;

&lt;pre class="example" id="orgfc31ccc"&gt;
-record(context, {session}).
&lt;/pre&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;
In the is\&lt;sub&gt;authorized&lt;/sub&gt;/2 function for public facing pages, it returns
the tuple:
&lt;/p&gt;

&lt;pre class="example" id="org4359bca"&gt;
is_authorized(RD, Context) -&amp;gt;
    {true, RD, Context#context{session=session:start(RD)}.

&lt;/pre&gt;

&lt;ul class="org-ul"&gt;
&lt;li&gt;Within session:start/1, I extract the session cookie (if it
exists) and retrieve the session document from CouchDB. If the
session cookie isn't set, I create a new #session{} record with
some defaults pre-filled.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;
#+BEGIN&lt;sub&gt;EXAMPLE&lt;/sub&gt;
%% session.hrl
-record(session, {'&lt;sub&gt;id&lt;/sub&gt;', '&lt;sub&gt;rev&lt;/sub&gt;', user&lt;sub&gt;id&lt;/sub&gt;, expires, created, storage=[]}).
&lt;/p&gt;

&lt;p&gt;
%% session.erl
start(RD) -&amp;gt; get&lt;sub&gt;session&lt;/sub&gt;&lt;sub&gt;rec&lt;/sub&gt;(RD).
&lt;/p&gt;

&lt;p&gt;
get&lt;sub&gt;session&lt;/sub&gt;&lt;sub&gt;id&lt;/sub&gt;(RD) -&amp;gt; wrq:get&lt;sub&gt;cookie&lt;/sub&gt;&lt;sub&gt;value&lt;/sub&gt;(?COOKIE&lt;sub&gt;NAME&lt;/sub&gt;, RD).
&lt;/p&gt;

&lt;p&gt;
%% get&lt;sub&gt;session&lt;/sub&gt;&lt;sub&gt;doc&lt;/sub&gt;(request()) -&amp;gt; not&lt;sub&gt;found&lt;/sub&gt; | json&lt;sub&gt;object&lt;/sub&gt;()
get&lt;sub&gt;session&lt;/sub&gt;&lt;sub&gt;doc&lt;/sub&gt;(RD) -&amp;gt;
    case get&lt;sub&gt;session&lt;/sub&gt;&lt;sub&gt;id&lt;/sub&gt;(RD) of
	undefined -&amp;gt; not&lt;sub&gt;found&lt;/sub&gt;;
	SessDocId -&amp;gt; couchbeam&lt;sub&gt;db&lt;/sub&gt;:open&lt;sub&gt;doc&lt;/sub&gt;(?DB, SessDocId)
    end.
&lt;/p&gt;

&lt;p&gt;
%% get&lt;sub&gt;session&lt;/sub&gt;&lt;sub&gt;rec&lt;/sub&gt;(request()) -&amp;gt; session&lt;sub&gt;rec&lt;/sub&gt;()                                   
get&lt;sub&gt;session&lt;/sub&gt;&lt;sub&gt;rec&lt;/sub&gt;(RD) -&amp;gt;
    case get&lt;sub&gt;session&lt;/sub&gt;&lt;sub&gt;doc&lt;/sub&gt;(RD) of
	not&lt;sub&gt;found&lt;/sub&gt; -&amp;gt;
	    new();
	Doc -&amp;gt; from&lt;sub&gt;couchbeam&lt;/sub&gt;(Doc)
    end.
&lt;/p&gt;

&lt;p&gt;
new() -&amp;gt;
    Now = calendar:datetime&lt;sub&gt;to&lt;/sub&gt;&lt;sub&gt;gregorian&lt;/sub&gt;&lt;sub&gt;seconds&lt;/sub&gt;(calendar:local&lt;sub&gt;time&lt;/sub&gt;()),
    #session{created=Now, expires=?MAX&lt;sub&gt;AGE&lt;/sub&gt;, '&lt;sub&gt;id&lt;/sub&gt;'=couchbeam&lt;sub&gt;util&lt;/sub&gt;:new&lt;sub&gt;uuid&lt;/sub&gt;()}.
&lt;/p&gt;

&lt;p&gt;
#+END&lt;sub&gt;EXAMPLE&lt;/sub&gt;
&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;So first I pull out the session ID from the cookie header in
get\&lt;sub&gt;session&lt;/sub&gt;\&lt;sub&gt;id&lt;/sub&gt;/1 which will return either undefined or the ID.&lt;/li&gt;
&lt;li&gt;If the session ID is undefined, get\&lt;sub&gt;session&lt;/sub&gt;\&lt;sub&gt;doc&lt;/sub&gt;/1 returns the
not\&lt;sub&gt;found&lt;/sub&gt; atom, which is what Couchbeam returns when it can't
find a document. If a session ID is found, I call
couchbeam:open\&lt;sub&gt;doc&lt;/sub&gt;/2 to retrieve the document from CouchDB.&lt;/li&gt;
&lt;li&gt;Finally, if get\&lt;sub&gt;session&lt;/sub&gt;\&lt;sub&gt;doc&lt;/sub&gt;/1 returns not\&lt;sub&gt;found&lt;/sub&gt;, I create a new
session record calling new/0. If a document is returned, I call a
function that transforms the proplist to the session record.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Note:&lt;/b&gt; Couchbeam will auto assign your '\&lt;sub&gt;id&lt;/sub&gt;' if the document to
be saved doesn't have one or it is undefined. While useful, for
sessions it causes a problem as the auto-generated UUIDs are
fairly sequential (not exactly but usually only the last four or
five characters differ). Calling couchbeam\&lt;sub&gt;util&lt;/sub&gt;:new\&lt;sub&gt;uuid&lt;/sub&gt;()
creates your random uuids.&lt;/li&gt;
&lt;li&gt;I store the created time in seconds, as well as the expires time
(30 minutes worth of seconds right now). This allows me to easily
clear ended sessions from CouchDB with a view indexing the session
documents by (created+expires).&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;

&lt;li&gt;Now, as the resource request is processed, I can always access the
session through the Context parameter.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;
When the request is done processing, it is time to save the session
and set the cookie. In wm\&lt;sub&gt;resource&lt;/sub&gt;:finish\&lt;sub&gt;request&lt;/sub&gt;/2 I have:
&lt;/p&gt;

&lt;pre class="example" id="orgd4e7ff2"&gt;
finish_request(RD, Context) -&amp;gt;
    {true, wm_session:finish(Context#context.session, RD), Context}.
&lt;/pre&gt;

&lt;p&gt;
And in session.erl, I define the finish/2 process:
&lt;/p&gt;

&lt;pre class="example" id="org57fc95d"&gt;
%% finish(#session(), request()) -&amp;gt; request().
%%  save session and set cookie headers
finish(S, RD) -&amp;gt; save_session_rec(S, RD).

save_session_rec(S, RD) -&amp;gt;
    DateTime = calendar:local_time(),
    Now = calendar:datetime_to_gregorian_seconds(DateTime),
    case has_expired(S#session.created + S#session.expires) of
        true -&amp;gt; close(S, RD);
        false -&amp;gt;
            D = to_couchbeam(S#session{created=Now,expires=?MAX_AGE}),
            S1 = from_couchbeam(couchbeam_db:save_doc(?DB, D)),
            set_cookie_header(RD, S1, DateTime, ?MAX_AGE)
    end.

close(S, RD) -&amp;gt;
    DateTime = calendar:local_time(),
    SessionDoc = to_couchbeam(S),
    couchbeam_db:delete_doc(?DB, SessionDoc),
    set_cookie_header(RD, S, DateTime, -1).

set_cookie_header(RD, Session, DateTime, MaxAge) -&amp;gt;
    {CookieHeader, CookieValue} = mochiweb_cookies:cookie(?COOKIE_NAME, Session#session.'_id', [{max_age, MaxAge},
                                                                                                {local_time, DateTime}]),
    wrq:set_resp_header(CookieHeader, CookieValue, RD).
&lt;/pre&gt;

&lt;ul class="org-ul"&gt;
&lt;li&gt;So to finish a session, I check if the current session has
expired, and if so close it down. Otherwise, I update the created
and exipres properties of the session and save it to CouchDB, and
then set the cookie headers to reflect the new times.&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;
For a resource needing valid authentication to be accessible, the
is\&lt;sub&gt;authorized&lt;/sub&gt;/2 is slightly different:
&lt;/p&gt;

&lt;pre class="example" id="orgcefa5b7"&gt;
%% wm_resource.erl
is_authorized(RD, Context) -&amp;gt;
    S = session:start(RD),
    case session:is_authorized(S) of
    true -&amp;gt; {true, RD, Context#context{session=S}};
        false -&amp;gt;
            RD0 = wrq:do_redirect(true, RD),
            RD1 = wrq:set_resp_header("Location", "/login", RD0),
            {{halt, 307}, RD1, Context#context{session=S}}
    end.
&lt;/pre&gt;

&lt;pre class="example" id="org5b06321"&gt;
%% session.erl
is_authorized(S) -&amp;gt;
    S#session.user_id =/= undefined andalso
        not has_expired(S#session.created + S#session.expires).
&lt;/pre&gt;

&lt;ul class="org-ul"&gt;
&lt;li&gt;Obviously you can set your own redirect header. The {halt, 307} is
a temporary redirect (since hopefully you've only forgotten to
login or been away too long and your session expired).&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ol&gt;</description><category>News</category><guid>https://jamesaimonetti.com/posts/338/</guid><pubDate>Sat, 24 Apr 2010 12:02:41 GMT</pubDate></item><item><title>Erlang, Euler, Primes, and gen_server</title><link>https://jamesaimonetti.com/posts/2010/03/26/erlang-euler-primes-and-gen_server/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
I have been working on &lt;a href="http://projecteuler.net"&gt;Project Euler&lt;/a&gt;
problems for a while now and many of them have centered around prime
numbers. I've referenced my work with the sieve in
&lt;a href="http://jamesaimonetti.com/2009/12/16/erlang-primes-and-the-sieve-of-eratosthenes/"&gt;other&lt;/a&gt;
&lt;a href="http://jamesaimonetti.com/2009/12/17/erlang-and-primes-again/"&gt;posts&lt;/a&gt;
but found with a particular
&lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=47"&gt;problem&lt;/a&gt;
that some of my functions could benefit from some state being saved
(namely the sieve being saved and not re-computed each time). The
problem called to count prime factors of numbers and find consecutive
numbers that had the same count of prime factors. My
&lt;a href="http://github.com/jamesaimonetti/ProjectEuler/blob/master/primes.erl"&gt;primes
module&lt;/a&gt; had a prime\&lt;sub&gt;factors&lt;/sub&gt;/1 function that would compute the prime
factors and the exponents of those factors (so 644 = 2&lt;sup&gt;2&lt;/sup&gt; * 7 * 23, and
primes:prime\&lt;sub&gt;factors&lt;/sub&gt;(644) would return [{2,2},{7,1},{23,1}]. The
prime\&lt;sub&gt;factors&lt;/sub&gt;/1 looked something like this:
&lt;/p&gt;

&lt;pre class="example" id="org559c35b"&gt;
prime_factors(N) -&amp;gt;
    CandidatePrimes = prime_factors(N, primes:queue(N div 2)),
    PrimeFactors = [ X || X &amp;lt; - CandidatePrimes, N rem X =:= 0 ],
    find_factors(PrimeFactors, N, 0, []).
&lt;/pre&gt;

&lt;p&gt;
The call to find\&lt;sub&gt;factors&lt;/sub&gt;/4 takes the factors and finds the exponents
and can be ignored for now. The time sink, then, is in generating the
CandidatePrimes list. I think my primes:queue/1 function is pretty fast
at generating the sieve, and dividing N by two eliminates a lot of
unnecessary computation, but when you're calling prime\&lt;sub&gt;factors&lt;/sub&gt;/1
thousands of times, the call to queue/1 begins to add up. This is where
I needed to save some state (the sieve) in between calls. Erlang,
fortunately enough, has a module behavior called
&lt;a href="http://www.erlang.org/doc/man/gen_server.html"&gt;gen\&lt;sub&gt;server&lt;/sub&gt;&lt;/a&gt; that
abstracts away a lot of the server internals and lets you focus on the
business bits of the server. I won't discuss it much here as I'm not an
authority on it, but
&lt;a href="http://www.pragprog.com/titles/jaerlang/programming-erlang"&gt;Joe
Armstrong's book&lt;/a&gt; and the Erlang docs have been a great help in
understanding what's happening behind the scene. You can view the
&lt;a href="http://github.com/jamesaimonetti/ProjectEuler/blob/master/prime_server.erl"&gt;prime\&lt;sub&gt;server&lt;/sub&gt;
module&lt;/a&gt; to see what its current state is code-wise. To speed up
prime\&lt;sub&gt;factors&lt;/sub&gt;/1, I split it into two functions prime\&lt;sub&gt;factors&lt;/sub&gt;/1 and
prime\&lt;sub&gt;factors&lt;/sub&gt;/2. The functions look like this:
&lt;/p&gt;

&lt;pre class="example" id="orgc04b099"&gt;
prime_factors(N, CandidatePrimes) -&amp;gt;
    PrimeFactors = [ X || X &amp;lt; - CandidatePrimes, N rem X =:= 0 ],
    find_factors(PrimeFactors, N, 0, []).

prime_factors(N) -&amp;gt;
    prime_factors(N, primes:queue(N div 2)).
&lt;/pre&gt;

&lt;p&gt;
Now, if we don't need to save the queue between calls you can still call
prime\&lt;sub&gt;factors&lt;/sub&gt;/1 as usual. The prime\&lt;sub&gt;server&lt;/sub&gt; module utilizes the
prime\&lt;sub&gt;factors&lt;/sub&gt;/2 function because it initializes its state to contain a
primes sieve (either all primes under 1,000,000 if no arg is passed to
start\&lt;sub&gt;link&lt;/sub&gt;, or all primes =&amp;lt; N when using start\&lt;sub&gt;link&lt;/sub&gt;/1) and the
current upper bound. Now when the server handles the call for getting
the factors, we pass a pared down list of primes to prime\&lt;sub&gt;factors&lt;/sub&gt;/2 and
get a nice speed boost. Well, the heavy lifting is front-loaded in the
initialization of the server (generating the sieve) and in calls that
increase the sieve's size. One improvement there might be to save the
Table generated during the initial sieve creation and start the loop
back up from where it left off (when N &amp;gt; UpTo) but that is for another
time. If you choose your initial value for start\&lt;sub&gt;link&lt;/sub&gt; right,
regenerating the sieve should be unnecessary. The last speed boost was
noticing that calculating the exponents was an unnecessary step so I
wrote a count\&lt;sub&gt;factors&lt;/sub&gt;/1 and count\&lt;sub&gt;factors&lt;/sub&gt;/2 that skips the call to
find\&lt;sub&gt;factors&lt;/sub&gt;/4 and returns the length of the list comprehension. With
these changes complete,
&lt;a href="http://github.com/jamesaimonetti/ProjectEuler/blob/master/p47.erl"&gt;problem
47&lt;/a&gt; went from taking well over 5 minutes to just under 20 seconds to
solve brute force.
&lt;/p&gt;</description><category>News</category><guid>https://jamesaimonetti.com/posts/2010/03/26/erlang-euler-primes-and-gen_server/</guid><pubDate>Fri, 26 Mar 2010 09:33:33 GMT</pubDate></item><item><title>Purely Functional Data Structures and Me</title><link>https://jamesaimonetti.com/posts/2010/03/23/purely-functional-data-structures-and-me/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
Quick post to say that I've put my dabblings into Chris Okasaki's
&lt;a href="http://www.amazon.com/Purely-Functional-Structures-Chris-Okasaki/dp/0521663504"&gt;Purely
Functional Data Structures&lt;/a&gt; book up as a
&lt;a href="http://github.com/jamesaimonetti/Purely-Functional-Data-Structures"&gt;github
repo&lt;/a&gt;. I am picking and choosing what exercises and examples to code,
with the goal being to slowly cover the whole book. Some concessions are
made to fit the ideas into Erlang (like recursively calling an anonymous
function), but overall I think the ideas fit nicely. There is a
&lt;a href="http://www.erlang.org/pipermail/erlang-questions/2000-October/001916.html"&gt;streams
lib&lt;/a&gt; from Richard Carlsson that I found on the Erlang mailing list in
the repo as well that I used for reference for a couple things in
chapter 4. I stuck with my streams being represented either as an empty
list [] or [term() | fun()] with term() being the calculated value and
fun() being the suspension function, instead of the tuple that Richard
chose. After reading further in the thread, I understand why (don't
confuse people that might use proper list functions on streams) but for
my small examples, it was enough to use lists.
&lt;/p&gt;</description><category>Geekdom</category><category>News</category><category>Research</category><guid>https://jamesaimonetti.com/posts/2010/03/23/purely-functional-data-structures-and-me/</guid><pubDate>Tue, 23 Mar 2010 10:24:20 GMT</pubDate></item><item><title>Connect to remote erlang shell while inside emacs</title><link>https://jamesaimonetti.com/posts/2010/01/07/connect-to-remote-erlang-shell-while-inside-emacs/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
While developing my top secret project, I have been getting into the fun
stuff in Erlang and Emacs. Connecting to a running instance of my app
from a remote shell wasn't straightforward to me at first, so below is
my documented way of connecting, as well as dropping into the
&lt;a href="http://www.erlang-fr.org/erlang.org/doc/getting_started/getting_started.html#jcl"&gt;Erlang
JCL&lt;/a&gt; from within an Emacs erlang shell.
&lt;/p&gt;

&lt;ol class="org-ol"&gt;
&lt;li&gt;Start yaws: yaws –daemon -sname appname –conf /path/to/yaws.conf&lt;/li&gt;
&lt;li&gt;Start emacs, and from within emacs start an Erlang shell with C-c C-z
(assuming you have &lt;a href="http://code.google.com/p/distel/"&gt;distel&lt;/a&gt;
configured).&lt;/li&gt;
&lt;li&gt;From the Emacs erlang shell, get into Erlang's JCL by typing C-q C-g
and pressing enter. A \&lt;sup&gt;G&lt;/sup&gt; will be printed at the prompt, but won't be
evaluated until you press enter. You should see the familiar JCL
prompt "User switch command –&amp;gt;".&lt;/li&gt;
&lt;li&gt;Type 'j' to see current jobs you have running locally, which is
probably just the current shell (1 {shell,start,[init]}).&lt;/li&gt;
&lt;li&gt;Type 'r appname@compy' to connect to the remote node identified by
appname ( from the -sname parameter ) on the computer compy (usually
whatever hostname returns)&lt;/li&gt;
&lt;li&gt;Type 'j' to see current jobs, which should list your current shell as
"1 {shell,start,[init]}", and a second shell "2*
{appname@compy,shell,start,[]}".&lt;/li&gt;
&lt;li&gt;Type 'c 2' to connect to the remote shell. You can now run commands
in the node's shell. You may have to press enter again to bring up a
shell prompt.&lt;/li&gt;
&lt;/ol&gt;

&lt;pre class="example" id="orgababa14"&gt;
james@compy 14:33:34 ~/dev/erlang/app
&amp;gt; yaws --daemon -sname app --conf config/yaws.conf

james@compy 14:34:00 ~/dev/erlang/app
&amp;gt; emacs
&lt;/pre&gt;

&lt;pre class="example" id="org5b02b44"&gt;
Eshell V5.7.4  (abort with ^G)
1&amp;gt; ^G

User switch command
 --&amp;gt; j
   1* {shell,start,[init]}
 --&amp;gt; r app@compy
 --&amp;gt; j
   1  {shell,start,[init]}
   2* {app@compy,shell,start,[]}
 --&amp;gt; c 2

1&amp;gt;
&lt;/pre&gt;</description><category>Geekdom</category><category>News</category><category>Research</category><guid>https://jamesaimonetti.com/posts/2010/01/07/connect-to-remote-erlang-shell-while-inside-emacs/</guid><pubDate>Thu, 07 Jan 2010 13:41:18 GMT</pubDate></item><item><title>Posting from Emacs</title><link>https://jamesaimonetti.com/posts/2009/12/24/posting-from-emacs/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
I am posting this short message from emacs using the weblogger.el
package. Cool!
&lt;/p&gt;</description><category>News</category><guid>https://jamesaimonetti.com/posts/2009/12/24/posting-from-emacs/</guid><pubDate>Thu, 24 Dec 2009 19:37:11 GMT</pubDate></item><item><title>Erlang, Primes, and the Sieve of Eratosthenes</title><link>https://jamesaimonetti.com/posts/2009/12/16/erlang-primes-and-the-sieve-of-eratosthenes/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
Working through the Project Euler again and using Erlang to do so, there
are quite a few problems that deal with primes. It is important, then,
to have a library of functions that make generating and validating
primes easy. A typical method for generating primes is using the
&lt;a href="http://en.wikipedia.org/wiki/Sieve_of_eratosthenes"&gt;Sieve of
Eratosthenes&lt;/a&gt;. As discussed in the wiki article, Melissa O'Neill has
&lt;a href="http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf"&gt;shown&lt;/a&gt; the given
Haskell implementation is not a true implementation, and shows a couple
versions that are more true to the algorithm and more performant. I took
the implementation she described using a
&lt;a href="http://en.wikipedia.org/wiki/Priority_queue"&gt;priority queue&lt;/a&gt; on page
7 of the &lt;a href="http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf"&gt;pdf&lt;/a&gt;. I
used a
&lt;a href="http://www.erlang.org/pipermail/erlang-questions/2007-August/028769.html"&gt;skew
heap implementation&lt;/a&gt; I found as the priority queue, modified it
slightly to handle a Key and a Value parameter, and away I went. Here's
the implementation I came up with:
&lt;/p&gt;

&lt;pre class="example" id="org666c94d"&gt;
%% primes.erl
-module(primes).

-export([queue/1]).

queue(N) -&amp;gt;
    sieve_queue(lists:seq(2, N)).

sieve_queue([]) -&amp;gt;
    [];
sieve_queue([X|XS]) -&amp;gt;
    Table = insert_prime(X, skew_kv:empty()),
    [X | sieve_queue(XS, Table)].

insert_prime(P, Table) -&amp;gt;
    skew_kv:insert(P*P, from(P*P, P), Table).

sieve_queue([], _Table) -&amp;gt;
    [];
sieve_queue([X|XS], Table) -&amp;gt;
    {NextComposite, _Value} = skew_kv:min(Table),
    case  NextComposite =&amp;lt; X of
        true -&amp;gt; sieve_queue(XS, adjust(Table, X));
        _Else -&amp;gt; [X | sieve_queue(XS, insert_prime(X, Table))]
    end.

adjust(Table, X) -&amp;gt;
    {N, [Nprime | NS]} = skew_kv:min(Table),
    case N =&amp;lt; X of
        true -&amp;gt;
            T = skew_kv:delete_min(Table),
            T2 = skew_kv:insert(Nprime, NS(), T),
            adjust(T2, X);
        _Else -&amp;gt; Table
    end.

%% from http://www.erlang.org/cgi-bin/ezmlm-cgi?4:mss:177:khdaceipfabbicmifdhf
%% a lazy list that starts at K and increments by Inc
from(K, Inc) -&amp;gt;
    [K|fun()-&amp;gt; from(K+Inc, Inc) end].
&lt;/pre&gt;

&lt;p&gt;
I tried to keep variable names similar to the O'Neill implementation,
but I have modified it a bit to use lazy lists, Erlang-style. Not being
familiar with Haskell, I think lists there are defined lazily by
default, but Erlang needs an explicit construct to do lazy lists.
Fortunately, it isn't terribly hard. So, the code! I named the function
queue because I was implementing prime number generators using other
methods from the paper (the unfaithful sieve and a sieve with a map
instead of a queue), hence why you need to call the function with:
&lt;/p&gt;

&lt;pre class="example" id="orge6021a5"&gt;
1&amp;gt; primes:queue(10).
&lt;/pre&gt;

&lt;p&gt;
I don't think I'll expound on the why, as O'Neill does a nice job
explaining it in her paper, so I'll focus on the how and leave it to the
reader to learn the why. My insert\&lt;sub&gt;prime&lt;/sub&gt; function differs from the
O'Neill insertprime because mine has to account for Erlang-style lazy
lists. My assumption is that the map call in O'Neill's creates a lazy
list and so all the mappings are not computed, since XS is a potentially
huge list of numbers. Instead, I observed that those lists are created
by the map call are arithmetic progressions that start at &lt;i&gt;p*p&lt;/i&gt; and
increase by &lt;i&gt;p&lt;/i&gt;. My simple lazy list that accomplishes this is called
from/2, which I got from a
&lt;a href="http://www.erlang.org/cgi-bin/ezmlm-cgi?4:mss:177:khdaceipfabbicmifdhf"&gt;Joe
Armstrong suggestion&lt;/a&gt; and adapted to my needs. Let's take a quick look
at from/2. We pass in a number K and an increment Inc and return a list
with K at the head and a fun as the tail. The fun wraps a call to
from/2, which acts as the iterator for the lazy list. So evaluating
&lt;/p&gt;

&lt;pre class="example" id="orgb088132"&gt;
1&amp;gt; primes:from(2, 2).
[2|#Fun]
&lt;/pre&gt;

&lt;p&gt;
Here's an example showing the iterator in action:
&lt;/p&gt;

&lt;pre class="example" id="orge5c5b3a"&gt;
57&amp;gt; [K|F1] = primes:from(2, 2).
[2|#Fun]
58&amp;gt; [K2|F2] = F1().
[4|#Fun]
59&amp;gt; [K3|F3] = F2().
[6|#Fun]
60&amp;gt; 
&lt;/pre&gt;

&lt;p&gt;
This lazy list is stored in the queue as the value (line 16) for the key
P*P. The lazy list is then used in the adjust/2 function when we want to
compare the smallest key of the queue to the current number we're
evaluating. Line 28 extracts the K into Nprime, and the fun into NS,
from the from/2 call earlier. Line 32 shows how to get the next number
in the lazy list, NS(), which returns [K+Inc | fun()], and inserts it as
the value into the queue. Again, for the why, consult the O'Neill paper.
Otherwise, the implementation is roughly the same. There are some
improvements and optimizations that could be made, I'm sure, but for a
rough draft, it is fairly speedy, minimal memory usage, and for Project
Euler, it is more than adequate in producing the primes I need to solve
the problems.
&lt;/p&gt;</description><category>News</category><guid>https://jamesaimonetti.com/posts/2009/12/16/erlang-primes-and-the-sieve-of-eratosthenes/</guid><pubDate>Wed, 16 Dec 2009 12:37:54 GMT</pubDate></item></channel></rss>