IAP C# 2011 Lecture 2: Delegates, Lambdas, LINQ Geza Kovacs.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms The C Programming Language.
Advertisements

Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Svetlin Nakov Telerik Corporation
C# and LINQ Yuan Yu Microsoft Research Silicon Valley.
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
IAP C# Lecture 3 Parallel Computing Geza Kovacs. Sequential Execution So far, all our code has been executing instructions one after another, on a single.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Lecture 2: Design and Implementation of Lambda Expressions in Java 8 Alfred V. Aho CS E6998-1: Advanced Topics in Programming Languages.
2.3 Cool features in C# academy.zariba.com 1. Lecture Content 1.Extension Methods 2.Anonymous Types 3.Delegates 4.Action and Func 5.Events 6.Lambda Expressions.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
C++ fundamentals.
Introduction to Methods
PARALLEL PROGRAMMING ABSTRACTIONS 6/16/2010 Parallel Programming Abstractions 1.
LINQ Programming in C# LINQ CSE Prof. Roger Crawfis.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Extension Methods Programming in C# Extension Methods CSE Prof. Roger Crawfis.
LINQ, An IntroLINQ, An Intro Florin−Tudor Cristea, Microsoft Student Partner.
Advanced .NET Programming I 13th Lecture
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 11 th Lecture Pavel Ježek
Lambdas and Streams. Functional interfaces Functional interfaces are also known as single abstract method (SAM) interfaces. Package java.util.function.
Getting familiar with LINQ to Objects Florin−Tudor Cristea, Microsoft Student Partner.
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
Putting it all together: LINQ as an Example. The Problem: SQL in Code Programs often connect to database servers. Database servers only “speak” SQL. Programs.
Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Based on material from Telerik Corporation.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
 Although VERY commonly used, arrays have limited capabilities  A List is similar to an array but provides additional functionality, such as dynamic.
Introduction to LINQ Chapter 11. Introduction Large amounts of data are often stored in a database—an organized collection of data. A database management.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
IAP C# and.NET 2011 Lecture 1: Basic Syntax Geza Kovacs.
Lambda Expressions Version 1.0
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
C#: Future Directions in Language Innovation Anders Hejlsberg TLN307 Technical Fellow Microsoft Corporation.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
FEN 2014UCN Teknologi/act2learn1 Higher order functions Observer Pattern Delegates Events Visitor Pattern Lambdas and closures Lambdas in libraries.
Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Classes - Intermediate
LINQ and Lambda Expressions Telerik Software Academy LINQ Overview.
Chapter 11.  Large amounts of data are often stored in a database—an organized collection of data.  A database management system (DBMS) provides mechanisms.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Building Web Applications with Microsoft ASP
Lambda Expressions Version 1.1
Taking Java from purely OOP by adding “functional level Programming”
Advanced .NET Programming I 6th Lecture
Generics, Lambdas, Reflections
CompSci 230 Software Construction
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Introduction to LINQ Chapter 11 10/28/2015 Lect 4 CT1411.
An Introduction to Java – Part I, language basics
Group Status Project Status.
Introduction to LINQ Chapter 11.
Defining methods and more arrays
Type & Typeclass Syntax in function
Chapter 6 Methods.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Object Oriented Programming
Advanced .NET Programming I 7th Lecture
Advanced .NET Programming I 6th Lecture
Corresponds with Chapter 5
Chengyu Sun California State University, Los Angeles
Presentation transcript:

IAP C# 2011 Lecture 2: Delegates, Lambdas, LINQ Geza Kovacs

Two ways of thinking about operations on collections Task: I have an array of integers. I want a new array containing just the positive integers Imperative style: Use a loop With a Language Integrated Query (LINQ): Define a query (a request for information) that’ll request the positive integers, and execute it

using System; using System.Collections.Generic; static class MyMainClass { static void Main(string[] args) { int[] arr = new int[] { 1, -1, 0, 4, -3, 2 }; var positiveList = new LinkedList (); foreach (int x in arr) { if (x > 0) positiveList.AddLast(x); } int[] positiveArr = positiveList.ToArray(); // contains { 1, 4, 2 } }

using System; using System.Collections.Generic; static class MyMainClass { static void Main(string[] args) { int[] arr = new int[] { 1, -1, 0, 4, -3, 2 }; var positiveList = new LinkedList (); foreach (int x in arr) { if (x > 0) positiveList.AddLast(x); } int[] positiveArr = positiveList.ToArray(); // contains { 1, 4, 2 } } Entry point for application

using System; using System.Collections.Generic; static class MyMainClass { static void Main(string[] args) { int[] arr = new int[] { 1, -1, 0, 4, -3, 2 }; var positiveList = new LinkedList (); foreach (int x in arr) { if (x > 0) positiveList.AddLast(x); } int[] positiveArr = positiveList.ToArray(); // contains { 1, 4, 2 } } Array initialization

using System; using System.Collections.Generic; static class MyMainClass { static void Main(string[] args) { int[] arr = new int[] { 1, -1, 0, 4, -3, 2 }; var positiveList = new LinkedList (); foreach (int x in arr) { if (x > 0) positiveList.AddLast(x); } int[] positiveArr = positiveList.ToArray(); // contains { 1, 4, 2 } } A Generic class

using System; using System.Collections.Generic; static class MyMainClass { static void Main(string[] args) { int[] arr = new int[] { 1, -1, 0, 4, -3, 2 }; var positiveList = new LinkedList (); foreach (int x in arr) { if (x > 0) positiveList.AddLast(x); } int[] positiveArr = positiveList.ToArray(); // contains { 1, 4, 2 } } A Generic class A using statement, saves us the need to type out System.Collections.Generic. LinkedList

using System; using System.Collections.Generic; static class MyMainClass { static void Main(string[] args) { int[] arr = new int[] { 1, -1, 0, 4, -3, 2 }; var positiveList = new LinkedList (); foreach (int x in arr) { if (x > 0) positiveList.AddLast(x); } int[] positiveArr = positiveList.ToArray(); // contains { 1, 4, 2 } } Type is inferred

using System; using System.Collections.Generic; static class MyMainClass { static void Main(string[] args) { int[] arr = new int[] { 1, -1, 0, 4, -3, 2 }; var positiveList = new LinkedList (); foreach (int x in arr) { if (x > 0) positiveList.AddLast(x); } int[] positiveArr = positiveList.ToArray(); // contains { 1, 4, 2 } } Instance method (defined in the LinkedList class)

Two ways of thinking about operations on collections Task: I have an array of integers. I want a new array containing just the positive integers Imperative style: Use a loop With a Language Integrated Query (LINQ): Define a query (a request for information) that’ll request the positive integers, and execute it

LINQ Compact way to define a query To understand how it’s implemented, we’ll need to know about delegates and lambdas int[] arr = new int[] { 1, -1, 0, 4, -3, 2 }; var query = from x in arr where x > 0 select x; int[] positiveArr = query.ToArray(); // contains { 1, 4, 2 }

Delegates A type that can reference a method Here, we declare a delegate type IntegerToBool which can reference a method that has one int argument and returns a bool delegate bool IntegerToBool(int x);

Delegates A type that can reference a method Here, we declare a delegate type IntegerToBool which can reference a method that has one int argument and returns a bool delegate bool IntegerToBool(int x); Delegate type name is IntegerToBool

Delegates A type that can reference a method Here, we declare a delegate type IntegerToBool which can reference a method that has one int argument and returns a bool delegate bool IntegerToBool(int x); 1 int argument Delegate type name is IntegerToBool

Delegates A type that can reference a method Here, we declare a delegate type IntegerToBool which can reference a method that has one int argument and returns a bool delegate bool IntegerToBool(int x); Returns a bool 1 int argument Delegate type name is IntegerToBool

delegate bool IntegerToBool(int x); static class MyMainClass { static bool IsPositive(int v) { return v > 0; } static void Main(string[] args) { IntegerToBool fn = IsPositive; bool isFivePositive = fn(5); } A delegate can reference a static method fn references the isPositive static method

delegate bool IntegerToBool(int x); static class MyMainClass { static bool IsPositive(int v) { return v > 0; } static void Main(string[] args) { IntegerToBool fn = IsPositive; bool isFivePositive = fn(5); } A delegate can reference a static method invoking fn will invoke isPositive

using System.Collections.Generic; delegate bool IntegerToBool(int x); static class MyMainClass { static void Main(string[] args) { var set = new HashSet (); set.Add(5); IntegerToBool fn = set.Contains; bool setContainsFive = fn(5); bool setContainsThree = fn(3); } A delegate can also reference an instance method fn reference the HashSet instance’s Contains method

using System.Collections.Generic; delegate bool IntegerToBool(int x); static class MyMainClass { static void Main(string[] args) { var set = new HashSet (); set.Add(5); IntegerToBool fn = set.Contains; bool setContainsFive = fn(5); bool setContainsThree = fn(3); } A delegate can also reference an instance method invoking fn will invoke set.Contains

Methods can accept delegate types as arguments delegate bool IntegerToBool(int x); static int[] filterIntegers(int[] orig, IntegerToBool fn) { var list = new LinkedList (); foreach (int x in orig) { if (fn(x)) list.AddLast(x); } return list.ToArray(); } filterIntegers is a method that takes an IntegerToBool delegate as an argument

Can pass any method matching the delegate’s signature (same return value and arguments) to a function that has a delegate as an argument static class MyMainClass { delegate bool IntegerToBool(int x); static int[] filterIntegers(int[] orig, IntegerToBool fn){ … } static bool IsPositive(int v) { return v > 0; } static void Main(string[] args) { int[] orig = new int[] { 1, -1, 0, 4, -3, 2}; int[] arr = filterIntegers(orig, IsPositive); } IsPositive matches IntegerToBool’s signature, so it can be passed without explicit conversion

Observe the definition of our IntegerToBool delegate type: It can reference only methods with 1 int argument, and a bool return value. delegate bool IntegerToBool(int x);

Observe the definition of our IntegerToBool delegate type: It can reference only methods with 1 int argument, and a bool return value. If we want to reference a method with an int return value, we’d need to declare another type: delegate bool IntegerToBool(int x); delegate int IntegerToInteger(int x);

Observe the definition of our IntegerToBool delegate type: It can reference only methods with 1 int argument, and a bool return value. If we want to reference a method with an int return value, we’d need to declare another type: If we want to reference a method with a string argument, we’d need to declare another type: delegate bool IntegerToBool(int x); delegate int IntegerToInteger(int x); delegate bool StringToBool(string x);

The System.Func delegate delegate TResult Func (T1 arg1); Used for referencing methods with 1 argument and 1 return value A generic delegate type defined as:

The System.Func delegate delegate TResult Func (T arg1); delegate TResult Func (T1 arg1, T2 arg2); Used for referencing methods with 1 argument and 1 return value Used for referencing methods with 2 arguments and 1 return value A generic delegate type defined as:

The System.Func delegate A generic delegate type defined as: delegate TResult Func (T arg1); delegate TResult Func (T1 arg1, T2 arg2); delegate TResult Func (T1 arg1, T2 arg2, T3 arg3); … (etc) Used for referencing methods with 1 argument and 1 return value Used for referencing methods with 2 arguments and 1 return value Used for referencing methods with 3 arguments and 1 return value

We can use System.Func instead of defining our own IntegerToBool delegate type: using System; static class MyMainClass { static int[] filterIntegers(int[] orig, Func fn) { … } static bool IsPositive(int v) { return v > 0; } static void Main(string[] args) { int[] orig = new int[] { 1, -1, 0, 4, -3, 2}; int[] arr = filterIntegers(orig, IsPositive); } Func : references method with one int argument which returns bool; matches IsPositive’s signature

Lambdas Notice that in our previous example, we had an IsPositive method which did very little: Lambdas are a shorthand for declaring short methods, which allow them to be declared in a single expression: static bool IsPositive(int v) { return v > 0; } Func IsPositive = (int x) => { return x > 0; };

Func IsPositive = (int v) => { return v > 0; }; Argument types can be excluded: If it’s just a single statement, you can also omit the return statement and braces If the lambda has just a single argument, the parentheses can be omitted: Func IsPositive = (v) => { return v > 0; }; Func IsPositive = (v) => v > 0; Func IsPositive = v => v > 0;

Because lambdas are expressions, we can declare lambdas directly in function invocations using System; static class MyMainClass { static int[] filterIntegers(int[] orig, Func fn) { … } static void Main(string[] args) { int[] orig = new int[] { 1, -1, 0, 4, -3, 2}; int[] arr = filterIntegers(orig, v => v > 0); } lambda expression

Returning to LINQ LINQ defines an method called Where, which is similar to our filterIntegers example method: – Takes a collection of values and a Func that outputs a bool for each element; returns those elements for which the Func returns true

LINQ: Where LINQ’s Where method, however, works with more general collections (as opposed to filterIntegers, which works only with int[]) static int[] filterIntegers(int[] orig, Func fn); static IEnumerable Where ( this IEnumerable source, Func predicate );

LINQ: Where IEnumerable: An interface implemented by collections (including LinkedList, HashSet, arrays, etc) static IEnumerable Where ( this IEnumerable source, Func predicate );

LINQ: Where TSource: a generic type parameter (so it’ll work with collections of int, string, custom classes, etc) static IEnumerable Where ( this IEnumerable source, Func predicate );

LINQ: Where this: It’s an extension method for IEnumerable static IEnumerable Where ( this IEnumerable source, Func predicate );

LINQ: Where using System.Collections.Generic; using System.Linq; static void Main(string[] args) { int[] arr = new int[] { 1, -1, 0, 4, -3, 2 }; IEnumerable query = arr.Where(v => v > 0); int[] positiveArr = arr.ToArray(); // contains { 1, 4, 2 } }

using System.Collections.Generic; using System.Linq; static void Main(string[] args) { int[] arr = new int[] { 1, -1, 0, 4, -3, 2 }; IEnumerable query = arr.Where(v => v > 0); int[] positiveArr = arr.ToArray(); // contains { 1, 4, 2 } } LINQ: Where Extension methods (like Where, for IEnumerable) can be invoked with same syntax as instance methods

using System.Collections.Generic; using System.Linq; static void Main(string[] args) { int[] arr = new int[] { 1, -1, 0, 4, -3, 2 }; IEnumerable query = arr.Where(v => v > 0); int[] positiveArr = arr.ToArray(); // contains { 1, 4, 2 } } LINQ: Where ToArray, like Where, is also an extension method for IEnumerable. Both are defined in System.Linq Need this, otherwise Where and ToArray won’t be defined

LINQ: Select Suppose you have an array of product objects, each of which have a name and price. You want an array of strings, with all the product names. We can use Select for this class Product { public string name; public int price; }

LINQ: Select Product[] products = new Product[] {…}; IEnumerable query = products.Select(v => v.name); string[] productNames = query.ToArray();

LINQ: Select Argument to Select is a selector function which take one element (a Product instance), and returns something else (which may have a different type, as in this example) Product[] products = new Product[] {…}; IEnumerable query = products.Select(v => v.name); string[] productNames = query.ToArray();

Alternative LINQ Syntax Product[] products = new Product[] {…}; IEnumerable query = products.Select(v => v.name); string[] productNames = query.ToArray(); Equivalent to Product[] products = new Product[] {…}; IEnumerable query = from v in products select v.name; string[] productNames = query.ToArray();

Using Where and Select Together LINQ allows query operators like Where and Select to be combined For example, what are the names of all products with price less than 4? Product[] products = new Product[] {…}; IEnumerable query = products.Where(v => v.price < 4).Select(v => v.name); string[] productNames = query.ToArray();

Using Where and Select Together LINQ allows query operators like Where and Select to be combined For example, what are the names of all products with price less than 4? Product[] products = new Product[] {…}; IEnumerable query = from v in products where v.price < 4 select v.name; string[] productNames = query.ToArray();

Aggregate Known in other languages as “reduce” or “fold” Begins with a seed value (ie first element in the sequence), then applies a function from left to right in the sequence, keeping some running value. Ex: Finding a sum: keep a running total of the sum so far, initialize it to the leftmost element in the sequence, and for each new element, add it to the running total

Aggregate – Implementing Sum Ex: Finding a sum: keep a running total of the sum so far, initialize it to the leftmost element in the sequence, and for each new element, add it to the running total static class MyMainClass { static void Main(string[] args) { double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; double sum = doubles.Aggregate((runningSum, nextItem) => runningSum + nextItem); // 12.9 }

static class MyMainClass { static void Main(string[] args) { double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; double product = doubles.Aggregate((runningProduct, nextItem) => runningProduct * nextItem); // } Aggregate – Implementing Product Ex: Finding a product: keep a running product, initialize it to the leftmost element in the sequence, and for each new element, multiply the running product by it

More Operators Max, Min, Reverse, OrderBy, etc See us/vcsharp/aa for exampleshttp://msdn.microsoft.com/en- us/vcsharp/aa336746