Sep 30, 2010

Clojure supports java based exceptions. There are two types of exceptions in clojure. One is clojure exceptions and the other is user defined exceptions. If you type as;
user=>(/ 1 0)
This is devide 1 by 0. So this will give an exception.
So, this is the way to catch the exceptions.
user=>(try (/ 1 0 ) (catch Exception e (prn "Cach the exception" ))(finally (prn "In finally...")))

Now it will handle the exception. But if you use a wrong type of exception it will not catch that and in the end of the try statment it will give the error.

Writing a simple clojure program.
Now we will try to write a clojure program to add remove and change things.

(alias 'set 'clojure.set)
This wiil make an alias to clojure.set. Now we can use "set" word when ever we need to write clojure.set/smoething.

(defstruct employee :name :id :role)
This will create a structore named employee.
(def employee-records (ref #{}))
The "ref" is to allow mutation of a storage with transactions.

(defn- delete-by-name [n recs]
(set/select #(not (= (:name %) n)) recs))
Both of these functions update and delete will do the operation and create a
new set of data, becouse these sequences are immutable.
Now you can see some public functions which will not cantaining a "-" sign when definding it.

(defn update-employee-role [n r]
"update the role for employee named n to the new role r"
(dosync
(ref-set employee-records (update-role n r @employee-records))))

(defn add-employee [e]
"add new employee e to employee-records"
(dosync (commute employee-records conj e)))
Now the code is completed. Save this as test.clj and in the clojure prompt you
can import that coding by,

(load-file "test.clj")
Then you can do add delete and change employee details. Here is some
coding to add some data initially. Then after that you can view what you enered.(add-employee (struct employee "Jack" 0 :Engineer))
(add-employee (struct employee "Jill" 1 :Finance))
(add-employee (struct-map employee :name "Hill" :id 2 :role :Stand))
@employee-records

Sep 29, 2010

We can recognise one or more spaces by importing monads librerys. We can try by
this coding.
readExpr input = case parse (spaces >> symbol) "lisp" input of
Left err -> "No match: " ++ show err
Right val -> "Found value"

Return values
We can convert return valuer of some inputs. To do this we need to defined a data type which can store any data type. So for do that;

Sep 28, 2010

Monad transformers
Monads transformers are also in a formata of monads. So we must make MaybeT m an instance of the Monad class.
instance Monad m => Monad (MaybeT m) where
return = MaybeT . return . Just

This will get the monads return and send it ot the monadts constructore. This thing can be wriiten as,
x >>=f= MaybeT $ do maybe_value return Nothing
Just value -> runMaybeT $ f value

List transformer
newtype ListT m a = ListT { runListT :: m [a] }

We can transform list also and to do that we need to crerate a datatype with a constructoore which takes an argument.

LiftM
This is a library which can transfer the non-monadic functions to monadic ones. The function is like this.
liftM :: Monad m => (a1 -> r) -> m a1 -> m r

Pasing monad
This is more powerful parser with monads like Maybe, list and sate. This will use parcer librery functions also. So first of all we must import relevent librery functions to our program.
import System
import Text.ParserCombinators.Parsec hiding (spaces)
Now we are going to use a parser librery function call oneOf
symbol :: Parser Char
symbol = oneOf "!$%&|*+-/:<=>?@^_~"

This will keep track of each charactor. Then we must make some arrangements to handle the errors too.
readExpr :: String -> String
readExpr input = case parse symbol "lisp" input of
Left err -> "No match: " ++ show err
Right val -> "Found value"

Now we can write the program.
main :: IO ()
main = do args
putStrLn (readExpr (args !! 0))

Run clojure.jar by,
java -jar clojure.jar
and it will prompt,
user=>
Clojure got three main numerical types. Integr, Float and Ratio.
To do 1 + 2 it is; (+ 1 2)
If you need to defind a function you can defid as,
The function you need to defind is a = 2 + 3
So you can write it as,
user=> (def a(+ 2 3))
Then it will give an output as
#'user/a
and prompt user=>
If you type "a" in that you will get the output of 2+3 as 5.

Data Structures
Creating a stucture;
(defstruct employee :name :id)
Using the structures
(struct employee "Mr. X" 10)
We can make functions from these things and get values from them like;
(def e1(struct employee "Nimal" 324))
And if you type e1 it will give an output like
{:name "Nimal", :id 324}
Also you can get only a value you need by typing like;
(e1 :name)
This will print the name of e1.

We can use accessor function to get easy access of feilds. It is like this.
(def emp-name(accessor employee :name))
Now if you want to get the employee name of e1 you cann type as;
(emp-name e1)

Add or remove new fields to structures
We can only remove fields which are added by assoc, can't delete the fields which are defined by defstruct.
Adding
(def e1-new (assoc e1 :function "engineer"))
e1-new
It will give the output as;
{:name "Nimal", :id 324, :function "engineer"}
Removing
(def e1-rm (dissoc e1-new :function))
e1-rm

Haskell class is not like classes in Java or c++.
Haskell got many numeric types, which are Int, Integer and Float. You can compare two numbers of the same type for equality but in not different types. Also you can compare two values of type Bool for equality, but you cannot add them together. A class is a template for types, and it gives the operations that the types must do.

class Eq a where
(==), (/=) :: a -> a -> Bool
It defines the == and /= functions.

Simple type decleration

plus :: Int -> Int -> Int
plus x y = x + y

This plus function will get 2 Int numbers, do the calculation and then it will return an Int too.

Monad
This is a very useful concept in haskell.
It,
Do an action from the type of which returns by the function.
Do an action which simply produce a value.
Do a chain "actions" together, while allowing the result of an action to be used for the second action

Maybe

farther :: Person -> maybe Person

This function will return data with type Person or if there is no value it will return nothing.

write this in a hakell file and load it into the intepreter. Now type;
quickSort ["i", "have", "a", "thing", "for", "linux"]
It will give the output like this.
["a","for","have","i","linux","thing"]

But if you type as;
quickSort ["I", "have", "a", "thing", "for", "Linux"]
you will get the output as;
["I","Linux","a","for","have","thing"]
this. Sorting is done by looking at the ASCII values and capital letters got less value than the others. So to avoid that thing we must ignore the case of the characters.

Modules
Module is a group of related functions in a singe package. We can use functions for our codings without implementing them. The only thing we must do is import the correct module and call the correct function. There are many ways of importing modules.

This will download come congigeration packages into the .emacs or .emacs.d directory and update the init.el file. Then you should remove the above code part (only that thing) from the init.el file.
Then open an emacs window and type

M-x package-list-packages
Then type r to update the list. And then select package (clojer-mode) from i and install it by x.

Then type;
M-x describe-variable and in the prompt type;
inferior-lisp-program

In that you should customize the variable to;
lein repl
then save it.
Now open a .clj file from emacs.

This script will connect to a mysql database via haskell script.
Firs of all you must need to download some modules which used to communicate with the database. For that you may you can use
hackage.haskell.org/package/
This link.

You should download and install,
HDBC module and
HDBC-mysql module.

If you encount some problen while you are installing those modules, you should check the dependenci packages as well. You should install those thing also inorder to run those packages without errors. Dependence details are in the package webpage.

Then you can create a test database to test the script. So for that do these thing in MySQL.

In this script you must change the user name, password and the mysqlUnixSocket path. The user name and its passwd should be able to access the animal database. You can get the mysql.sock path from the file /etc/my.cnf

Clojure is a dynamic programing language which use the java virtual machine. It directly compile the source code into java bytecode.
Doing projects with leingingen
Leiningen is a simple building tool for clojure. It will make "Build" files for your clojure project.

You can install leniingin by this script. Alsoit will install clojure for you.

Sep 21, 2010

Selenium is a good web application testing tool which is a free and open source software. It can run in many browsers, inmany operating systems and can controled by many programing languages and testing frameworks.
You can get more datails as well as the software from visiting the official website http://seleniumhq.org

First of all download the selenium ide from the website.

Then extrat the zip file.

Now you can see some jar file as well as some library files.

So first run the selenium server. For that you may need java. If you do not have java, you must install JDK first.
java -jar selenium-server.jar

Wait for about 1 min. It will give the configurations of your system.

Then you should deside which language you are using to control the selenium. I will use python but you can do it in come other language.(see the website)

Now you should change the directory to python library file. There you can see selenium lib files for python.
Then try to run a default test. To do that;

Sep 17, 2010

Folds - a flod apply a function to a list, accumilate items and give a single output.
There are 4 main fold functions;
foldr - this will fold up a list from the last element to the first element and apply a given function for each element.

fac n = foldr (*) 2 [1..n]

if you run this code and type as;
fac 3
it will give the output as
12
What will happen in this is,
3*2 + 2*2 + 1*2 = 12

foldl - this will go from the first element to the last element.
foldl1 and foldr1 -
these things will work as the way the foldl and foldr work. But this will give an error message when it is an empty array.

scan
This do the both things done by map and fold. It will accumilate values like fold but the output will be given as a list as in map for each intermediate value.

PostgreSQL is a very powerfull open source database. You can download the binary package or the source code by visitiing the website.
http://www.postgresql.org

I will describe how to install this by using the source code.

First of all you must download the source code from the website. It may like;
postgresql-.tar.bz2
Decompress the tar file.
Then change the directory into the decompressed diredtory.
Now you can see source files of the software.

Now type;
./configure
Then after that;
gmake
Then you should become a super user to comtinue the instalation.
For that type;
su
then if it ask for the password. Give the password and type,
gmake install

This will copy all files which needed to the software. But the instalation is not over. This database need an user called postgres. So to add this user to your system you must type;
useradd postgres
Then do these instructions
mkdir /usr/local/pgsql/data
chown postgres /usr/local/pgsql/data
su - postgres
The last instruction will change the user root to postgres.

Also you should type some instructions to make the configuration files. For that;
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data

Sep 16, 2010

Proxy is used to control network trafic and access controling. It will increase the network speed by caching the websites which freequently access by the users. It can also restrict ip addresses as well as protocls.

squid-cache is a proxy server which we can freely download and use.

You can download this by visiting the website http://squid-cache.org .

Download a stable vertion of squid.
There may by some distribution which are still on testing.

If you download a tar file decompress it.
Then change the directory to the squid director.

Now you can read the INSTALL file. It will say to run configuratoion file.
like
./configure --prefix=/usr/local/

This instalation will be placed in /user/local/ directory.
then type;
make all
Now you must switch to super user mode.
For that;
su -
After that;
make install

Now the instalation will run.
After the instalation you must customised the squid.conf file. It will be located in;
/usr/local/squid/etc/squid.conf

In that folder you may see some examples and those things will help you to do your task.

Then run the squid by typping
/usr/local/squid/sbin/squid

In that time if some permition error occure you may goto the directory,
/usr/llocal/squuid/
Then add a user and a group call squid by typing

useradd squid
groupadd squid

then type
chown squid -R var
chgrp qsuid -R var

And also change line of the squid.conf which like
#cache_effective_user nobody to
cache_effective_user squid

Dot Dot notation
You can get a sequence of integer numbers like;
[1..10] it will give the result as [1,2,3,4,5,6,7,8,9,10]
And also if you type it as
[1,3..10] it will be like
[1,3,5,7,9]
And also on the other way around too.
[5..0]
[5,4,3,2,1,0]

Never ending list
This is like a never ending loop;

loop n=n: loop(n+1)
runLoop = loop 1

If the user type;
runLoop
The haskell will give a never ending loop.

Head and tali
If you type;
let list = [1,2,3,4,5,6]
head list
it will give the output as
1
tail list
it will give the output as
[2,3,4,5,6]

We can write this as;
casefunction c =
case c of {0->0;10->1;100->2;_->-1}

This also gives the same answer.
But the order of these statments are very important. Because if you insert the default statement (_-> ) in the beggining you will get that as the result for every thing.

Outputs

hello = do
putStrLn "Hello World"

Load this one and type hello you will see;
Hello world

Inputs

input = do
putStrLn "What is your name ?"
name 10 then
do putStrLn "Num is greater than 10"
else
do putStrLn "Num is less than 10"

IF you load this thing and type as;
num 12
It will give the out put as
Num is greater than 10

Read Inputs

main =
do putStrLn "What is your name"
name
if (read name) =="guest" then
do putStrLn ("Welcome "++ name ++" ..!! ")
else
do putStrLn "Opss ...!!!"

In this it will ask to user to input some thing.
Then it will read the input and chech the condition.

Sep 14, 2010

function_name x =
if x<0
-1
else
1
if you write this as;
function_name 5
it will give the output as;
0
And also you can write if then else statment also.
function_name y =
if y<0
-1
else if y>0
1
else
0

function_name x =
if x<0
-1
else
1
if you write this as;
function_name 5
it will give the output as;
0
And also you can write if then else statment also.
function_name y =
if y<0
-1
else if y>0
1
else
0

Sep 8, 2010

Haskell is an advance purely functional language. This language is not like java or c.

Install haskell compiler in fedora
$yum install ghc

After the installation you can type
$ghci
to run haskell in interactive prompt.

Writing your own functions
You can write Haskell source code and load it into the interpreter. To do that you should;
1. Write a Haskell code in the working directiry including 'import Data.Maybe'at the top of your code if that code need modules.
2. Sav the code with the extention of .hs.
3. Run ghci command and at the prompt type ":l .sh " (l= L in lowercase)

Lists
let list=[2,3,4,5,7]
This is a list defined in Haskell. You can add values to the front of the list by doing
23:14:list
This will add 23 and 14 to the list and its output will like this.
[23,14,2,3,4,5,7]

Note:- If you need to get the file type of a variable and function you can type ':t'like this;
:t list
list - zipping and unzipping
This will take two lists zips them and returns a tupples.
zip [1,2,3]["aaa","bbb","ccc"]
the output will be like this,
[(1,"aaa"),(2,"bbb"),(3,"ccc")]

List - tali
This will alter the first item in the list. If you type as;
let list2=[1,2,3]
tail list2
This will give the output as;
[2,3]
In Haskell it is not updating the values, it only computes and give the output.

Sep 7, 2010

OS library - This will provide many number of functons which will interact with the operating system.
>>>import os # import the os library.
>>>dir(os) # can get a list of all module
# functions in the os module
>>>help(os) # will give the man page of os # module

Sep 2, 2010

This will handle the error if the user inputs an non-integer value.
In a program first the try clause runs. The exception clause runs only if try clause mane an exception.
If an exception occurs two things can be happend. first thing the exception will mach the exception clause and will excecute the code belongs to that thing. The other thing is an exception is not matching our names in the exception clause.

We can download a git project edit that thing. To do that we must;
$git clone remort_URL Our_destinatioon_folder
Eg:
bob$git clone git://git.videolan.org/vlc.git ~/vlc_clone

This will save a clone of vlc in the directory called vlc_clone.

Now you can edit that clone and commit.
If some other person is also involving on editing this clone he/she can get the Bob's edited git by using;

alise$git pull /home/bob/vlc_clone master

This will merges the changes of Bob's Master branch with Alise's current branch.

alise$git fetch /home/bob/vlc_clone master
alise$git log -p HEAD..FETCH_HEAD
This is more safer than just pulling if alise has not committed her editings.
By typing;
alise$gitk HEAD..FETCH_HEAD
Alise can see what are the changes Bob had done for the original clone.

alise$git remort add bob /home/bob/vlc_clone
By this Alice can add bob's clone without merging with her own branch.

alise$git fetch bob
In this it will simply create an extra branch without merging them with any of Alise's branchers.
In that case we must use:
alise$git log -p master..bob/master
in order to see the changers which Bob had make. Then if Alise need to merg them she can use;
$git pull . remortes/bob/master

Sep 1, 2010

sed
We can use sed to search for some string patterns. By using sed we can easily modify a string.

Search and replace
The basic command is like this:
sed 's/day/night/' output_fie
This code will search for the keyword 'day'in the input_file and replace it by night then save the changed file in output_file.

sed 's/\(day\).*/\1' output_file
This will search for the day keyword in the input file and it will delete other characters behind the keyword, then it save the data in output_file.

AWK
We can use awk for report writing and filtering. Mainly it is a good table creator.

We can create a module file and can use it in some other files. TO do that we must create the module file in the same directory and in our coding we must import it. If the module file is "module_file.py".
>>>import module_file
Then we can get the variables and methods by;
>>>module_file.variable #or
>>>module_file.method

We can view the thing in a module by doing
>>>import module
>>>def(module)
This will give all related details about the module.

Inputs and outputs
We can pass parameters when we are executing the python script.
$python script.py argument1 argument2

in the script we can use these arguments by using
if __name__="__main__":
import sys
function(sys.arg[1], sys.arg[2])

Open file
we can use this to open a externel file.
f=open('file_name','mode')
mode can be,
w - write
r - read
r+ - read and write
a - append

f.read(size) - will give the size
f.read - will read the file
f.readline - will read a line
f.write(string) - will write string to file
f.close() - will close the file