<?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 Geekdom)</title><link>https://jamesaimonetti.com/</link><description></description><atom:link href="https://jamesaimonetti.com/categories/geekdom.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><lastBuildDate>Wed, 26 Mar 2025 20:45:28 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Better Blogging via Emacs</title><link>https://jamesaimonetti.com/posts/392/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
Finally trying to figure out how to get back into writing a little more
consistently. Since I spend so much time in Emacs, blogging from Emacs
might help in that endeavor. To that end, I've installed
[org2blog](&lt;a href="https://github.com/punchagan/org2blog"&gt;https://github.com/punchagan/org2blog&lt;/a&gt;) via [this blog
post](&lt;a href="http://blog.gabrielsaldana.org/post-to-wordpress-blogs-with-emacs-org-mode/"&gt;http://blog.gabrielsaldana.org/post-to-wordpress-blogs-with-emacs-org-mode/&lt;/a&gt;).
One caveat, you'll need to download xml-rpc.el from
&lt;a href="http://launchpad.net/xml-rpc-el"&gt;http://launchpad.net/xml-rpc-el&lt;/a&gt; and add (require 'xml-rpc) to your
.emacs file.
&lt;/p&gt;</description><category>Geekdom</category><guid>https://jamesaimonetti.com/posts/392/</guid><pubDate>Wed, 24 Oct 2012 17:13:00 GMT</pubDate></item><item><title>Emulating Webmachine's {halt, StatusCode} in Cowboy</title><link>https://jamesaimonetti.com/posts/2012/03/15/emulating-webmachines-halt-statuscode-in-cowboy/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
At &lt;a href="http://2600hz.org"&gt;2600Hz&lt;/a&gt;, we recently converted our REST
webserver from
&lt;a href="https://github.com/mochi/mochiweb"&gt;Mochiweb&lt;/a&gt;/&lt;a href="https://github.com/basho/webmachine"&gt;Webmachine&lt;/a&gt;
to &lt;a href="https://github.com/extend/cowboy"&gt;Cowboy&lt;/a&gt;, with
&lt;a href="https://github.com/extend/cowboy/blob/master/src/cowboy_http_rest.erl"&gt;cowboy\&lt;sub&gt;http&lt;/sub&gt;\&lt;sub&gt;rest&lt;/sub&gt;&lt;/a&gt;
giving us a comparable API to process our REST requests with. One
feature that was missing, however, was an equivalent to Webmachine's
&lt;a href="http://wiki.basho.com/Webmachine-Resource.html#Halting-Resources"&gt;{halt,
StatusCode}&lt;/a&gt; return. While there has been
&lt;a href="https://github.com/extend/cowboy/pull/144"&gt;chatter&lt;/a&gt; about adding this
to cowboy\&lt;sub&gt;http&lt;/sub&gt;\&lt;sub&gt;rest&lt;/sub&gt;, we've got a function that emulates the behaviour
pretty well (this is cleaned up a bit from our actual function, removing
project-specific details).
&lt;/p&gt;

&lt;pre class="example" id="org567cd62"&gt;
-spec halt/4 :: (#http_req{}, integer(), iolist(), #state{}) -&amp;gt; {'halt', #http_req{}, #state{}}.
halt(Req0, StatusCode, RespContent, State) -&amp;gt;
    {ok, Req1} = cowboy_http_req:set_resp_body(Content, Req0),
    {ok, Req2} = cowboy_http_req:reply(StatusCode, Req1),
    {halt, Req2, State}.
&lt;/pre&gt;

&lt;p&gt;
Obviously you can omit setting the response body if you don't plan to
return one.
&lt;/p&gt;</description><category>Geekdom</category><category>Research</category><category>Work</category><guid>https://jamesaimonetti.com/posts/2012/03/15/emulating-webmachines-halt-statuscode-in-cowboy/</guid><pubDate>Thu, 15 Mar 2012 12:26:05 GMT</pubDate></item><item><title>CouchDB/BigCouch Bulk Insert/Update</title><link>https://jamesaimonetti.com/posts/2012/01/27/couchdbbigcouch-bulk-insertupdate/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
While writing a bulk importer for Crossbar, I took a look at squeezing
some performance out of BigCouch for the actual inserting of documents
into the database. My first time running all the documents into BigCouch
at the same time resulted in some poor performance, so I went digging
around for some ideas on how to improve the insertions. Reading up on
the &lt;a href="http://guide.couchdb.org/draft/performance.html"&gt;High Performance
Guide&lt;/a&gt; for CouchDB (which BigCouch is API-compliant with), I started to
play with chunking my inserts up to get better overall execution time.
&lt;b&gt;Note&lt;/b&gt;: the following are very unscientific results, but I think are
fairly instructive for what one might expect.
&lt;/p&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col class="org-right"&gt;

&lt;col class="org-right"&gt;
&lt;/colgroup&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th scope="col" class="org-right"&gt;Docs Per Insertion&lt;/th&gt;
&lt;th scope="col" class="org-right"&gt;Elapsed Time (ms)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="org-right"&gt;26618&lt;/td&gt;
&lt;td class="org-right"&gt;107176&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class="org-right"&gt;1000&lt;/td&gt;
&lt;td class="org-right"&gt;8325&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class="org-right"&gt;1500&lt;/td&gt;
&lt;td class="org-right"&gt;5679&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class="org-right"&gt;2000&lt;/td&gt;
&lt;td class="org-right"&gt;3087&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class="org-right"&gt;2500&lt;/td&gt;
&lt;td class="org-right"&gt;1644&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class="org-right"&gt;Docs Per Insertion&lt;/td&gt;
&lt;td class="org-right"&gt;Elapsed Time (ms)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
Based on the CouchDB guide, I decided to not pursue this further, as
dropping insertion time 2 orders of magnitude was fine enough for me! I
may have to bake this into the platform natively. For those interested
in the Erlang code, it is pretty simple. Taking a list of documents to
save, use lists:split/2 to try and split the list. By catching the
error, we can know that the list is less than our threshold, and can
save the remaining list to BigCouch. Otherwise, lists:split/2 chunks our
list into one for saving, and one for recursing back into the function.
Since we don't really care about the results of couch\&lt;sub&gt;mgr&lt;/sub&gt;:save\&lt;sub&gt;docs&lt;/sub&gt;/2,
we could put the calls in the second clause of the case in a spawn to
speed this up (relative to the calling process).
&lt;/p&gt;

&lt;pre class="example" id="orgf019908"&gt;
-spec save_bulk_rates/1 :: (wh_json:json_objects()) -&amp;gt; no_return().
save_bulk_rates(Rates) -&amp;gt;
    case catch(lists:split(?MAX_BULK_INSERT, Rates)) of
        {'EXIT', _} -&amp;gt;
            couch_mgr:save_docs(?WH_RATES_DB, Rates);
        {Save, Cont} -&amp;gt;
            couch_mgr:save_docs(?WH_RATES_DB, Save),
            save_bulk_rates(Cont)
    end.
&lt;/pre&gt;</description><category>Geekdom</category><category>Research</category><category>Work</category><guid>https://jamesaimonetti.com/posts/2012/01/27/couchdbbigcouch-bulk-insertupdate/</guid><pubDate>Fri, 27 Jan 2012 19:57:28 GMT</pubDate></item><item><title>cURL stripping newlines from your CSV or other file?</title><link>https://jamesaimonetti.com/posts/2012/01/26/curl-stripping-newlines-from-your-csv-or-other-file/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
I'm in the process of writing a REST endpoint for uploading CSVs to
&lt;a href="http://wiki.2600hz.org/display/docs/REST+APIs+%28High-Level+Functionality%29"&gt;Crossbar&lt;/a&gt;
as part of &lt;a href="http://github.com/2600hz/2600hz-platform"&gt;our
communications platform&lt;/a&gt; at &lt;a href="http://2600hz.org"&gt;2600hz&lt;/a&gt;. Not wanting
to invoke the full REST client interface, I generally use
&lt;a href="http://curl.haxx.se/"&gt;cURL&lt;/a&gt; to send the HTTP requests. Today,
however, I had quite the time figuring out why my CSV files were being
stripped of their newline characters. The initial invocation:
&lt;/p&gt;

&lt;pre class="example" id="org25a3b6b"&gt;
$&amp;gt; curl http://localhost:8000/v1/path/to/upload -H "Content-Type: text/csv" -X POST -d @file.csv
&lt;/pre&gt;

&lt;p&gt;
Walking through the code, from where I was processing the CSV down to
the webserver handling the connection itself, looking for who was
stripping the newlines, I determined it was coming in sans-newlines and
decided to check out &lt;a href="http://curl.haxx.se/docs/manpage.html"&gt;cURL's man
pages&lt;/a&gt; for what might be amiss. I quickly found that the
&lt;a href="http://curl.haxx.se/docs/manpage.html#-d"&gt;-d option&lt;/a&gt; was treating the
file as ascii, and although the docs don't explicitly say so, it appears
this option will strip the newlines. The resolution is to use the
&lt;a href="http://curl.haxx.se/docs/manpage.html#--data-binary"&gt;–data-binary
flag&lt;/a&gt; so cURL doesn't touch the file before sending it to the server.
&lt;/p&gt;</description><category>Geekdom</category><category>Gotchas</category><category>Work</category><guid>https://jamesaimonetti.com/posts/2012/01/26/curl-stripping-newlines-from-your-csv-or-other-file/</guid><pubDate>Thu, 26 Jan 2012 19:33:43 GMT</pubDate></item><item><title>Cron and infinite loops do not mix</title><link>https://jamesaimonetti.com/posts/2011/03/09/cron-and-infinite-loops-do-not-mix/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
More "expert" code time! From the "expert":
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;
Please put this script in a cron to run every minute
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;pre class="example" id="org76dd61f"&gt;
while true; do
  rsync -a server:remote_dir local_dir
  sleep $freq
done
&lt;/pre&gt;

&lt;p&gt;
local\&lt;sub&gt;dir&lt;/sub&gt; is going to be really, really, really up to date after a few
minutes…the server crash will be epic. Perhaps we should write a
script to find and kill these rogue processes and run it every minute
too, but stagger it with the other cron…
&lt;/p&gt;</description><category>Geekdom</category><category>Work</category><guid>https://jamesaimonetti.com/posts/2011/03/09/cron-and-infinite-loops-do-not-mix/</guid><pubDate>Wed, 09 Mar 2011 17:50:00 GMT</pubDate></item><item><title>Resolving Dialyzer "Function foo/n has no local return" errors</title><link>https://jamesaimonetti.com/posts/2010/11/23/resolving-dialyzer-function-foon-has-no-local-return-errors/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
&lt;a href="http://www.erlang.org/doc/man/dialyzer.html"&gt;Dialyzer&lt;/a&gt; is a great
static analysis tool for &lt;a href="http://www.erlang.org/"&gt;Erlang&lt;/a&gt; and has
helped me catch many bugs related to what types I thought I was passing
to a function versus what actually gets passed. Some of the errors
Dialyzer emits are rather cryptic at first (as seems commonplace in the
Erlang language/environment in general) but after you understand the
causes of the errors, the fix is easily recognized. My most common error
is Dialyzer inferring a different return type that what I put in my
-spec, followed by Dialyzer telling me the same function has no local
return. An example:
&lt;/p&gt;

&lt;pre class="example" id="orgcc211f2"&gt;
foo.erl:125: The specification for foo:init/1 states that the function might also return {'ok',tuple()} but the inferred return is none()
foo.erl:126: Function init/1 has no local return
&lt;/pre&gt;

&lt;p&gt;
The init/1 function (for a gen\&lt;sub&gt;server&lt;/sub&gt;, btw):
&lt;/p&gt;

&lt;pre class="example" id="org59c92ff"&gt;
-spec(init/1 :: (Args :: list()) -&amp;gt; tuple(ok, tuple())).
init(_) -&amp;gt;
  {ok, #state{}}.
&lt;/pre&gt;

&lt;p&gt;
And the state record definition:
&lt;/p&gt;

&lt;pre class="example" id="orge27d458"&gt;
-record(state, {
  var_1 = {} :: tuple(string(), tuple())
  ,var_2 = [] :: list(tuple(string(), tuple()))
}).
&lt;/pre&gt;

&lt;p&gt;
Spot the error? In the record definition, var\&lt;sub&gt;1&lt;/sub&gt; is initialized to an
empty tuple and var\&lt;sub&gt;2&lt;/sub&gt; is initialized to an empty list, yet the spec
typing for the record does not take that into account. The corrected
version:
&lt;/p&gt;

&lt;pre class="example" id="org79b1e1f"&gt;
-record(state, {
  var_1 = {} :: tuple(string(), tuple()) | {}
  ,var_2 = [] :: list(tuple(string(), tuple())) | []
}).
&lt;/pre&gt;

&lt;p&gt;
And now Dialyzer stops emitting the spec error and the no local return
error.
&lt;/p&gt;</description><category>Geekdom</category><category>Research</category><category>Work</category><guid>https://jamesaimonetti.com/posts/2010/11/23/resolving-dialyzer-function-foon-has-no-local-return-errors/</guid><pubDate>Tue, 23 Nov 2010 12:03:31 GMT</pubDate></item><item><title>Erlang and Webmachine</title><link>https://jamesaimonetti.com/posts/2010/04/23/erlang-and-webmachine/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
I'm currently working on a small startup project, for one to meet a need
of some acquaintances, but more importantly to learn me some Erlang with
regards to the web. While I'm further along than I actually expected to
be, I thought I'd begin documenting the steps I've taken towards
building this app. The current nerdities I'm using:
&lt;/p&gt;

&lt;ul class="org-ul"&gt;
&lt;li&gt;&lt;a href="http://www.gnu.org/software/emacs/"&gt;Emacs&lt;/a&gt; - Editor of choice&lt;/li&gt;
&lt;li&gt;&lt;a href="http://erlang.org"&gt;Erlang&lt;/a&gt; - Functional programming language&lt;/li&gt;
&lt;li&gt;&lt;a href="http://bitbucket.org/justin/webmachine/wiki/Home"&gt;Webmachine&lt;/a&gt; -
REST toolkit&lt;/li&gt;
&lt;li&gt;&lt;a href="http://couchdb.apache.org/"&gt;CouchDB&lt;/a&gt; - Document store&lt;/li&gt;
&lt;li&gt;&lt;a href="http://benoitc.github.com/couchbeam/"&gt;Couchbeam&lt;/a&gt; - Couchdb
framework for Erlang&lt;/li&gt;
&lt;li&gt;&lt;a href="http://code.google.com/p/erlydtl/"&gt;ErlyDTL&lt;/a&gt; - Templating engine&lt;/li&gt;
&lt;li&gt;&lt;a href="http://github.com/archaelus/esmtp"&gt;ESTMP&lt;/a&gt; - Email sending&lt;/li&gt;
&lt;li&gt;&lt;a href="http://tsung.erlang-projects.org/"&gt;Tsung&lt;/a&gt; - Stress-testing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
Installation of all of these on a GNU/Linux system is pretty
straightforward, so I won't cover that here. Defaults were used for
Erlang. I installed the other libraries/applications in ~/dev/erlang/lib
and pointed $ERL\&lt;sub&gt;LIBS&lt;/sub&gt; there in my .bashrc. I did follow
&lt;a href="http://beebole.com/en/blog/erlang/test-performance-and-scalability-of-your-web-applications-with-tsung/"&gt;this
guide&lt;/a&gt; for setting up Tsung. The BeeBole site has
&lt;a href="http://beebole.com/erlang/en/"&gt;several other pages&lt;/a&gt; worth reading for
developing web applications in Erlang. Once installed, build the
webmachine project:
&lt;/p&gt;

&lt;pre class="example" id="org6a469d5"&gt;
$WEBMACHINE_HOME/scripts/new_webmachine.erl wm_app /path/to/root
cd /path/to/roow/wm_app
make
./start.sh
&lt;/pre&gt;

&lt;p&gt;
You now have a working project! Of course, I like to have my Erlang
shell inside of emacs while I'm developing, so I added a comment to the
start.sh script that contained the shell parameters. My start.sh looks
like this:
&lt;/p&gt;

&lt;pre class="example" id="orga152bb4"&gt;
#!/bin/sh

# for emacs C-c C-z flags:
# -pa ./ebin -pa ./priv/templates/ebin -boot start_sasl -s wm_app

cd `dirname $0`
exec erl -pa $PWD/ebin $PWD/deps/*/ebin $PWD/deps/*/deps/*/ebin $PWD/priv/templates/ebin -boot start_sasl -s wm_app
&lt;/pre&gt;

&lt;p&gt;
I currently have all of my dependencies in $ERL\&lt;sub&gt;LIBS&lt;/sub&gt;; when I deploy
this to production, I'll add the libs to the wm\&lt;sub&gt;app&lt;/sub&gt;/deps as either a
symlink or copied into the directory. To have the custom shell means you
need the
&lt;a href="http://jamesaimonetti.com/2010/02/03/more-erlangemacs/"&gt;.emacs code&lt;/a&gt;
to start an Erlang shell with custom flags. &lt;b&gt;Important note&lt;/b&gt;: If you
need to specify multiple code paths in the -pa arg, you have to use a
-pa for each path, unlike in the shell command version where any path
after the -pa (or -pz) is added. Another caveat: when starting the
Erlang shell within emacs, if you're currently in a erlang-related
buffer (.erl, .hrl, etc), the default shell is started without the
option to set flags. I typically have the start.sh open anyway to copy
the flags so I don't run into this much anymore; I'm documenting it here
just in case anyone stumbles on it. Now you have a shell within which to
execute commands against your webmachine app, load updated modules, etc.
Coming up, I'll talk about how I'm using ErlyDTL to create templates and
using CouchDB/Couchbeam for the document store.
&lt;/p&gt;</description><category>Geekdom</category><category>Research</category><category>Work</category><guid>https://jamesaimonetti.com/posts/2010/04/23/erlang-and-webmachine/</guid><pubDate>Fri, 23 Apr 2010 08:54:48 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>Problem Solved</title><link>https://jamesaimonetti.com/posts/2010/02/22/problem-solved/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
In the comments section of a recent
&lt;a href="http://www.codinghorror.com/blog/2010/02/the-nonprogramming-programmer.html"&gt;Atwood
post&lt;/a&gt;, commentor Paul Jungwirth (search for the name as I can't find
comment permalinks) posted about a problem from a
&lt;a href="http://www.mail-archive.com/boston-pm@mail.pm.org/msg00100.html"&gt;perl
mailing list&lt;/a&gt; that he would give potential hires. This post is not
about the blog post but about the problem from the comments section.
&lt;b&gt;The Problem&lt;/b&gt; (from the mailing list): Consider the following string: 1
2 3 4 5 6 7 8 9 = 2002 The problem is to add any number of addition &amp;amp;
multiplication operations wherever you'd like on the left such that in
the end you have a valid equation. So for example if it gets you to a
solution you can have: 12 * 345 + 6 … if that works as part of your
solution [it's much too big: 4146]. Bearing in mind that multiplication
takes higher precedence than addition, what is the solution? My answer
generator can be found
&lt;a href="http://github.com/jamesaimonetti/ProjectEuler/blob/master/prob.erl"&gt;here&lt;/a&gt;
in Erlang. I liked the problem because it is in the vein of
&lt;a href="http://projecteuler.net"&gt;Project Euler&lt;/a&gt;. The eval/1 is a slightly
modified version of &lt;a href="http://www.trapexit.org/String_Eval"&gt;this one&lt;/a&gt; on
TrapExit.
&lt;/p&gt;</description><category>Geekdom</category><guid>https://jamesaimonetti.com/posts/2010/02/22/problem-solved/</guid><pubDate>Mon, 22 Feb 2010 13:28:50 GMT</pubDate></item><item><title>PHP, cURL, and POST</title><link>https://jamesaimonetti.com/posts/2010/02/12/php-curl-and-post/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
While working on a script today that had been working, I couldn't for
the life of me figure out why it was failing. It uses the PHP curl\&lt;sub&gt;*&lt;/sub&gt;
functions to make various requests and processes the results. Turns out
when you send a POST body with the CURLOPT\&lt;sub&gt;POSTFIELDS&lt;/sub&gt; and a value field
begins with an at symbol(@), you have to escape it (\@). The reason is
the at symbol is used by curl to denote a file upload path
("@/path/to/upload.file"). So escape the at symbol and you should be
back to good with the curling.
&lt;/p&gt;</description><category>Geekdom</category><category>Research</category><category>Work</category><guid>https://jamesaimonetti.com/posts/2010/02/12/php-curl-and-post/</guid><pubDate>Fri, 12 Feb 2010 14:44:20 GMT</pubDate></item></channel></rss>