Prasenjit Kumar Nag a.k.a Joy

A Developers Adventure in Coding

Functional Programming : My Intro to Higher Order Functions

In University time, I was mainly acquainted to C/CPP programming with some Java. Then to me function was something special and very different than other inhabitants of the my then programming world. In that time I couldn’t even think of passing/returning functions to other functions.

Then sometime at the end of 2009, while I was learning Javascript I saw different behaviors and JS seemed more liberal to me. Like I could declare functions without name, could also assign them to variables, It was like wow to me. I read about anonymous functions and IIFE( Immediately-Invoked Function Expression ) and they amazed me back then. I first learned from Hasin Hayder while working in Leevio that I can assign name of a function to a variable and call them like any other function and could even play with their scope with apply and call Prototype methods.

A Bite of Functional Programming

I love programming languages and trying a new one is always fun. A year or so ago I first heard the name of functional paradigm of programming. I have came across blog posts, saw people talking about functional programming in programming communities over the last year. I have seen countless veteran programmers referring SICP as a must read for anyone who wants to be really good in programming. When I went over to search for SICP found the videos of original lecture of Hal Abelson and I heard the name of LISP from him while checking the first lecture.

From then on I have been cherishing a desire to learn a functional language and went ahead to try learning some like Haskell, Clojure and Scala though couldn’t get too far with anyone of them. But after every few months something inside me pushes me to get back to learning a functional language well and I spend several days reading blogs, trying out simple things.

Moving Magento Store From One Server to Another Easily Using Ssh

When I first started working with Magento I thought moving a magento installation with lots of data from one server to another wont be easy considering the db size is very big and the codebase is not small either.

I was a noob at linux server’s stuff then, I am not good at all now, but I have become better than before. And after moving several Magento installations around, creating development sandboxes from live data with large databases like 4GB+ I do think with some terminal kungfu you can pretty easily move magento from one server to another. The method I will show will required ssh access to both servers.

Looking for Occurrences of a String in All Files Under a Directory Using Grep

grep is a very simple command available on linux and OSX terminals which can do amazing things and can save a lot fo time. I was working on a Magento site this morning and on the checkout page I found the telephone field was using <input type="Number" ... while it should use <input type="text" ... or else phone number with dashes in them wont validate and customers wont be able to proceed with next step.

I was thinking of how can I find the file which have this code, those who of you are familiar with Magento knows that magento has a very organized file structure and hierarchies of templates, and different templates gets loaded based on active theme and layout xml files. So I have to find out all the occurrences of <input type="number" ... in the whole installation.

I knew I can use grep to do this. I instantly looked at the man page of grep and reviewed the available options and got what I needed.

The final command I wrote on the server’s terminal was

Grep
1
grep -R -i -n 'type="number"' public_html/app

-R tells grep to search recursively into that directory, -i is for ignore-case and -n finds and outputs the line number along with file name too. As soon as I ran that command it searched the whole app directory and came back with all the place where type="number" occurred with line number too. Then I just edited those files and the issue was fixed.

What took me several minutes would have taken hours to search and find those files manually. Thats one of many benefits of using command line. And you can do pretty amazing things using/combining only a handful of commands.

Taking a Magento Site Into Maintenance Mode for Everyone Except You

Magento takes a dead simple approach for activating the maintanence mode for any site using it. You just get into servers’s terminal via ssh , cd into the magento root folder and run the following command touch maintenance.flag or just crate a file named maintenance.flag in the root of the your magento installation and voila, you will see the maintenance mode message when you visit your store now.

Now Its locked for everyone, but say you are updating somethings on the site and you would like to check how thats affecting the look of the store but you dont want anyone to view it before getting done. Fortunately thas easy too.

How to Highlight the Search Term in Results in an Android App

Few months ago I created an Android app Al-Quran Bangla Mormobani in several days with a quick deadline, so couldn’t add much features to it. One such feature was the search terms were not highlighted in the results view so users couldn’t use it effectively for reference. I got several requests from the users asking for this features. Few days ago I had couple of hours free to look into it and after searching for a while found a solution.

Creating a Very Simple Artisan Startover Command in Laravel

While working on a recent project using Laravel I felt the need for a quick startover command, as we were brainstorming with domain objects and businees logic our migrations were changing frequently. So after each pull/change in migration I needed to reset the migrations, remigrate, seed database with updated seeder classes.

So I looked into creating a simple artisan command. The command is fairly straight forward, I am including the code for the command below.

Revisting Data Structure : Linked List Part 2

In previous part of the series We talked about Linked List in General and it’s implementation details in C++ Programming language. We saw how to layout the structure of a Linked List and how to insert data into the list.

In this episode we will be talking about how we can print all the nodes of a linked list. So we can assume from the nature of Linked list that we have to traverse the whole list and print each nodes value.

Printing Linked List through Iteration Method

The definition of the print function using iteration is as follows:

printList function
1
2
3
4
5
6
7
8
9
 void printList(struct Node* head){
    struct Node* temp = head;
    printf("\nList is: ");
    while(temp != NULL) { // exit condition
        printf(" %d", temp->data); // Print the value of the Current Node
        temp = temp->next;        // Proceed to the next Node
    }
    printf("\n");
 }

Revisting Data Structure : Linked List Part 1

Linked List is a very important data structure in terms of the concepts required to understand it’s inner working and the operation that can be performed on it.

In this post we will be exploring structure of Linked List and operations that can be performed on Linked list.

So without further ado lets dive into the details.

The Unix Tar Pipe : Lots of Unix Knowledge in a Simple One Liner

I have recently been introduced to the Tar Pipe. It’s not a command to run in the shell but a pattern which I liked very much.

It uses some primitive unix command and couple features of the shell to solve a problem. Let me talk a bit about the problem it solves. Suppose you have a directory called src and you want to clone it to a destination folder named dst preserving metadata like owners, groups and permissions of files and directories. In modern systems there are command like

Copy Command
1
2
3
4
5
cp -rp

# or

rsync -a

using which you can clone contents of one directory to another preserving permissions and other metadata easily. But in the days when rsync -a was not there or the cp implementation didn’t have the permissions preserving flag there was a tar pipe which did the same thing using unix tar command. The Tar Pipe looks like this

Tar Pipe
1
 (cd src && tar -c .) | (cd dst && tar -xp)