C# – Dynamic methods with lambda expressions

 

 

Lambda expression is a function that is not bound with an identifier

The logic behind this is like the one shown below.

(parameters) => expression

I know it looks weird so I will try to make it look less weirdo.

using System;

namespace lambdaexpressions
{
    public class Program
    {
        
        public static void Main(string[] args)
        {
            
			Action greetings = () => { Console.WriteLine("Hi, Sir! Welcome."); };
 			greetings();

  			Action saymyname = (name) => { Console.WriteLine("Hi "+name+"! Have a nice day!" ); };
			saymyname("John Doe");
			saymyname("Jane Doe");
			saymyname("Mehmet Burak");
  			
            
        }
        
    }
}

Here, Lambda expressions let us use dynamic functions.

Output:

Hi, Sir! Welcome.
Hi John Doe! Have a nice day!
Hi Jane Doe! Have a nice day!
Hi Mehmet Burak! Have a nice day!
using System;

namespace lambdaexpressions
{
    public class Program
    {
		static void nameandsurname(Action callback,string surname){
		
			Console.Write("Hi, ");
			callback();
			Console.WriteLine(surname + "! How are ye?");
			
		}
		
        public static void Main(string[] args)
        {
            string gender = "male";
	    nameandsurname( () => { if(gender =="male"){ Console.Write("John "); }else if(gender =="female"){	Console.Write("Jane ");	}},"Doe");
        }
    }
}

In here, we used System.Action to redirect the parameters to our function.
Output:

Hi, John Doe! How are ye?

C# – Delegate

What the heck is a delegate?

Delegate:

– Function pointers
– Can be changed run-time
– Comes from System.Delegate

using System;

delegate void myDelegate(string name);

namespace delegatedis
{
    public class Program
    {
        static void women(string name){
            Console.WriteLine("Hi Miss "+name+"! Have a nice day!");
        }

        static void attackhelicopter(string name){
            Console.WriteLine("Hi Apache " +name+"! gl hf!");
        }
        
        public static void Main(string[] args)
        {
            myDelegate mydel;
            string name = "Jane Doe";
            mydel = women; // We can use mydel like it is a pointer.
            mydel(name);
            string name = "AH-64";
            mydel = attackhelicopter; 
            mydel(name);
            
        }
    }
}

Out:

Hi Miss Jane Doe! Have a nice day!
Hi Apache AH-64! gl hf!

Installing Lisp working environment slime with Emacs in Ubuntu

No matter how hard it looks emacs is not that hard to use, at least for basic purposes.

 

Let’s install emacs first:

sudo apt-get install emacs -y

Install Steel bank common lisp the Common Lisp compiler:

sudo apt-get install sbcl

Install the slime(Superior Lisp Interaction for Emacs) for Emacs:

sudo apt-get install slime

Now, we can open the emacs

emacs

Press “Alt – X” to jump to the command line and type “slime” and open up our working environment.

For example, let’s code a function that returns the double of given integers.

We will have a screen like the one below;

; SLIME 2015-08-24
CL-USER>

We can start coding here:

(defun double (x) (* x 2))

After we complete the function we can call it and give an input.

(double 6)

It will return 12 as you know.

Good luck, have fun!