c++ 11 features with practical examples (for each or range based loop)

I am going to use a famous example of factory design pattern which creates two stooges.

Stooge class implementation along with factory method is here

#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
class Stooge {
public:
	static Stooge *make_stooge(int);
	virtual void tell_a_joke()=0;
};

class Jerry : public Stooge{
public:
	void tell_a_joke(){
		std::cout << "I am jerry the stooge \n";
	}
};

class Moe : public Stooge{
public:
	void tell_a_joke(){
		std::cout << "I am Moe the stooge \n";
	}
};
Stooge *Stooge::make_stooge(int choice)
{
	if (choice == 1)
	{
		return new Jerry;
	}
	else
		return new Moe;
}

In summary a factory pattern takes out burden from “main” function (class consumer normally) to write all the “new” and stuff.

now the main

int main()
	std::vector<Stooge*> circus;
	int choice;
	cin >> choice;
	while (choice != 0)
	{
		cin >> choice;
		circus.push_back(Stooge::make_stooge(choice));
	}

	for each (Stooge* st in circus) st->tell_a_joke();
	cout << " \n Previous to that you have to use mem_fun... see implementation in source code\n";
	for_each(circus.begin(), circus.end(), mem_fun(&Stooge::tell_a_joke));
	cin >> choice;
}

So you can see how using for each looks somewhat comparable to other languages (like python)
The older way (“for_each”) has to provide the address of the static function which requires using “mem_fun” function which in my opinion is ok but error prone

Advertisements

Funtional programming with Scala

My first impression of scala is that it’s too verbose. Thing that could be done in may be 1 line in haskell and 2 lines in erlang takes around 5-10 lines(Might not look like a big difference for 1 line but production codes easily span thousands of line even with the most expressive languages and here the factor is of 5). Anyways, I am still new so might not be my place to judge it.

Here goes implementation of some functions / practice problems in scala.

1. Pascal triangle such that pascal(r,c) such function return value at rth row and cth column

a. Haskel version used ZipWith [Signature ((A->A->A) -> [A] -> [A] -> [A]) which is not in scala so I wrote my own version.

(I don’t like the fact that I was not able to pattern match the arguments to map.. to do –> Find a way to pattern match the tuple)

def zipWith[A] (f: (A,A) => A, l1: List[A], l2: List[A]) : List [A] = {

val zipList = l1.zip(l2)

zipList.map(x=>f(x._1,x._2))

}

———————– Now goes my implementation of the above function —————————-

def pascal(c: Int, r: Int) : Int  {

def pascalH(pr: List[Int], currentRow :Int): List [Int] =

{

if (r==currentRow) pr else

{

val l1  = pr :+ 0

val l2 = List(0) ++ pr

val next_list = zipWith((x:Int,y:Int)=> x+y), l1,l2)

pascalH(next_list,cr+1)

}

val thePascalRow = pascalH(List(1),0)

thePascalRow(c)

}

—– Note that the helper definition works here because scala (as other functional languages) has lexical closure..

I am still new to scala to could not figure out how to use + instead of that lambda in the zipWith call — if you know please leave a comment