My Emacs Settings

“GNU Emacs is an extensible, customizable text editor—and more.” as they introduce it to the readers on the official site of GNU Emacs.

Actually it’s an old thing still in use today. But it’s no wonder so many people like it even now, since it’s flexible, configurable and extensible. It’s built around some powerful core ideas about buffers, editing and so, and provides a LISP dialect the Emacs Lisp to it’s users to extend and configure it’s features. It’s worth to mention, that the modern versions of GNU Emacs comes with a package system, and there are package repositories containing tons of useful packages to choose form.

Side note. The fact Emacs Lisp is a dynamically scoped language might surprise you, since almost all modern programming languages are statically scoped today. Before you would throw a rock on it, you would like to consider that there are cases when it can come in handy. Eg. You can make other parts of the code behave temporary different, without the need to pass arguments around all the way down in the call stack. The dynamic binding is the default in Emacs, but you may opt for lexical binding if needed on a per file basis.

;; -*- lexical-binding: t -*-

(defun make-adder (x)
  (lambda (y) (+ x y)))

To me it’s a versatile platform for doing many text related operations. Starting from source code editing, through project- and file management, to actually testing REST APIs interactively in a minimalist and easy to use style.

There are many awesome repositories on GitHub hosting Emacs settings and configuration for individuals. There were some attempts to provide a minimalistic, standard common ground with enhancements compared to the bare setup a freshly installed Emacs comes with, that fits everyone. Actually it’s a difficult task to do, since what fits all? Probably the most well known example is Technomancy’s Better Defaults for Emacs.

Based on these influencing examples by Technomancy, Magnars and Bodil, I assembled my minimalistic version that fits me well.

I find hosting my Emacs settings on GitHub to be a good idea, because when I move to a new machine, I only need to install Emacs and check out the repo and start Emacs up. And that’s all. All the packages I need are automatically downloaded and installed during the first start.

For my personal Emacs setup, I didn’t like the idea to depend on external, non-standard libraries to be able to install external and non-standard packages. One reason is that these libraries can be installed and updated through the package system and I didn’t like the git submodule binding for this.

Therefore I choose Bodil’s approach to use exclusively pure core Emacs Lisp code to perform this “bootstrapping”.

;; Install packages if not present
(dolist (pkg '(flx
	       flx-ido
	       flycheck
	       markdown-mode
	       paredit
	       projectile
	       restclient))
  (install-package-if-not-installed pkg))

I must also mention that on a Mac, it’s a bit tricky to setup properly to have the user environment settings from the shell right. But don’t worry. It’s not a big deal really. There is a package to the rescue.

;; Are we on a mac?
(defvar is-mac (equal system-type 'darwin)
  "Boolean indicating if we are on a mac")

;; Setup environment variables from the user's shell.
(when is-mac
  (install-package-if-not-installed 'exec-path-from-shell)
  (exec-path-from-shell-initialize))

I like the idea to have the customisation in a separate file. Therefore I put the following code in my init.el file.

;; Keep emacs Custom-settings in separate file
(setq custom-file (expand-file-name "custom.el" user-emacs-directory))
(load custom-file)

Then it’s in a separate file, the custom.el.

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(js-indent-level 2)
 '(projectile-find-dir-includes-top-level t)
 '(projectile-switch-project-action (quote projectile-find-dir))
 '(scheme-program-name "guile"))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )

Are you considering using GNU Emacs?

Pros

  • Powerful and productive environment for text related tasks
  • No need to move your hand between the keyboard and the mouse all the time
  • Many packages for tons of tasks
  • You can modify, bend, and extend it to serve your needs best

Cons

  • Hard to learn all the funny keystrokes
  • It can be hard to learn LISP
  • Steep learning curve

GNU Emacs is an extensible text editor and can be customised to meet individual needs.

To see the the complete code to my Emacs settings, visit my GithHub repo!

My Emacs Settings

Combining ExtJS with Clojure Compojure

Back in 2013, I was working on an R&D project to create a proof of concept on combining the Sencha ExtJS frontend with a Clojure Compojure backend, having a RESTful API in-between.

With this loosely-coupled architecture and way more productive server-side development approach -using Clojure instead of Java- I could demonstrate an alternative to the previous, heavy-weight J2EE backend-based old-school application.

I did have an easy job working on this, since Michael Jakl already posted an example using Compojure for REST API with an in-memory RDBMS.

Also Sencha provided an example on using their REST proxy.

All the remaining work was to blend the parts and to write a unit test for the server side.

The server side code looks like this:

(ns users.handler
  (:import com.mchange.v2.c3p0.ComboPooledDataSource)
  (:use compojure.core)
  (:use ring.util.response)
  (:require [compojure.handler :as handler]
            [compojure.route :as route]
            [ring.util.response :as response]
            [ring.middleware.json :as middleware]
            [clojure.java.jdbc :as sql]))

(def db-config
  {:classname "org.h2.Driver"
   :subprotocol "h2"
   :subname "mem:users"
   :user ""
   :password ""})

(defn pool [config]
  (let [cpds (doto (ComboPooledDataSource.)
               (.setDriverClass (:classname config))
               (.setJdbcUrl (str "jdbc:" (:subprotocol config) ":" (:subname config)))
               (.setUser (:user config))
               (.setPassword (:password config))
               (.setMaxPoolSize 1)
               (.setMinPoolSize 1))]
    {:datasource cpds}))

(def pooled-db (delay (pool db-config)))

(defn db-connection [] @pooled-db)

(sql/with-connection (db-connection)
                                        ;(sql/drop-table :users)
  (sql/create-table :users
                    [:id :int "PRIMARY KEY AUTO_INCREMENT"]
                    [:email "varchar(128)"]
                    [:first "varchar(64)"]
                    [:last "varchar(64)"]))

(defn wrap-ext-store
  "Put the data and message into a map used by the ExtJS REST Store/Proxy"
  [data message]
  {:success true :message message :data data})

(defn get-all-users []
  (response
   (sql/with-connection (db-connection)
     (sql/with-query-results results
       ["select * from users"]
       (-> (into [] results) (wrap-ext-store "Loaded data"))))))

(defn raw-get-user [id]
  (sql/with-connection (db-connection)
    (sql/with-query-results results
      ["select * from users where id = ?" id]
      (cond (empty? results) {:status 404}
            :else (first results)))))

(defn get-user [id]
  (response (-> raw-get-user(id) (wrap-ext-store (str "Feched User " id)))))

(defn create-new-user [user]
  (response
   (sql/with-connection (db-connection)
     (let [usr (dissoc user :id)
           id (first (vals (sql/insert-record :users usr)))]
       (-> (raw-get-user id) (wrap-ext-store "Created new User"))))))

(defn update-user [id user]
  (sql/with-connection (db-connection)
    (let [usr (assoc user "id" id)]
      (sql/update-values :users ["id = ?" id] usr)))
  (response (-> (raw-get-user id) (wrap-ext-store (str "Updated User " id)))))

(defn delete-user [id]
  (sql/with-connection (db-connection)
    (sql/delete-rows :users ["id = ?" id]))
  (response (-> [] (wrap-ext-store (str "Destroyed User " id)))))

(defroutes app-routes
  (GET "/" [] (response/resource-response "index.html" {:root "public"}))
  (context "/users" [] (defroutes users-routes
                         (GET "/" [] (get-all-users))
                         (POST "/" {body :body} (create-new-user body))
                         (context "/:id" [id] (defroutes users-routes
                                                (GET "/" [] (get-user id))
                                                (PUT "/" {body :body} (update-user id body))
                                                (DELETE "/" [] (delete-user id))))))
  (route/resources "/")
  (route/not-found "<h1>Not Found</h1>"))

(def app
  (-> (handler/site app-routes)
      (middleware/wrap-json-body)
      (middleware/wrap-json-response)))

And the whole project can be found on GitHub.

 

Combining ExtJS with Clojure Compojure

Week number vs. Erlang/OTP

Some years ago I had to find out what is the current week number for some reason.

Of course there are common tools like Office/Calendar applications and also the Web for your aid. You can look it up easily. Also there are support for it in many programming language libraries.

However, that time I was working on some Erlang research and found that it’s standard library did lack support for determining week numbers. Then I decided to add it since it did sound like a fun little project and a nice experience in Open Source project contribution.

So I began by researching how to determine week numbers. I found that there are different kind of “week numbers” used by different parts of the World. I found that it’s the ISO Week Number I was looking for.

Then I read the well made contribution description/guideline by Ericsson and based on that I made my contribution to Erlang/OTP with the source, documentation and tests.

The contribution was accepted and is part of the Erlang/OTP now. It serves the needs of people developing Erlang applications and wanting to use the Week Number for some reason.

The code in Erlang looks like this:

%%
%% Calculates the iso week number for the current date.
%%
-spec iso_week_number() -> yearweeknum().
iso_week_number() ->
    {Date, _} = local_time(),
    iso_week_number(Date).


%%
%% Calculates the iso week number for the given date.
%%
-spec iso_week_number(Date) -> yearweeknum() when
      Date :: date().
iso_week_number({Year, Month, Day}) ->
    D = date_to_gregorian_days({Year, Month, Day}),
    W01_1_Year = gregorian_days_of_iso_w01_1(Year),
    W01_1_NextYear = gregorian_days_of_iso_w01_1(Year + 1),
    if W01_1_Year =< D andalso D < W01_1_NextYear ->
	    % Current Year Week 01..52(,53)
	    {Year, (D - W01_1_Year) div 7 + 1};
	D < W01_1_Year ->
	    % Previous Year 52 or 53
	    PWN = case day_of_the_week(Year - 1, 1, 1) of
		4 -> 53;
		_ -> case day_of_the_week(Year - 1, 12, 31) of
			4 -> 53;
			_ -> 52
		     end
		end,
	    {Year - 1, PWN};
	W01_1_NextYear =< D ->
	    % Next Year, Week 01
	    {Year + 1, 1}
    end.

References

Week number vs. Erlang/OTP

My Ruby configuration for tinkering

I don’t want the Gem documentation to be built locally on my system, I refer to the online documentation instead.

Setting this up is actually easy.

Put this in a file called .gemrc in your home directory:

gem: --no-document

I like to use the REPL in Ruby (IRB) to tinker with it, and enjoy the dynamic properties of the language.

Unfortunately out of the box IRB lacks such extremely useful tools like code completion, pretty print and automatic indentation.

The interesting thing is that, IRB can be equipped with such tools right out of the box, but it’s not equipped with any by default. Maybe that’s because they didn’t wanted to enforce one particular setting over others, and wanted to leave it up to the user which one to use.

To automate the setup for each and every IRB session, put the following code in a file called .irbrc in your home directory:

require 'irb/completion'
require 'pp'
IRB.conf[:AUTO_INDENT]=true

Let me mention an alternative to IRB with lots of useful tools like the above and more. Pry – An IRB alternative and runtime developer console

My Ruby configuration for tinkering

Ruby is fun!

It’s so easy and natural to solve problems in Ruby!

Consider the problem of finding and printing the second prime number found in the interval 10,000 – 1,000,000.

Side note. One approach can be the usual imperative solution, where the different kinds of computations like enumerating the interval, checking for primality and collecting the solution are interleaving resulting in an effective BUT cryptic solution. It’s much harder to understand -and possibly modify later- the different parts if they are mixed together to form one big monolithic chunk of code. On the other hand one might want to express this idea using the sequence paradigm, where each phase of the computation is done by a function box, these boxes are linked to form a chain, which in turn represents the program. Though this latter approach is very expressive, easy to understand and maintain, BUT it can be very ineffective. Eg. When enumerating big amounts of data, no transition to the next function box is possible until all the data is computed. However there is a clever way to have the best from both worlds. It’s the stream paradigm as shown in the SICP. With this we gain the clarity and elegant style of programming from the sequence paradigm, and the effective computation from the imperative style at the same time.

Fortunately Ruby has lazy enumerators which can support the same idea. In Ruby using lazy enumerators it might look something like:

require 'prime'

primes_in_interval = (10_000..1_000_000).lazy.select(&:prime?)

p primes_in_interval.first(2)[1]

Without the .lazy method call it would filter the whole interval for primes and only when all the primes are filtered and collected into an array, could the printing of the second element be done. It’s a terrible waste. BUT calling the .lazy method on the interval -which returns a lazy Enumerator object- makes a huge difference, since now NO primes are searched and collected, UNTIL they are really needed. With this, only the first two primes are computed, which is a huge gain.

References

Ruby is fun!

Recursive anonymous functions?

Introduction

The Lambda Calculus, invented by Alonzo Church back in the 1930s, can be referred to as the smallest universal programming language. It’s Turing complete, meaning everything which is computable, can be computed in it.

It has only a few basic constructs, like – anonymous – functions and function application. These are the primitives. By adding Church Numerals, representation of Booleans, conditionals, lists and recursion with the help of combinators, we get a fully usable language.

Functional programming was born to Lambda Calculus. Functional programming is getting more and more attention nowadays, because it makes parallel programming so much easier.

Recursion

Recursion is amazing, since it enables us to compute and process self-similar structures in a natural way. It also helps to solve a more complex problem by breaking it down into similar, but simpler problems to be solved separately. Finally recursion can be thought of an alternative to iteration.

Side note. The conventional programming languages are equipped with looping-constructs like, while, for, etc. The interesting thing is that it turns out these are unnecessary, since the same can be accomplished by using functions and function-application. By adding Tail Call Optimisation (TCO for short) and having the underlying system translate them to primitive loops in the machine language, we can achieve the same performance as we were using the usual looping-constructs to express iteration.

To get recursion working, we need to have a “handle” on the function itself. But functions in Lambda Calculus are anonymous. (Meaning, we cannot really refer to them by their names.) So how can we make it work then? Well, we can bind expressions to function parameters. That’s all what’s offered by Lambda Calculus. But as you will see shortly, it’s sufficient to make it work. U and Y combinators to the rescue!

The U combinator is a higher-order function, that applies its parameter to its parameter. It provides a way to get a handle on the function through parameter binding, hence making recursion possible.

Side note. The smallest non-terminating program can be written using the U combinator like this: U(U)

The Y combinator is a bit more complicated compared to the U combinator, and it’s better-known too.

The funny thing is that today’s programming languages, which are functional, or multi-paradigm but supporting the functional paradigm as well, are capable of this kind of programming. That’s because they are descendants of the Lambda Calculus.

Next let’s see how to do recursion with anonymous functions in some mainstream programming languages. And, also in Scheme for those who are not afraid of this beautiful, minimalistic LISP dialect.

Code Examples

Side note. While in Lambda Calculus we bind the name (parameter of the function) to the expression to which the function is applied, they’re bound to the values of the expressions in the following examples, since they use applicative order evaluation. That’s why it’s better to call these combinators applicative order combinators.

function (f) { return f(f); }
(function (f) {
  return f(f);
}(function (h) {
  return function(n) {
    return n <= 1 ? 1 : n * h(h)(n - 1);
};
}))(5);
 -> f { f.call(f) }
-> f { f.call(f) }.call(
  lambda do |h|
    lambda do |n|
      n <= 1 ? 1 : n * h.call(h).call(n - 1)
    end
  end
).call(5)
(lambda (f) (f f))
(((lambda (f) (f f))
  (lambda (h)
    (lambda (n)
      (if (<= n 1)
        1
        (* n ((h h) (- n 1)))))))
  5)

 

References

  1. Lambda Calculus
  2. Anonymous function
  3. Church Encoding, U combinator
  4. Y Combinator
Recursive anonymous functions?

Building my joule thief LED flashlight

Have you ever thought of hand-making your own little flashlight? Actually it’s easy! Well, you need only a few inexpensive and common components to build it.

What’s this?

To put it simple, a “Joule Thief” is an electrical circuit which makes it possible to light an LED with a lower voltage than it would normally need to function.

Use case

I’d like to make a small flashlight which requires only an AA battery (1.5 Volts) to operate.

How to make it?

You will need to wind a ferrite toroid with two wires. It is highly recommended to use two different color insulated wires because you will need to distinguish between them when soldering the parts together. The funny is that you will end up with four leads when done with the winding, but you will need three leads to make the connection to the other parts of the circuit. The trick is to choose two different color wires coming from the opposite side and solder them together. That’s it. Now you have only three leads.

Connect this “double” lead from the inductor to the switch, which is connected to the positive side of the battery in turn.

One of the “single” leads from the toroid should be connected to the base of the transistor through a 1 K resistor. The other goes to the collector.

You need to connect the emitter lead of the transistor to the negative side of the battery to close the circuit.

Components
  • White LED
  • TACT micro switch
  • 1 K resistor
  • 2N3904 transistor (NPN)
  • Ferrite toroid core (14.2X5X9)
  • Wires
  • 1 AA battery holder
  • 50x100mm Stripboard (prototyping PCB)
  • Rubber bands
  • AA battery

Of course you’ll need a soldering iron, solder and a helping hand with a magnifying glass attached too.

The finished flashlight

JouleThief_front JouleThief_side

great Joule Thief maker posts

Read more about the Joule Thief on Wikipedia

Building my joule thief LED flashlight

About archiving my SICP related stuff on GitHub

Well, I have done some exercises offered by the books Simply Scheme and the SICP for my own personal development and joy.

One interesting example is the representation of pairs as procedures. Instead of using a data structure we can use ordinary procedures to achieve the same, with identical semantics.

;;; Procedural representation of pairs
(define (cons x y)
  (define (dispatch m)
    (cond ((= m 0) x)
	  ((= m 1) y)
	  (else (error "Only 0 or 1 is allowed as argument -- CONS" m))))
  dispatch)

(define (car z)
  (z 0))

(define (cdr z)
  (z 1))

Another example

;;; Procedural representation of pairs
(define (cons x y)
  (lambda (m) (m x y)))

(define (car z)
  (z (lambda (p q) p)))

(define (cdr z)
  (z (lambda (p q) q)))

Or another example introducing mutation on top of that

(define (cons x y)
  (define (set-x! v) (set! x v))
  (define (set-y! v) (set! y v))
  (define (dispatch m)
    (cond ((eq? m 'car) x)
	  ((eq? m 'cdr) y)
	  ((eq? m 'set-car!) set-x!)
	  ((eq? m 'set-cdr!) set-y!)
	  (else (error "Undefined operation -- CONS" m))))
  dispatch)

(define (car z) (z 'car))

(define (cdr z) (z 'cdr))

(define (set-car! z new-value)
  ((z 'set-car!) new-value)
  z)

(define (set-cdr! z new-value)
  ((z 'set-cdr!) new-value)
  z)

To see more, please take a look at: SICP and Simply Scheme.

Related stuff:

About archiving my SICP related stuff on GitHub

Programming an AVR MCU in JavaScript

arduinoyun

I have launched a project exploring some aspects of moving the development of an AVR MCU from the AVR/C++ world to the MIPS Linux/JavaScript world.

Code example:

// PWM glowing example.
// Copyright (C) 2015 Imre Horvath
//
// Build a circuit using a breadboard like this:
// Arduino Yun digital pin 11 (PWM) --> 270 R 5% --> 5 mm LED --> Arduino Yun GND
//
// This code below will cause the LED to glow first brighter and brighter gradually,
// then it will glow dimmer and dimmer till off.
// And then, this will repeat forever.

var firmata = require('firmata');
var board = new firmata.Board('/dev/ttyATH0', function (err) {

  var pwmPin = 11;
  var value = 0;
  var up = true;
  var incr = 10;

  if (err) {
    console.log(err);
    return;
  }

  function cleanExit() {
    board.reset();
    process.exit();
  }

  process.on('SIGTERM', cleanExit);
  process.on('SIGINT', cleanExit);

  board.pinMode(pwmPin, board.MODES.PWM);
  
  setInterval(function () {

    board.analogWrite(pwmPin, value);

    if (up) {
      if (value < 250) {
        value += incr;
      } else {
        up = false;
        value -= incr;
      }
    } else {
      if (value > 0) {
        value -= incr;
      } else {
        up = true;
        value += incr;
      }
    }
  }, 125);

});

Actually all the parts to assemble a system for experimenting are available.

  1. They have created the Arduino Yun, which is an ATMega AVR MCU connected to a Atheros MIPS Linux SOC with a serial line on board.
  2. There is the Arduino software to program the MCU
  3. There is the StandardFirmata sketch and the modification of it to enable it’s use within the Yun
  4. There is the Node.js and the serialport module with native extension provided through the Yun package repository
  5. You can configure BusyBox to start your node app at the end of the boot process, hence having a similar behaviour as with a native MCU, but with slower startup time
  6. You have SCP to transfer your app from your development machine to the Yun
  7. You can create a simple helper script to stop the old app on the Yun, transfer the new app and finally start it again.

It is an interesting idea to program an MCU in JavaScript, because of the language’s powerful features like closures, first-class function objects and higher-order functions. These features enable enormous expressive power compared to plain C/C++.

However transferring control from the AVR to the Embedded Linux on-board through a serial line (57600 baud) and the Firmata protocol on top of it, might render this idea useless for time critical real-time applications.

Also a lot of low-level (C/C++) libraries are available for the Arduino platform like the Arduino-IRremote which are unavailable and unusable on the Node.js side. While other low-level libraries like the one for controlling a WiFi shield are rendered unnecessary when using a Yun with the Linux side which already has WiFI capabilities.

Check out my GitHub repo on the topic!

Programming an AVR MCU in JavaScript