Monday, March 31, 2014

How to zip in Clojure

In programming, there's time when we have 2 arrays and we want values at the same index of both arrays to have operation on each other producing an array of result. In imperative style, we have to setup a temporary counter variable to store index the current operation is on, increment it until reaching the last value of an array. For example, we want to sum values of each index.

In functional style, there's a function called 'zip' (Haskell, Ruby) which allows us to pair up values of each index preparing to apply operation on each pair later.

In Haskell, there's even 'zipWith' which apply operation on the pair immediately instead of producing an intermediate array.

But when it comes to Clojure, I wasn't be able to find a function in its standard library with the same behavior. The closest I could find is 'zipmap' that returns back a map which is not exactly what I want. We definitely can work around a little bit to get a vector.

But as you can see, it doesn't preserve the order of an original vectors.

The next idea I have is to use 'interleave' and 'partition'

It's kinda work, but the fact that we have to call 2 functions is not so satisfied.

I went look up and found this Stack Overflow answer. Yes. Just use simple 'map'!

I think, I overlooked this solution because in Ruby, the language the I'm most comfortable with, 'map' can only operate on only one array.

Learning that Clojure's map can takes any number of collections is an aha moment for me. So now I can sum elements of more than 2 vectors easily.

Since the second argument can accept any function, creating Clojure records is as easy as this.

Wednesday, March 5, 2014

Speed up command line file download with Axel

Axel is a command line file downloading tool that can create multiple connection to download parts of the file and combine them later after the download finish. It's perfect replacement for cURL or Wget in downloading a big file.

To use it, as simple as;

axel -n 10 http://url/big-file
10 is number of connections

It's available through Homebrew, agt-get and other OS package managers, I suppose.

Using this tool always reminds me to FlashGet and Download Accelerator which I had used more than 10 years ago.

Thanks @kamal who introduced it to me.

Thursday, February 27, 2014

[Git] Prune when fetch/pull

When a branch is removed from your git central server, remote branch on your machine won't get removed automatically. You might already know popular command to clean those removed branches, which is
git remote prune origin
But also you could give --prune or -p argument to fetch or pull to do the pruning for you after it's done with its normal behavior. For example,
git fetch -p
git pull -p
If you are in the habit of using remote prune that's fine. These prune arguments just save you one extra step if you just put them in to help cleanup once in a while.

Tuesday, January 28, 2014

Rails4's Active Record where not nil

Since Rails 4.0 no more .where('column_name IS NOT NULL'). Instead, we now can use
.where.not(column_name: nil)

Tuesday, January 21, 2014

start-stop-daemon: help for start, stop, create pidfile for your command that doesn't have pidfile

You have a command to start a long running process but it doesn't create a pidfile for you to write a script to start/stop it. start-stop-daemon could help that.

This is a document of the command.


start-stop-daemon -d dir_want_to_run_on -b -m -p --start --startas command
start-stop-daemon -p --stop

-b : background
-m : make file (need to use with -p)
-p : pid file name (need to use with -m)

Monday, January 20, 2014

Light Table: Stop the execution

Use `Eval: Cancel evaluation for the current client` or `Editor: Disconnect clients attached to editor` from the command bar

credit: How to interrupt eval?

Monday, January 6, 2014

Solved a different cause of SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)

You get SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError) or similar error when trying to request to a https endpoint but no any other people solutions you have found through googling seems to be able to fix it.

What I have found is that those blog posts and stackoverflow answers led me to the wrong direction. The problem is neither Ruby's false nor you misconfigured it. Instead, missing intermediate CA certificate is the cause of the problem.

To verify that you are having the same cause of problem that I had, try running these command and compare the results
$ ruby -rnet/http -e "puts Net::HTTP.get(URI(''))"
$ ruby -rnet/http -e "puts Net::HTTP.get(URI('https://your_webserver'))"
If you get back proper result for the first command and get the same error for the second command. That's the same problem I had. If you get errors on both commands, this post won't help solving you problem.

What's happening is that Ruby doesn't have a certificate of the intermediate CA that is used to issue certificate of the server you are requesting to

Compare Certificate chain section of the result of these 2 commands
$ openssl s_client -connect
$ openssl s_client -connect
In github's server case, you will get 0 and 1. On the other hand, in your web server case, you probably get only 0. SSL certificate chains section of this nginx documentation page has a good explanation of the chain.

If you own that web server or has an access to it, go fix that and your ssl issue should be resolved. For nginx web server, the fix is described in the previous link.

Good luck.