Wednesday, August 14, 2013

Lambda Expressions

A function can be sent as a parameter to another sub procedure or function.

For example:

    <Extension>
    Public Iterator Function Filter(aBookEnum As IEnumerable(Of Book), selector As Func(Of Book, Boolean)) As IEnumerable(Of Book)
        For Each thisBook As Book In aBookEnum
            If selector(thisBook) Then
                Yield thisBook
            End If
        Next
    End Function

The selector parameter is defined with the Func type.  This means that a function must be passed in with a return type of boolean and takes a parameter of type Book.

So when we call the Filter extension method, can do the following:

Let's say you have a function that meets the criteria for the parameter:

    Function GetBookOne(myBook As Book) As Boolean
        Return myBook.Title = "Book One"
    End Function

You could send this function as a parameter like this:

Dim MySelector As Func(Of Book, Boolean) = AddressOf GetBookOne
MyBookShelf.Filter(MySelector)

Or you could use a lambda expression and seriously streamline your code like this:

MyBookShelf.Filter(Function(x) x.Title = "Book One")

So, "x" represents the Book input parameter.

Tuesday, August 13, 2013

What is IEnumerable(Of T)?

IEnumerable is the base interface for all generic collections that can be enumerated (iterated through).  Examples of classes that implement IEnumerable include Lists, Arrays, Stacks, and Queues.  You can also write your own classes that implement it.  Any class that implements this interface can have LINQ queries performed on its enumerator.  You can also iterate through the collection using the For Each operator.  T is the type of item that the collection stores.  When you perform LINQ queries, you often get anonymous classes that implement the IEnumerable interface in return.

The System.Linq namespace contains all kinds of extension methods that operate on classes that implement the IEnumerable interface.  This is what allows you to write LINQ queries against these objects.

Use Syntax Highlighter to display code in your Blogger posts

Edit the HTML of your blogger template.  From your blog dashboard, click Template on the left side, and then click the Edit HTML button.  Add the following to the head section.

    <link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
    <link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeEclipse.css' rel='stylesheet' type='text/css'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'/>
    <script language='javascript' type='text/javascript'>
    SyntaxHighlighter.config.bloggerMode = true;
    SyntaxHighlighter.all();
    </script>

This example is set up for JavaScript, XML, and VB, but you can find other "brushes" here:

http://alexgorbatchev.com/SyntaxHighlighter/manual/brushes/

Switching to HTML when writing your post, if you wanted to demonstrate a bit of HTML, you do this:

<pre class="brush:xml;">
    &lt;script src="StevesJScript.js" type="text/javascript"&gt;&lt;/script&gt;
</pre>

Note that I had to escape the angle brackets with the &lt; and &gt;.

Symmetric and Asymmetric Encryption (and SSL)

Symmetric encryption requires one shared private key.  This same key is used by Person A to encrypt the message and by Person B to decrypt the message.  They can go back and forth all day long using the same key.  It's great and it's private.  However, the problem is getting the key to the other person.  Physically giving the key to the other person is great, but obviously not practical for an internet situation.

Asymmetric encryption is another technique.  Asymmetric means one-way.  It is another means of encrypting a message, but it can only occur in one direction.  Person A can encrypt a message that only Person B can decrypt.  How?  Mathematics.  You don't really need to understand the cryptography details to understand this.  Person B has a private key.  Mathematically, Person B can derive a public key from this private key.  Due to the miracle of modern math, this "public" key can be used to encrypt a message, but not decrypt the message.  Person B can give this public key out to anyone that he wants to receive a secure message from and only Person B will be able to decrypt the message.  So Person B sends Person A the public key.  Person A encrypts his message using the public key, and sends it to Person B who decrypts the message using the private key.  The big breakthrough here is that a private key never has to be given out.

How are these two methods used?  SSL.  A combination of these two methods is used to establish a secure browsing session.  The web server has a certificate installed that contains the private key.  When average Joe points his browser to the web site, the server will generate a certificate that contains the public key derived from this private key and send it to the browser.  The browser generates something called a "session key" (AKA shared private key ), and sends this to the web server using the public key in the certificate.  Now, the web server and the browser both know the session key (AKA shared private key) and can begin communicating both ways using symmetric encryption.

To summarize, the shared private key is securely given to the web server by using asymmetric encryption.  Once the shared private key is in both party's hands, symmetric encryption can commence.