Dominik Picheta's Bloghttp://picheta.meipsumgenera2014-06-24T16:37:01ZBabel - Nimrod's package managerhttp://picheta.me/articles/2014/06/babel--nimrods-package-manager.html2014-06-24T17:40:00Z2014-06-24T16:36:08ZDominik Picheta
<p>I recently released Babel version 0.4.0 on <a href="https://github.com/nimrod-code/babel">Github</a>. Babel is the official package manager for the <a href="http://nimrod-lang.org">Nimrod programming language</a>. With this release I decided it's time to talk about how far Babel has come since its initial release. Myself and the rest of the Nimrod community have been working very hard on getting the design of Babel just right, it took us 3 iterations and a lot of hard work to get to where we are now. Package managers are notoriously hard to get right but they are very important tools and I am happy with the results!</p>
<p>There are currently <a href="http://nimrod-lang.org/lib.html#babel">76 packages</a> in Babel's package repository, which is currently hosted on Github on the <a href="https://github.com/nimrod-code/packages">nimrod-code/packages</a> repository.</p>
<p>Babel is a very lightweight package manager that uses your filesystem to determine the packages that are installed already. It focuses on ease of implementation, so even if something goes wrong it's easy to circumvent. It supports the 3 major operating systems and likely works on the lesser known ones also.</p>
<p>You can build it from source by simply cloning the repository and using the Nimrod compiler to compile it, like so:</p>
<p><pre class='code'><span class="Operator">$</span> <span class="Identifier">git</span> <span class="Identifier">clone</span> <span class="Identifier">https</span><span class="Punctuation">:</span><span class="Operator">//</span><span class="Identifier">github</span><span class="Operator">.</span><span class="Identifier">com</span><span class="Operator">/</span><span class="Identifier">nimrod</span><span class="Operator">-</span><span class="Identifier">code</span><span class="Operator">/</span><span class="Identifier">babel</span><span class="Operator">.</span><span class="Identifier">git</span>
<span class="Operator">$</span> <span class="Identifier">cd</span> <span class="Identifier">babel</span>
<span class="Operator">$</span> <span class="Identifier">nimrod</span> <span class="Identifier">c</span> <span class="Identifier">src</span><span class="Operator">/</span><span class="Identifier">babel</span>
</pre></p>
<p>If you're on Windows you can <a href="https://github.com/nimrod-code/babel/releases/download/v0.4/babel-0.4_win32.zip">download</a> an archive with a pre-built binary which automates the installation. Take a look at the <a href="https://github.com/nimrod-code/babel#babel">readme</a> for more information.</p>
<p>It's important to note that Babel requires Nimrod and git to be in your PATH. As well as mercurial if you wish to use packages hosted on mercurial repositories.</p>
<p>All Babel packages must currently be hosted on a git or mercurial repository and must contain a ini-formatted file named after your package with the <code>.babel</code> extension. These files contain information about your package: including its name, author name, license, dependencies and more.</p>
<p>Registering packages in the Babel package repository allows you to install the package by name. If a package is not registered in this package repository then it can still be installed via its git or hg URL. This is useful if you wish to keep your package private, or if its not suited for the package repository.</p>
<p>Babel determines package versions by the tags present in the git and hg repositories. In addition to being able to specify a version range when installing a package (<code>babel install commandeer@"> 0.1"</code>), you can also specify a commit hash (<code>babel install commandeer@#26b6c035b6c</code>). This is of course more useful when specifying dependencies for your package and is done in a similar matter. An example .babel file follows:</p>
<p><pre class='code'><span class="Punctuation">[</span><span class="Identifier">Package</span><span class="Punctuation">]</span>
<span class="Identifier">name</span> <span class="Operator">=</span> <span class="StringLit">"foobar"</span>
<span class="Identifier">version</span> <span class="Operator">=</span> <span class="StringLit">"0.1.0"</span>
<span class="Identifier">author</span> <span class="Operator">=</span> <span class="StringLit">"Your Name"</span>
<span class="Identifier">description</span> <span class="Operator">=</span> <span class="StringLit">"Example .babel file."</span>
<span class="Identifier">license</span> <span class="Operator">=</span> <span class="StringLit">"MIT"</span>
<span class="Punctuation">[</span><span class="Identifier">Deps</span><span class="Punctuation">]</span>
<span class="Identifier">Requires</span><span class="Punctuation">:</span> <span class="StringLit">"nimrod >= 0.9.4, commandeer > 0.1, https://github.com/runvnc/bcryptnim.git#head"</span>
</pre></p>
<p>Babel ensures these dependencies are met when installing the package.</p>
<p>Babel also makes sure that these dependencies are not deleted with the use of <code>babel uninstall</code> as long as a package which depends on them is installed. Attempting to uninstall commandeer after installing <code>foobar</code> will be met with an error.</p>
<p>To learn more about creating babel packages take a look at the documentation <a href="https://github.com/nimrod-code/babel/blob/master/developers.markdown">here</a> and if you want to learn more about how to use babel take a look at the <a href="https://github.com/nimrod-code/babel#babel">readme</a>.</p>
<p>I also hope that you will use this as an excuse to try out the Nimrod programming language if you haven't done so already! Exciting things are happening in the world of Nimrod and you should definitely give it a spin! </p>
About Nimrod's featureshttp://picheta.me/articles/2013/10/about-nimrods-features.html2013-10-27T22:10:00Z2014-06-24T13:55:32ZDominik Picheta
<p><a href="http://nimrod-code.org">Nimrod</a> is a relatively new programming language that is severely underrated in comparison to other <span style="font-style: italic;">new</span> programming languages. This article aims to change that, its goal is to show off Nimrod's excellence and to raise awareness about this brilliant programming language.</p>
<p>Nimrod is a compiled programming language with a static type system which puts it in the same category as the Rust, C++ and Go programming languages. The reason that I was attracted to Nimrod in the first place was because of its syntax which unlike the syntax of the 3 languages mentioned above is not C-like. There are no braces and no semicolons. Instead Nimrod's syntax is similar to Python as it uses whitespace to delimit scope.</p>
<p>Let me demonstrate this by showing you a quick code example.</p>
<p><pre class='code'><span class="Keyword">proc</span> <span class="Identifier">fizzBuzz</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">,</span> <span class="Identifier">y</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">)</span> <span class="Operator">=</span>
<span class="Keyword">for</span> <span class="Identifier">i</span> <span class="Keyword">in</span> <span class="Identifier">x</span> <span class="Operator">..</span> <span class="Identifier">y</span><span class="Punctuation">:</span>
<span class="Keyword">if</span> <span class="Identifier">i</span> <span class="Keyword">mod</span> <span class="DecNumber">15</span> <span class="Operator">==</span> <span class="DecNumber">0</span><span class="Punctuation">:</span>
<span class="Identifier">echo</span><span class="Punctuation">(</span><span class="StringLit">"FizzBuzz"</span><span class="Punctuation">)</span>
<span class="Keyword">elif</span> <span class="Identifier">i</span> <span class="Keyword">mod</span> <span class="DecNumber">3</span> <span class="Operator">==</span> <span class="DecNumber">0</span><span class="Punctuation">:</span>
<span class="Identifier">echo</span><span class="Punctuation">(</span><span class="StringLit">"Fizz"</span><span class="Punctuation">)</span>
<span class="Keyword">elif</span> <span class="Identifier">i</span> <span class="Keyword">mod</span> <span class="DecNumber">5</span> <span class="Operator">==</span> <span class="DecNumber">0</span><span class="Punctuation">:</span>
<span class="Identifier">echo</span><span class="Punctuation">(</span><span class="StringLit">"Buzz"</span><span class="Punctuation">)</span>
<span class="Keyword">else</span><span class="Punctuation">:</span>
<span class="Identifier">echo</span><span class="Punctuation">(</span><span class="Identifier">i</span><span class="Punctuation">)</span>
</pre></p>
<p>In the example I declare a new <code>fizzBuzz</code> procedure which takes two parameters of type <code>int</code>, in the body of this procedure there is a for loop which iterates from the value of <code>x</code> to the value of <code>y</code>. This classic FizzBuzz example allows me to demonstrate a lot about the language. What is immediately apparent is the indentation based syntax. At first glance you may think that this code looks very much like Python and while on the outside Nimrod is just that, once you explore it further you will see that it is so much more. The example also shows that Nimrod is statically typed, and I will talk about that at length later in the article.</p>
<p>Now that I have given you a quick taste of what Nimrod looks like. It is time to talk more in-depth about the core features of the language, its compiler and the surrounding tools.</p>
<h1>Nimrod is compiled</h1><p>Nimrod code is compiled ahead of time. First the Nimrod code is translated into C code by the Nimrod compiler and then it's compiled using a C compiler into a binary. This means that Nimrod software is very portable and that wrapping C libraries is a joy. Thanks to this you are able to write software which will work on a wide range of hardware, I have found that my applications work perfectly on both my desktop and my Raspberry Pi. The resulting binaries are also dependency free and very <a href="http://togototo.wordpress.com/2013/08/23/benchmarks-round-two-parallel-go-rust-d-scala-and-nimrod/">fast</a> as they can take advantage of all the optimisations provided by GCC or other C compilers.</p>
<p>These features make Nimrod suitable for low level systems programming, allowing for example the creation of <a href="https://github.com/dom96/nimkernel">operating systems</a>.</p>
<p>Another important feature of the compiler is that it knows exactly which modules your application depends on. It compiles them all for you so there is no need for makefiles, the compiler implements the build system. Compiling code is condensed into a single, simple command: <code>nimrod c file.nim</code>.</p>
<p>The Nimrod compiler also supports other compilation backends including JavaScript, C++ and Objective C. This allows you to write scripts which will work in your web browser and apps which will work on iOS devices.</p>
<h1>Realtime Garbage Collector</h1><p>Nimrod's GC is perfect for real-time software like games. It uses a deferred reference counting algorithm which can have pause times as small as 1ms. The GC is also deterministic, it will only run whenever you try to allocate memory on the heap. The memory usage is very small.</p>
<p>Two functions in the standard library allow the customisation of the GC's behaviour. These include the <code>GC_setMaxPause</code> function and the <code>GC_step</code> function. The former allows you to tell the GC the maximum amount of time that it is allowed to use to perform a collection, this ensures that the GC will not pause your application for longer than you have specified. The latter function allows you to explicitly perform a collection, so you can disable the GC's automatic collections and be in full control of when the GC does its work.</p>
<p>The GC can be switched off by using the <code>--gc:none</code> compiler flag if you are really determined to manage memory yourself. However it is important to note that memory can still be managed manually even while the GC is enabled.</p>
<h1>Static typing</h1><p>Nimrod is, unlike the language it resembles, a statically typed programming language. Some may think that static typing hinders their productivity, I however will not share my opinion about this ongoing dispute in this article. Instead I will focus on the features that Nimrod's type system offers.</p>
<p>Let's say we want to implement a <code>Maybe[T]</code> type. That is, a type which either holds a value of type <code>T</code> or nothing. This may be familiar to people coming from functional programming backgrounds, specifically Haskell.</p>
<p><pre class='code'><span class="Keyword">type</span>
<span class="Identifier">Maybe</span><span class="Punctuation">[</span><span class="Identifier">T</span><span class="Punctuation">]</span> <span class="Operator">=</span> <span class="Keyword">object</span>
<span class="Keyword">case</span> <span class="Identifier">hasValue</span><span class="Operator">*:</span> <span class="Identifier">bool</span>
<span class="Keyword">of</span> <span class="Identifier">true</span><span class="Punctuation">:</span>
<span class="Identifier">value</span><span class="Operator">*:</span> <span class="Identifier">T</span>
<span class="Keyword">of</span> <span class="Identifier">false</span><span class="Punctuation">:</span> <span class="Keyword">nil</span>
<span class="Keyword">var</span> <span class="Identifier">x</span> <span class="Operator">=</span> <span class="Identifier">Maybe</span><span class="Punctuation">[</span><span class="Identifier">string</span><span class="Punctuation">]</span><span class="Punctuation">(</span><span class="Identifier">hasValue</span><span class="Punctuation">:</span> <span class="Identifier">True</span><span class="Punctuation">,</span> <span class="Identifier">value</span><span class="Punctuation">:</span> <span class="StringLit">"Hello World!"</span><span class="Punctuation">)</span>
<span class="Identifier">echo</span> <span class="Identifier">x</span><span class="Operator">.</span><span class="Identifier">value</span> <span class="Comment"># Output: Hello World!</span>
</pre></p>
<p>This example shows Nimrod's <a href="http://en.wikipedia.org/wiki/Generic_programming">generics</a> and <a href="http://nimrod-code.org/manual.html#object-variants">object variants</a> at work. The <code>hasValue</code> field determines whether the <code>value</code> field is present in the object. If it were to be set to <code>False</code> then accessing the <code>value</code> field would cause a runtime exception. Nowadays in some cases this error can be detected at compile-time.</p>
<p>This is not the extent of the power of Nimrod's generics.</p>
<p><pre class='code'><span class="Keyword">proc</span> <span class="Identifier">intOrString</span><span class="Punctuation">[</span><span class="Identifier">T</span><span class="Punctuation">:</span> <span class="Identifier">int</span> <span class="Operator">|</span> <span class="Identifier">string</span><span class="Punctuation">]</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Identifier">Maybe</span><span class="Punctuation">[</span><span class="Identifier">T</span><span class="Punctuation">]</span><span class="Punctuation">)</span> <span class="Operator">=</span>
<span class="Identifier">assert</span> <span class="Identifier">x</span><span class="Operator">.</span><span class="Identifier">hasValue</span>
<span class="Keyword">when</span> <span class="Identifier">T</span> <span class="Keyword">is</span> <span class="Identifier">int</span><span class="Punctuation">:</span>
<span class="Identifier">echo</span> <span class="Identifier">x</span><span class="Operator">.</span><span class="Identifier">value</span> <span class="Operator">+</span> <span class="DecNumber">5</span>
<span class="Keyword">elif</span> <span class="Identifier">T</span> <span class="Keyword">is</span> <span class="Identifier">string</span><span class="Punctuation">:</span>
<span class="Identifier">echo</span> <span class="Identifier">parseInt</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Operator">.</span><span class="Identifier">value</span><span class="Punctuation">)</span> <span class="Operator">+</span> <span class="DecNumber">5</span>
</pre></p>
<p>In this example I define a function <code>intOrString</code> which takes a value of type <code>Maybe[T]</code> where <code>T</code> is only allowed to be an <code>int</code> or a <code>string</code>. If I were to pass a type of <code>Maybe[float]</code> to this function I would be met with a compile-time error. The <code>is</code> operator is used in the body of the function to determine the type of <code>T</code> at compile time. Depending on this type different code is generated at compile-time.</p>
<hr /><p>I will mention quickly that Nimrod supports a range of different data types, these include sets, tuples and subranges. Nimrod distinguishes between traced and untraced pointers. A <code>ref T</code> is a traced reference to type <code>T</code>, these references point to a garbage collected heap and are therefore memory safe. Untraced references (or pointers) point to manually allocated objects and are unsafe but they are necessary for systems programming and wrapping of C code. By default references can be <code>nil</code> but a <code>not nil</code> annotation can be added to reference types which will instruct the compiler to ensure that the type is never <code>nil</code>.</p>
<h1>Procedures</h1><p>Procedures in Nimrod are synonymous to functions in other languages. They can be overloaded and support named arguments which can have default values. Operators are also supported, with the most commonly used being the <code>$</code> (dollar) operator which returns a string representation of a type which implements it.</p>
<p>Anonymous procedures are supported and a special do notation is also provided:</p>
<p><pre class='code'><span class="Keyword">import</span> <span class="Identifier">algorithm</span>
<span class="Keyword">var</span> <span class="Identifier">x</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">"foo"</span><span class="Punctuation">,</span> <span class="StringLit">"123456789"</span><span class="Punctuation">,</span> <span class="StringLit">"i"</span><span class="Punctuation">,</span> <span class="StringLit">"doloroo"</span><span class="Punctuation">]</span>
<span class="Identifier">x</span><span class="Operator">.</span><span class="Identifier">sort</span> <span class="Keyword">do</span> <span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">,</span> <span class="Identifier">y</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">)</span> <span class="Operator">-></span> <span class="Identifier">int</span><span class="Punctuation">:</span>
<span class="Identifier">cmp</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Operator">.</span><span class="Identifier">len</span><span class="Punctuation">,</span> <span class="Identifier">y</span><span class="Operator">.</span><span class="Identifier">len</span><span class="Punctuation">)</span>
<span class="Comment"># x = @["i", "foo", "doloroo", "123456789"]</span>
</pre></p>
<p>The <code>sort</code> procedure takes a <code>sequence</code> of any type and sorts it based on the results that the procedure that is passed to it generates. The do notation is syntactic sugar for a closure being passed as the last argument to a function. In this case it will pass an anonymous procedure which will compare the lengths of the strings. The <code>cmp</code> procedure is in the standard library and it returns a value which is less than 0 if <code>x < y</code>, a value of 0 if <code>x == y</code> and a value greater than 0 if <code>x > y</code>. It is this value which determines the way the sequence will be sorted.</p>
<p>A unique feature in Nimrod is that it forces you to explicitly discard returned values from procedures if you do not wish to use them.</p>
<p><pre class='code'><span class="Keyword">proc</span> <span class="Identifier">ret</span><span class="Punctuation">:</span> <span class="Identifier">int</span> <span class="Operator">=</span>
<span class="Identifier">result</span> <span class="Operator">=</span> <span class="DecNumber">5</span>
<span class="Keyword">discard</span> <span class="Identifier">ret</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
<span class="Identifier">echo</span> <span class="Identifier">ret</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
<span class="Identifier">ret</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Comment"># Error: value returned by statement has to be discarded</span>
</pre></p>
<p>This may look annoying but it is in fact very useful. Especially when working with C functions where the return value indicates a success or a failure in which case this feature encourages you to implement error checking.</p>
<p>The example above also demonstrates another unique feature of Nimrod. The implicit <code>result</code> variable. This variable is automatically declared for you whenever your procedure has a return type.</p>
<p>Procedures can be called in one of two ways, <code>f(a, b)</code> is the same as <code>a.f(b)</code>. The latter is referred to as "method call syntax".</p>
<h1>Effect system and Exceptions</h1><p>Exceptions in Nimrod with an effect system which provides exception tracking.</p>
<p>Nimrod exceptions are very traditional. Exceptions can be caught using a <code>try: .. except: ..</code> statement and they can be raised using the <code>raise</code> keyword. Exception tracking is a great addition to this, a procedure can be proven to be exception free by specifying that it should raise no exceptions. This is done by specifying an empty <code>raises</code> list in the procedure declaration:</p>
<p><pre class='code'><span class="Keyword">proc</span> <span class="Identifier">unsafeCall</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">=</span>
<span class="Keyword">raise</span> <span class="Identifier">newException</span><span class="Punctuation">(</span><span class="Identifier">EIO</span><span class="Punctuation">,</span> <span class="StringLit">"Foo"</span><span class="Punctuation">)</span>
<span class="Keyword">proc</span> <span class="Identifier">safeCall</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">bool</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">raises</span><span class="Punctuation">:</span> <span class="Punctuation">[</span><span class="Punctuation">]</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">=</span>
<span class="Keyword">try</span><span class="Punctuation">:</span>
<span class="Identifier">unsafeCall</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
<span class="Identifier">result</span> <span class="Operator">=</span> <span class="Identifier">true</span>
<span class="Keyword">except</span><span class="Punctuation">:</span>
<span class="Identifier">result</span> <span class="Operator">=</span> <span class="Identifier">false</span>
</pre></p>
<p>The compiler performs an analysis to determine whether the <code>safeCall</code> procedure will raise any exceptions. In the example above the <code>try</code> statement ensures that there will be no exceptions raised and so the requirement is satisfied. If the <code>try</code> statement is removed then the compiler will refuse to compile the code.</p>
<p>The effect system goes much further than that with tag tracking. One of the tags supported is the <code>FIO</code> effect, it is further split up into a <code>FReadIO</code> and <code>FWriteIO</code> effect. This allows you to prove exactly what type of IO your function can perform. Tags can be user-defined and the stdlib also contains many more.</p>
<h1>Iterators</h1><p>Like many language Nimrod supports <a href="http://nimrod-code.org/manual.html#iterators-and-the-for-statement">iterators</a>.</p>
<p><pre class='code'><span class="Keyword">type</span>
<span class="Identifier">Foo</span> <span class="Operator">=</span> <span class="Keyword">tuple</span><span class="Punctuation">[</span><span class="Identifier">x</span><span class="Punctuation">,</span><span class="Identifier">y</span><span class="Punctuation">,</span><span class="Identifier">z</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">]</span>
<span class="Keyword">iterator</span> <span class="Identifier">items</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">:</span> <span class="Identifier">Foo</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">int</span> <span class="Operator">=</span>
<span class="Keyword">yield</span> <span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">x</span>
<span class="Keyword">yield</span> <span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">y</span>
<span class="Keyword">yield</span> <span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">z</span>
<span class="Keyword">for</span> <span class="Identifier">i</span> <span class="Keyword">in</span> <span class="Punctuation">(</span><span class="DecNumber">1</span><span class="Punctuation">,</span><span class="DecNumber">2</span><span class="Punctuation">,</span><span class="DecNumber">3</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
<span class="Identifier">echo</span><span class="Punctuation">(</span><span class="Identifier">i</span><span class="Punctuation">)</span>
<span class="Comment"># Output:</span>
<span class="Comment"># 1</span>
<span class="Comment"># 2</span>
<span class="Comment"># 3</span>
</pre></p>
<p>The for loop in this example implicitly calls the <code>items</code> iterator for the <code>Foo</code> type. Iterators can be overloaded just like procedures and so overloading the <code>items</code> iterator for a custom type will allow you to iterate over it using a for loop like in this example.</p>
<h1>Metaprogramming</h1><p>This is possibly one of Nimrod's greatest features. Nimrod's metaprogramming support consists of templates and macros. Templates are a simple form of macros where you declaratively specify the code that you would like to be generated. Whereas macros work on the AST allowing it to be transformed at compile-time.</p>
<p>The combination of the two allows DSLs to be written. The <code>htmlgen</code> module defines a DSL for generating HTML:</p>
<p><pre class='code'><span class="Keyword">import</span> <span class="Identifier">htmlgen</span>
<span class="Identifier">echo</span> <span class="Identifier">h1</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">(</span><span class="Identifier">href</span><span class="Operator">=</span><span class="StringLit">"http://picheta.me"</span><span class="Punctuation">,</span> <span class="StringLit">"My Blog."</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
<span class="Comment"># Output: <h1><a href="http://picheta.me">My Blog.</a></h1></span>
</pre></p>
<p>Templates have many uses, they can remove boilerplate and they allow you to extend the language easily.</p>
<p><pre class='code'><span class="Keyword">import</span> <span class="Identifier">macros</span><span class="Punctuation">,</span> <span class="Identifier">strutils</span>
<span class="Keyword">template</span> <span class="Identifier">table</span><span class="Punctuation">(</span><span class="Identifier">name</span><span class="Punctuation">:</span> <span class="Identifier">expr</span><span class="Punctuation">)</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">immediate</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">=</span>
<span class="Keyword">proc</span> <span class="Punctuation">`</span><span class="Identifier">del</span> <span class="Identifier">name</span><span class="Punctuation">`</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">=</span> <span class="Identifier">db</span><span class="Operator">.</span><span class="Identifier">exec</span><span class="Punctuation">(</span><span class="StringLit">"delete table "</span> <span class="Operator">&</span> <span class="Identifier">astToStr</span><span class="Punctuation">(</span><span class="Identifier">name</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
<span class="Keyword">proc</span> <span class="Punctuation">`</span><span class="Identifier">insert</span> <span class="Identifier">name</span><span class="Punctuation">`</span><span class="Punctuation">(</span><span class="Identifier">values</span><span class="Punctuation">:</span> <span class="Identifier">varargs</span><span class="Punctuation">[</span><span class="Identifier">string</span><span class="Punctuation">]</span><span class="Punctuation">)</span> <span class="Operator">=</span>
<span class="Keyword">var</span> <span class="Identifier">vals</span> <span class="Operator">=</span> <span class="Identifier">values</span><span class="Operator">.</span><span class="Identifier">join</span><span class="Punctuation">(</span><span class="StringLit">","</span><span class="Punctuation">)</span>
<span class="Identifier">db</span><span class="Operator">.</span><span class="Identifier">exec</span><span class="Punctuation">(</span><span class="StringLit">"insert into $1 values ($2)"</span> <span class="Operator">%</span> <span class="Punctuation">[</span><span class="Identifier">astToStr</span><span class="Punctuation">(</span><span class="Identifier">name</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Identifier">vals</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
<span class="Identifier">table</span> <span class="Identifier">test</span>
<span class="Identifier">insertTest</span><span class="Punctuation">(</span><span class="StringLit">"foo"</span><span class="Punctuation">,</span> <span class="StringLit">"bar"</span><span class="Punctuation">)</span>
<span class="Identifier">delTest</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
</pre></p>
<p>In this example the <code>table</code> template generates two procedures. The procedures use the table's name as part of their own name which ensures that you will not get the table's name wrong at compile-time.</p>
<h1>Standard library</h1><p>Nimrod has a very rich standard library. Ranging from parsers for JSON and XML to HTTP clients and database wrappers. There are also many wrappers for C libraries available, many written by the community and available as a Babel package. A full list of modules in the standard library can be found <a href="http://nimrod-code.org/lib.html">here</a>.</p>
<h1>Native Unicode</h1><p>Go makes the fact that it supports unicode very prominent with a unicode "Hello World" on its <a href="http://golang.org/">homepage</a>. Nimrod can do the same thing:</p>
<p><pre class='code'><span class="Identifier">echo</span><span class="Punctuation">(</span><span class="StringLit">"Hello, 世界"</span><span class="Punctuation">)</span>
</pre></p>
<p>Unlike Go however, it manages to do it in a single line. You can also use unicode characters in variable, procedure and type names.</p>
<h1>Tools</h1><p>There is plenty of nimrod tools, some of which I have created personally.</p>
<h2>c2nim</h2><p>This tool allows the translation of C header files into Nimrod code which makes generating wrappers very easy. Recently it also gained support for C++.</p>
<h2>endb</h2><p>This is the <span style="font-weight: bold;">E</span>mbedded <span style="font-weight: bold;">N</span>imrod <span style="font-weight: bold;">D</span>e<span style="font-weight: bold;">b</span>ugger and acts as an alternative to GDB. It provides a more natural debugging environment for Nimrod applications.</p>
<h2><a href="https://github.com/nimrod-code/babel">Babel</a></h2><p>This is a package manager that I have written. It is currently considered the official Nimrod package manager and is home to many packages created by the Nimrod community.</p>
<h2><a href="https://github.com/nimrod-code/Aporia">Aporia</a></h2><p>This is a Nimrod IDE written in Nimrod. Another project of mine. It uses GTK+ as a widget toolkit.</p>
<h1>Community</h1><p>The community is great. The creator of Nimrod hangs out in an IRC channel on Freenode (#nimrod) and is very open to user's suggestions. The development is very transparent and you can learn a lot by even idling in the IRC channel.</p>
<h1>Conclusion</h1><p>Nimrod is definitely a good alternative to many programming languages out there. If you are a Python user looking for a replacement like I was many years ago then I would suggest trying out Nimrod. Even if you're not, Nimrod is still an extremely well designed language which is still evolving and your input could help make it even better.</p>
<p>Even though this article is long it is important to note that I have not mentioned many features that this language has. A comprehensive list of everything can be found in Nimrod's <a href="http://nimrod-code.org/manual.html">manual</a>.</p>
<p>Thanks for reading.</p>
<hr /><p>Discussion on <a href="https://news.ycombinator.com/item?id=6626811">Hacker News</a> and <a href="http://www.reddit.com/r/programming/comments/1pclbq/about_nimrods_features/">Reddit</a>.</p>
GTK+: A method to guarantee scrollinghttp://picheta.me/articles/2013/08/gtk-plus--a-method-to-guarantee-scrolling.html2013-08-23T23:38:00Z2014-06-24T13:55:32ZDominik Picheta
<p>Ever since I started using GTK+ a couple of years ago with a little <a href="https://github.com/dom96/Nyx">Python IRC client</a> project called Nyx I have been faced with scrolling problems in GTK+.</p>
<p>Nyx has been a great introduction to GTK+ programming for me but I have since moved away from that project onto a much bigger and more exciting project called <a href="https://github.com/nimrod-code/Aporia">Aporia</a>. Aporia has started as a simple text editor written in the Nimrod programming language but quickly evolved from a MS Notepad competitor to a gedit competitor. Nowadays it is used by me and the Nimrod community including the person that created Nimrod.</p>
<h1>The problem</h1><p>One of the most important features to me in Aporia is the session restore feature, it's great as it allows me to open many files, close Aporia and then easily get these files opened again when Aporia starts up (it's essentially a gift for tab hoarders like me). Aporia also saves the location of your cursor when you exit it, this means that the cursor position can also easily be restored. Once the cursor position is set the GtkSourceView should be made to scroll so that the cursor is centered in plain sight. This proved to be quite tricky to get working reliably, many times the GtkSourceView scrolled but not far enough resulting in some random part of the document being shown, other times it didn't scroll at all and only on rare occassions it scrolled successfully. This was one of the longest living bugs in Aporia, or at least one of the longest living bugs which I cared so much about.</p>
<h1>The investigation</h1><p>At first the function that I was using to scroll the GtkSourceView was <code>gtk_text_view_scroll_to_iter</code>. I immediately read the docs again and again to see if there was something that I was missing and I noticed that this function in fact returned a <code>gboolean</code> which determined whether scrolling occurred, furthermore the documentation suggested that I should use <code>gtk_text_view_scroll_to_mark</code> instead as it was more reliable -- it actually guaranteed scrolling. So I listened to the advice and did just that. Sadly, there was no improvement. In desperation I even attempted to change the parameters that I passed to the function which again resulted in no improvements.</p>
<p>I won't bore you with the details of my whole debugging process, it was very long, frustrating and not very interesting.</p>
<p>After a while, I figured that there must be something going on behind the scenes in GTK that I simply was not aware of. This of course was the case.</p>
<p>The code that was used to restore the files from the last session looked something like this:</p>
<p><pre class='code'><span class="Keyword">for</span> <span class="Identifier">file</span> <span class="Keyword">in</span> <span class="Identifier">session</span><span class="Operator">.</span><span class="Identifier">files</span><span class="Punctuation">:</span>
<span class="Keyword">var</span> <span class="Identifier">sourceView</span> <span class="Operator">=</span> <span class="Identifier">SourceViewNew</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Comment"># Create a new source view.</span>
<span class="Comment"># Read file into the GtkSourceBuffer ... etc. (ommited for brevity)</span>
<span class="Comment"># Set cursor position</span>
<span class="Keyword">var</span> <span class="Identifier">curMark</span> <span class="Operator">=</span> <span class="Identifier">sourceView</span><span class="Operator">.</span><span class="Identifier">buffer</span><span class="Operator">.</span><span class="Identifier">getInsert</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Comment"># Gets the mark at the cursor.</span>
<span class="Identifier">sourceView</span><span class="Operator">.</span><span class="Identifier">scrollToMark</span><span class="Punctuation">(</span><span class="Identifier">curMark</span><span class="Punctuation">,</span> <span class="Operator">...</span><span class="Punctuation">)</span> <span class="Comment"># Scrolls the source view to that mark.</span>
</pre></p>
<p>The conclusion that I gathered from this piece of code is that the <code>scrollToMark</code> function is called before the <code>sourceView</code> has a chance to initialise fully. Hence the scrolling does not occur because the <code>sourceView</code> has not had a chance to calculate it's height and what not.</p>
<p>I figured that perhaps there is a signal which tells me that the GtkSourceView is ready to be scrolled. I could not find a signal which let me determine this though so I decided to ask people on GNOME's IRC network whether they were aware of such a signal. Even though some signals have been suggested to me I have found that neither of them worked. Thankfully a certain user suggested a different approach which worked perfectly.</p>
<h1>The solution</h1><p>The solution involves creating an idle proc (which is a procedure which GTK will call when it has nothing else to do until told otherwise by the return value of this procedure). In that idle proc the source view is scrolled constantly, until it is determined that the source view has in fact scrolled to the location desired. This was accomplished by using a couple of nice functions:</p>
<ul><li><code>gtk_get_iter_location</code> to get the location of the cursor.</li><li><code>gtk_get_visible_rect</code> to get the rectangle of the visible part of the document in the source view.</li><li><code>gtk_intersect</code> to check whether the cursor is in the visible rectangle.</li></ul><p>And here is a code sample for you copy-pasters out there:</p>
<p><pre class='code'><span class="Keyword">proc</span> <span class="Identifier">idleConfirmScroll</span><span class="Punctuation">(</span><span class="Identifier">sv</span><span class="Punctuation">:</span> <span class="Identifier">PSourceView</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">gboolean</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">cdecl</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">=</span>
<span class="Identifier">result</span> <span class="Operator">=</span> <span class="Identifier">false</span>
<span class="Keyword">var</span> <span class="Identifier">buff</span> <span class="Operator">=</span> <span class="Identifier">sv</span><span class="Operator">.</span><span class="Identifier">getBuffer</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
<span class="Keyword">var</span> <span class="Identifier">insertMark</span> <span class="Operator">=</span> <span class="Identifier">buff</span><span class="Operator">.</span><span class="Identifier">getInsert</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
<span class="Keyword">var</span> <span class="Identifier">insertIter</span><span class="Punctuation">:</span> <span class="Identifier">TTextIter</span>
<span class="Identifier">buff</span><span class="Operator">.</span><span class="Identifier">getIterAtMark</span><span class="Punctuation">(</span><span class="Keyword">addr</span><span class="Punctuation">(</span><span class="Identifier">insertIter</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Identifier">insertMark</span><span class="Punctuation">)</span>
<span class="Keyword">var</span> <span class="Identifier">insertLoc</span><span class="Punctuation">:</span> <span class="Identifier">TRectangle</span>
<span class="Identifier">sv</span><span class="Operator">.</span><span class="Identifier">getIterLocation</span><span class="Punctuation">(</span><span class="Keyword">addr</span><span class="Punctuation">(</span><span class="Identifier">insertIter</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Keyword">addr</span><span class="Punctuation">(</span><span class="Identifier">insertLoc</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
<span class="Keyword">var</span> <span class="Identifier">rect</span><span class="Punctuation">:</span> <span class="Identifier">TRectangle</span>
<span class="Identifier">sv</span><span class="Operator">.</span><span class="Identifier">getVisibleRect</span><span class="Punctuation">(</span><span class="Keyword">addr</span><span class="Punctuation">(</span><span class="Identifier">rect</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
<span class="Comment"># Now check whether insert iter is inside the visible rect.</span>
<span class="Comment"># Width has to be higher than 0 for the 'intersect' proc to work.</span>
<span class="Keyword">if</span> <span class="Identifier">insertLoc</span><span class="Operator">.</span><span class="Identifier">width</span> <span class="Operator"><=</span> <span class="DecNumber">0</span><span class="Punctuation">:</span> <span class="Identifier">insertLoc</span><span class="Operator">.</span><span class="Identifier">width</span> <span class="Operator">=</span> <span class="DecNumber">1</span>
<span class="Keyword">let</span> <span class="Identifier">inside</span> <span class="Operator">=</span> <span class="Identifier">intersect</span><span class="Punctuation">(</span><span class="Keyword">addr</span><span class="Punctuation">(</span><span class="Identifier">rect</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Keyword">addr</span><span class="Punctuation">(</span><span class="Identifier">insertLoc</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Keyword">nil</span><span class="Punctuation">)</span>
<span class="Keyword">if</span> <span class="Keyword">not</span> <span class="Identifier">inside</span><span class="Punctuation">:</span>
<span class="Identifier">sv</span><span class="Operator">.</span><span class="Identifier">scrollToMark</span><span class="Punctuation">(</span><span class="Identifier">insertMark</span><span class="Punctuation">,</span> <span class="FloatNumber">0.25</span><span class="Punctuation">,</span> <span class="Identifier">False</span><span class="Punctuation">,</span> <span class="FloatNumber">0.0</span><span class="Punctuation">,</span> <span class="FloatNumber">0.0</span><span class="Punctuation">)</span>
<span class="Keyword">return</span> <span class="Identifier">true</span> <span class="Comment"># Tell GTK to keep calling this idle proc.</span>
<span class="Keyword">discard</span> <span class="Identifier">gIdleAdd</span><span class="Punctuation">(</span><span class="Identifier">idleConfirmScroll</span><span class="Punctuation">,</span> <span class="Identifier">sourceview</span><span class="Punctuation">)</span>
</pre></p>
<p>The code should be pretty simple to understand and use. I hope it will help you as much as it helped me. </p>
A new blog with a new beginninghttp://picheta.me/articles/2013/08/a-new-blog-with-a-new-beginning.html2013-08-23T17:12:00Z2014-06-24T13:55:32ZDominik Picheta
<p>The beginning of this blog has been foretold for a long time now. There was always something that stopped me from doing it though.</p>
<p>First a little bit of history.</p>
<p>My first blog had its home on blogspot. Even though it was convenient it was not powerful enough for my needs. The formatting was annoying and there was no easy way to tell it to add syntax highlighting to a piece of code. This may have changed nowadays with Google's changes to blogspot but my distate for it is still present so I don't want to go back there.</p>
<p>My second attempt involved wordpress. By that time I was involved with Nimrod and wordpress had no syntax highlighting for Nimrod. There were other issues too i'm sure, which I cannot remember. Possibly the fact that it is written in PHP, feels like bloatware, requires MySQL and that I didn't feel like dabbling with its templates.</p>
<p>Which brings me to my most recent attempts. I noticed more and more that static blog generators have become very fashionable so I decided to try them out for myself. Having no Ruby experience I was reluctant to try Jekyll but it seemed easy to set up so I gave it a go. Sadly though, I was met with a lot of errors due to its dependency on Pygments which failed because I have Python 3 installed on my system. After many failed attempts I just decided that it was not for me.</p>
<p>I then decided to try Pelican with little hope of it working since it's completely written in Python, but it mentioned that the latest release supports Python 3 and seeing that I am more familiar with Python I decided to give it a try. Pelican felt like a true nightmare though, using makefiles to build the blog is horrendous so I gave up using it.</p>
<p>I also tried Hakyll but its long compilation time has majorly deterred me from it.</p>
<p>After almost 6 months I finally decided to just write a static blog generator in Nimrod and be done with it. I'm certainly glad I did. The blog is here, it works, and it looks great. <span style="font-style: italic;">Ipsum genera</span> (what I named this blog generator) is also extremely fast, and it didn't even take me longer than 3 days to write it, of course I can't accurately measure the time as I have been hacking the style of this site along the way. But the <a href="https://github.com/dom96/ipsumgenera/commits/master">commit history</a> speaks for itself. If you would like to use it you can, but the documentation is sparse, so use at own risk. It's licensed under MIT and available on <a href="https://github.com/dom96/ipsumgenera">github</a>.</p>
<hr /><p>I certainly hope that I will be able to sustain my motivation to write blog articles. The articles that I will write on this blog will be about a range of topics from programming to random things about my life. The articles will be tagged appropriately so you will be able to ignore the non-interesting things easily.</p>
<p>Thanks for reading and I hope you will stick around.</p>