citizen428.blog()

Try to learn something about everything

Information Overload 2010-08-29

More Tinkering With J

Since my previous post on APL and J I’ve read the 45 or so pages of Easy J and am quite impressed by this nifty language. It may seem strange at first, but is very logically structured and not difficult to understand at all. That said I still need to constantly have the vocabulary open to write anything remotely useful, but I’m slowly starting to remember more and more of it.

Here are a couple of examples of J’s expressiveness, with the first two based on exercises from our “Core Ruby” course on RubyLearning.org.

How old is a person if he/she is 1 billion seconds old?

1
2
0 365 24 60 60 #: 1000000000
31 259 1 46 40

Using Antibase (#:) it’s easy to see that the answer is 31 years, 259 days, 1 hour, 46 minutes and 40 seconds.

Convert degress Fahrenheit to degrees Celsius.

1
2
3
toc =: %&1.8@-&32
  toc 100
37.7778

Divide by 1.8 after (Atop, @) subtracting 32. I suspect there’s a better way to write this, but it’s the first thing that came to my mind…

Create a sorted set from a list of numbers.

1
2
3
toset =: ({~/:)@~.
  toset 3 4 1 2 4 2 3 5
1 2 3 4 5

Sort (/:) the distinct items (nub) of the list. Don’t concern yourself too much with the passive (~) for now, it’s just there so the arguments to take end up in the right spot.

That’s it for today, in my next post on J I want to focus on the language’s excellent plotting capabilities.

Information Overload 2010-08-22

The web is full of wonderful information. In fact there’s so much of it, it almost feels intimidating at times. I therefore plan to start a series of blog posts about articles/screencasts/lectures/etc. I enjoyed during the week (they may have been published at a totally different time though).

RoR: ‘Call’: Cannot Yield From a Proc Type Filter.

As we all know, there are 2 main differences between procs and lambdas in Ruby.

1. Argument checking:

1
2
3
4
5
6
7
8
9
10
11
12

1
2
3
4
5
6
7
8
9
10
11
12
<span class='line'><span class="o">&gt;&gt;</span> <span class="nb">p</span> <span class="o">=</span> <span class="no">Proc</span><span class="o">.</span><span class="n">new</span> <span class="p">{</span> <span class="o">|</span><span class="nb">name</span><span class="o">|</span> <span class="s2">&quot;Hello </span><span class="si">#{</span><span class="nb">name</span><span class="si">}</span><span class="s2">&quot;</span> <span class="p">}</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="c1">#&lt;Proc:0x000001008dc600@(irb):3&gt;</span>
</span><span class='line'><span class="o">&gt;&gt;</span> <span class="n">l</span> <span class="o">=</span> <span class="nb">lambda</span> <span class="p">{</span> <span class="o">|</span><span class="nb">name</span><span class="o">|</span> <span class="s2">&quot;Hello </span><span class="si">#{</span><span class="nb">name</span><span class="si">}</span><span class="s2">&quot;</span> <span class="p">}</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="c1">#&lt;Proc:0x000001008d3c08@(irb):4 (lambda)&gt;</span>
</span><span class='line'><span class="o">&gt;&gt;</span> <span class="nb">p</span><span class="o">.</span><span class="n">call</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="s2">&quot;Hello &quot;</span>
</span><span class='line'><span class="o">&gt;&gt;</span> <span class="n">l</span><span class="o">.</span><span class="n">call</span>
</span><span class='line'><span class="no">ArgumentError</span><span class="p">:</span> <span class="n">wrong</span> <span class="n">number</span> <span class="n">of</span> <span class="n">arguments</span> <span class="p">(</span><span class="mi">0</span> <span class="k">for</span> <span class="mi">1</span><span class="p">)</span>
</span><span class='line'>	<span class="n">from</span> <span class="p">(</span><span class="n">irb</span><span class="p">):</span><span class="mi">4</span><span class="ss">:in</span> <span class="sb">`block in irb_binding&#39;</span>
</span><span class='line'><span class="sb">	from (irb):6:in `</span><span class="n">call</span><span class="s1">&#39;</span>
</span><span class='line'><span class="s1">	from (irb):6</span>
</span><span class='line'><span class="s1">	from /Users/michi/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `&lt;main&gt;&#39;</span>
</span>

2. The possibility to return from within the closure:

1
2
3
4
5
6
7
8
9
10
11
12
>> p = Proc.new { return }
=> #<Proc:0x000001008c83a8@(irb):7>
>> l = lambda { return }
=> #<Proc:0x000001008c4050@(irb):8 (lambda)>
>> p.call
LocalJumpError: unexpected return
	from (irb):7:in `block in irb_binding'
	from (irb):9:in `call'
	from (irb):9
	from /Users/michi/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'
>> l.call
=> nil

I’ve known about this for a while, but as a Rubyists who until quite recently happily ignored Rails, I didn’t know that ActiveSupport changes the “LocalJumpError” from the second example into

1
2
3

1
2
3
<span class='line'><span class="sb">`call&#39;: Cannot yield from a Proc type filter.</span>
</span><span class='line'><span class="sb">   The Proc must take two arguments and execute</span>
</span><span class='line'><span class="sb">   #call on the second argument. (ArgumentError)</span>
</span>

when you try to return from callbacks like “before_save”. I spent quite some time debugging this before I finally found the solution in this blog post. Instead of

1
2
3
4

1
2
3
4
<span class='line'><span class="n">after_save</span> <span class="k">do</span> <span class="o">|</span><span class="n">foo</span><span class="o">|</span>
</span><span class='line'>  <span class="k">return</span> <span class="k">if</span> <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'>  <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'><span class="k">end</span>
</span>

you have to do

1
2
3
4

1
2
3
4
<span class='line'><span class="n">after_save</span><span class="p">(</span><span class="nb">lambda</span> <span class="p">{</span> <span class="o">|</span><span class="n">foo</span><span class="o">|</span>
</span><span class='line'>  <span class="k">return</span> <span class="k">if</span><span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'>  <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'><span class="p">})</span>
</span>

Probably obvious to more experienced Rails developers, but I really got thrown off by the “ArgumentError” that replaced the “LocalJumpError”.

Rewiring My Brain: APL and J

By relieving the brain of all unnecesary work, a good notation sets it free to concentrate on more advanced problems, and in effect increases the mental power of the race.” – A. N. Whitehead

Yesterday Andreas posted this awesome video of a Game of Life coding session in APL, which really blew my mind. Watch it, it’s well worth 8 minutes of your time!

Being extremely impressed by what I saw, I realized that I never wrote anything in a language from the APL family, so I decided to change that. I couldn’t find a free version of APL running on OSX that wasn’t time limited or required me to apply for an educational license, so I finally settled on J (Wikipedia article), skimmed the docs and wrote solutions to 4 different Project Euler problems. They are all extremely terse, but I lack the APL/J knowledge to further comment on their quality. If you have experience in either language, I’d really appreciate some comments!

Problem 7: Find the 10001st prime.

This ones fairly easy, since the vocabulary contains p: which will return the n-th prime (0 indexed):

1
p: 10000

Problem 3: What is the largest prime factor of the number 600851475143?

1
{: q: 600851475143

Get the tail ({:) of the list of prime factors (q:)

Problem 5: What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

1
*./ }. i.20

Behead (}.) the list of all non-negative integers up to 20 (i.20) and apply (/) the least common multiple (*.).

Problem 1: Find the sum of all the multiples of 3 or 5 below 1000.

This one took me a bit, so we’ll do it step by step. First, apply (/) the residue (|) to the non-negative integers up to (non-inclusive) 1000 (i.1000):

1
2
3
(3 5|/i.1000)
0 1 2 0 1 2 0 1 2 0 1 2 ...
0 1 2 3 4 0 1 2 3 4 0 1 ...

Filter out the elements with index 0 (0 ([: I. =)):
1
2
3
0 ([: I. =) (3 5|/i.1000)
0 3  6  9 12 15 18 21 24 27 30 ...
0 5 10 15 20 25 30 35 40 45 50 ...

Finally append the two lists (,), remove duplicates (~.) and apply (/) +:

1
+/ ~. ,0 ([: I. =) (3 5|/i.1000)

Overall playing with my first non-von Neumann programming language was a rather fun experience, especially after getting over the somewhat strange syntax and the fact that most “words” have monadic and dyadic versions. From my cursory glances J’s documentation looks pretty solid and the language starts making sense a lot quicker than one might expect. It’s definitely stimulating to play around with and I think I’ll do a bit more of that over the next few weeks, especially since I haven’t yet explored the plotting and OpenGL frameworks.

Blog Moved From Serendipity to Wordpress

Ever since I started this blog in January 2005 it was powered by Serendipity (aka S9Y). Lately however I became more and more dissatisfied with it for various reasons, so I finally took the plunge and moved it over to Wordpress. The whole process was surprisingly painless, but did have some caveats, so I decided to post a short description of what I did:

There is a script for migrating S9Y to Wordpress that has been floating around for a while and has been patched/modified by various people. Luckily I found version 1.4, which got updated very recently (June 2010). For some reason it didn’t work for me when I tried to migrate between S9Y 1.5.3 and Wordpress 3.0.1 directly, so I installed Wordpress 2.9.2 first, migrated the content and then upgraded to the most recent version. The import itself was very smooth and successful, categories (which I stopped using a long time ago), users, posts and comments all made it to the new blog! Once you are done importing, you can run several SQL statements provided in readme.txt, so that your new posts will have the same IDs as the old ones, which is necessary for redirecting. For that purpose I added some Apache mod_rewrite rules, so that old post URLs will redirect to the new locations (and so will the RSS and Atom feed so subscriptions don’t get interrupted):

1
2
3
4
RewriteEngine on
RewriteRule ^/archives/([0-9]+)-.*$ /archives/$1 [R,L]
RewriteRule ^/feeds/index.rss2 /feed [R,L]
RewriteRule ^/feeds/atom.xml /feed/atom [R,L]

There’s still quite a bit to do though: first I need to find a theme I like (I might even stick with the one I’m using now and just tweak it a bit), then lots of posts need fixes to their formatting because they were written in Textile (I fixed the most recent 2 so far, it’s rather quick and painless). There are also some widgets and static pages I want to move over from the old blog, so I guess this site will be a work in progress for a while. I am however very satisfied with Wordpress and glad I finally did this move I put off for so long.

30 Free Programming eBooks

Since this post got quite popular I decided to incorporate some of the excellent suggestions posted in the comments, so this list now has more than 50 books in it. BTW: I’m not very strict on the definition of “ebook”, some of them are really just HTML versions of books. [UPDATED: 2012-01-18]

Learning a new programming language always is fun and there are many great books legally available for free online. Here’s a selection of 30 of them:

Lisp/Scheme:
Common Lisp: A Gentle Introduction to Symbolic Computation
How to Design Programs
Interpreting Lisp (PDF, suggested by Gary Knott)
Let Over Lambda
On Lisp
Practical Common Lisp
Programming in Emacs Lisp
Programming Languages. Application and Interpretation (suggested by Alex Ott)
Simply Scheme: Introducing Computer Science (suggested by Peter Aronoff
Successful Lisp: How to Understand and Use Common Lisp (suggested by Juanito)
Structure and Interpretation of Computer Programs
Teach Yourself Scheme in Fixnum Days

Ruby:
The Bastards Book of Ruby (suggested by Dan Nguyen)
Clever Algorithms (suggested by Tales Arvelos)
Data Structures and Algorithms with Object-Oriented Design Patterns in Ruby
Learn Ruby the Hard Way
Learn to Program
MacRuby: The Definitive Guide
Mr. Neighborly’s Humble Little Ruby Book (suggested by tundal45)
Programming Ruby
Read Ruby 1.9
Ruby Best Practices
Ruby on Rails Tutorial Book (suggested by tundal45)

Javascript:
Building iPhone Apps with HTML, CSS, and JavaScript
Eloquent Javascript
Essential JavaScript Design Patterns For Beginners (suggested by Rajaseelan)
jQuery Fundamentals
Mastering Node
The Node Beginner Book (suggested by alessio alex)

Haskell:
Implementing functional languages: a tutorial
Learn You a Haskell for Great Good
Real World Haskell
The Haskell Road to Logic, Maths and Programming

Erlang:
Concurrent Programming in Erlang
Learn You Some Erlang for Great Good

Perl:
Beginning Perl
Higher-Order Perl
Impatient Perl
Modern Perl (suggested by Gregory Brown)

Python:
A Byte of Python (suggested by Raoul Snyman)
Dive Into Python
Dive Into Python 3 (suggested by Rajaseelan)
How to Think Like a Computer Scientist – Learning with Python
Invent Your Own Computer Games with Python (suggested by D)
Learn Python The Hard Way (suggested by Stephen Wyatt Bush)
Non-Programmer’s Tutorial for Python 3 (suggested by WMN Trivia)

Smalltalk:
Dynamic Web Development with Seaside
Pharo by Example (based on the next book in this list, suggested by Anonymous)
Squeak by Example

Misc:
A to Z of C (sugested by Kevin)
Algorithms
The Art of Assembly Language
Building Accessible Websites (suggested by Joe Clark)
The C Book
C# Yellow Book (suggested by Joe Wyatt)
Compiler Construction
Dive Into HTML 5 (suggested by @til)
The Implementation of Functional Programming Languages (suggested by “Def”)
An Introduction to R
Learn Prolog Now!
Learning Go
The Little MongoDB Book (suggested by Francesca Krihely)
The Little Redis Book (suggested by Juan Fatas Huang)
Objective-C 2.0 Essentials
Parsing Techniques (suggested by IronScheme)
Programming Scala
Smooth CoffeeScript
Starting FORTH
Type Theory and Functional Programming

This is far from comprehensive and languages that are completely missing are mostly left out on purpose (e.g. PHP, C++, Java). I’m sure somebody else made a list for them somewhere.

GitHub Stats With Incanter

Today I stumbled upon a post called GitHub Stats on Programming Languages, where the author uses R to create various graphs about GitHub’s top 10 programming languages. He was nice and posted his data set, so I had a go at it with Incanter.

Load the necessary libraries:

Then read in the data set (the delimiter defaults to \, which can be changed with the :delim option) and create a (somewhat arbitrary) list of languages to examine:

Get a tabular view of the data:

Screen shot 2010-08-10 at 19.02.03

Narrow the data set down to languages of interest:

Display a bar chart of repositories per user (this has to be within the with-data form shown above):

Screen shot 2010-08-10 at 23.40.36

More graphs:

Screen shot 2010-08-10 at 23.40.53
Screen shot 2010-08-10 at 23.40.09
Screen shot 2010-08-10 at 23.41.06

The full code:

I just love how simple Incanter makes this type of thing. It’s an immensely cool and useful library, which can do much more than what I showed in this post (and previous ones). I hope that one day there’ll be a book about it!

Emacs for Rubyists

If you are interested in using Emacs for Ruby development, here are the relevant parts from my config file with some additional comments:

I use this setup at work every day and am very happy with it, but if you have any additional suggestions please leave a comment!

Fullscreen Emacs on MacOS X

After years of being an avid Vim user, I recently succumbed to the temptation of Emacs and haven’t looked back since. Now that I slowly understand the concept of “living in Emacs”, I also wanted a fullscreen mode. Turns out you’ll have to patch and build Emacs yourself for that if you are on OSX, so here’s a step by step guide on what I did to build the current Git HEAD (“About Emacs” reports the version as 24.0.50 by the way) with fullscreen support, based on Steve Purcell’s post from last January:

To toggle fullscreen mode call “M-x ns-toggle-fullscreen”, which I have bound to “M-RET”. Looking forward to try this on 1920×1080 at work on Monday!

If you can’t be bothered to build Emacs yourself, you can download the version I built.

Copyright © 2016 - Michael Kohl - Powered by Octopress