import bash history to zsh history

Import bash history to zsh history. Gist Link

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#################################################################
# = This script transfers bash history to zsh history
# = Change bash and zsh history files, if you don't use defaults
#
# = Usage: ruby bash_to_zsh_history.rb
#
# = Author: Ankit Goyal
#################################################################

# change if you don't use default values
BASH_HISTORY_FILE_PATH="#{ENV['HOME']}/.bash_history"
ZSH_HISTORY_FILE_PATH="#{ENV['HOME']}/.zsh_history"

# Read the bash history file
bash_hist_file = File.read(BASH_HISTORY_FILE_PATH)

# Get the list of commands from bash history hile
command_list = bash_hist_file.split("\n")

# Open the zsh history file
zsh_hist_file = File.open(ZSH_HISTORY_FILE_PATH, "a")

# Get timestamp required for zsh history file format and update the history file
time = Time.now.to_i
command_list.each do |command|
  time += 1
  zsh_hist_file.write(": #{time}:0;#{command}\n")
end

# Close the file
zsh_hist_file.close

compile and run cpp program from vim

To compile and run cpp program from MVIM(or VIM) directly. Add this to your ~/.vimrc

1
2
nnoremap <C-c> :!g++ -o  %:r.out % -std=c++11<Enter>
nnoremap <C-x> :!./%:r.out

Note: You can remove/add the <Enter> if you prefer to press <Enter> yourself.

Now simply hitCtrl + c to compile and Ctrl + x to execute.

For a source file with name main.cpp, commands will be expanded to

1
2
g++ -o main.out main.cpp -std=c++11
./main.out

The %:r gets the current filename without the extension whereas % gets the current filename.

You can very easily extend above approach to compile using makefile.

learning concept checking the hard way - part 3

In the previous posts this and this, I talked about the basics of concept checking and stuff you need to know.

Finally here’s our has_less method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <vector>
#import <complex>

using namespace std;

template <typename T, typename unused = decltype(T() < T())>
std::true_type has_less_helper(const T&);

template <typename T>
std::false_type has_less_helper(...);

template <typename T>
constexpr bool has_less(void){
    using my_type = decltype(has_less_helper<T>(std::declval<T>()));
    return my_type::value;
}

template <typename T>
class BinarySearchTree{
public:
    static_assert(has_less<T>(), "Assertion failed. No less than operation defined.");
    BinarySearchTree() {
    }
};

int main(int argc, const char * argv[])
{
    BinarySearchTree<int> binarySearchTree;
    // compile time error here. since complex<int> doesn't have a '<' operator
    BinarySearchTree<complex<int>> compBinarySearchTree;
    return 0;
}

Let’s dissect the above method and see what’s happening.

Read on →

learning concept checking the hard way - part 2

In the previous post, I talked about importance/use case of concept checking. In this post we’ll step by step see how concept checking works.

Our final goal is to write a has_less method, which for any given type will return true or false based on whether < is defined for that type or not at the compile time

Let’s first look at some of the c++ concepts which we’ll use in the process.

1. decltype

(since c++11)

decltype let’s you extract type from any expression. It’s available in C++11

For example, In the example below, decltype can be used to get the type of variable myType1 and used to define another variable with the same type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//dec.cpp file
class MyType{
public:
    MyType(){std::cout << "Mytype: constructor called." << std::endl; }
};

int main(int argc, const char * argv[])
{
    MyType myType1;
    decltype(myType1) myType2;
    return 0;
}

>> ./a.out
Mytype: constructor called.
Mytype: constructor called.

Takeaway: we can use decltype to get type of a variable.

Read on →

learning concept checking the hard way - part 1

What is a concept in C++?

A concept is a set of requirements (valid expressions, associated types, semantic invariants, complexity guarantees, etc.) that a type must fulfill to be correctly used as arguments in a call to a generic algorithm

For example if you write your own generic implementation for BinarySearchTree. It should basically work for any type for which < operator is defined. As long as you have that condition satisfied you can build a binary search tree for that type. For instance, < is defined for int, double. However it’s not defined for complex numbers. C++ has no explicit mechanism for representing concepts.

Consider the following possible way you may write your generic Binary Search Tree.

Read on →

star in ruby

I have seen the Ruby’s * operator used at several places in different ways and often times I get confused the way it is used. Finally I decided to learn more about it and here are few interesting roles of * operator that I found in Ruby. It can be used to multiply, repeat, copy or as a splat operator.


Multiplication

This is the most obvious role of *. It’s the multiplication operator. 2 * 2 = 4

Or an instance method of Fixnum class to perform multiplication. 2.*(2) = 4

Read on →

elasticsearch basics - Analyzers

Elasticsearch is a powerful open source search engine build over Apache Lucene. You can do all kind of customized searches on huge amount of data by creating customized indexes. This post gives an overview of Analysis module of elasticsearch.

Analyzers basically helps you in analyzing your data.:o You need to analyze data while creating indexes and while searching. You could analyze your analyzers using Analyze Api provided by elasticsearch.

Creating indexes mainly involves three steps:

  • Pre-processing of raw text using char filters . This may be used to strip html tags, or you may define your custom mapping. (Couldn’t find a way to test this using analyse api. Please put it in comments if you know some way to test these through Analyze Api)

Example: You could use a char-filter of type html_strip to strip out html tags.

A text like this:

1
<p> Learn Something New Today! which is <b>always</b> fun </p>

would get converted to:

1
Learn Something New Today! which is always fun
Read on →

modularize_sinatra - modular sinatra app generator

Sinatra is simple, small and fast.

Sinatra is a DSL for quickly creating web applications in Ruby with minimal effort. – sinatra readme

The only downside is that it doesn’t offer you the typical “MVC” like Rails.

Lot of times Rails is an overkill for a simple application and Sinatra seems like a perfect choice. Since using Sinatra, you could write all your code in a single file, and at one time it becomes really hard to manage the code and you may feel the need of porting it to a framework like Rails. However, you don’t need all the features that comes with Rails. What you need here is some kind of modularization in your application. For this very purpose I created a gem called modularize_sinatra

modularize_sinatra creates a Rails like MVC structure without the overhead.

modularize_sinatra is available on rubygems. For installation and usage instructions please visit http://github.com/goyalankit/modularize_sinatra

Read on →

Initial commit

This is my first post. Through this blog, I hope to share technical, non-technical, all kind of stuff that I discover and/or create.