32
LinQ Why we have to teach functional programmming Axel T. Schreiner http://www.cs.rit.edu/~ats/talks/linq/ http://www.cs.rit.edu/~ats/cs-2006-1/14_linq.pdf

LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

LinQWhy we have to teach functional programmming

Axel T. Schreinerhttp://www.cs.rit.edu/~ats/talks/linq/

http://www.cs.rit.edu/~ats/cs-2006-1/14_linq.pdf

Page 2: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

cs-2006-1 10/30/06 linq-

Principles

Map a query language to cascading method calls.

Implement the necessary methods for IEnumerable, XML- and database-access.

Sugar syntax liberally.

2

Page 3: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

cs-2006-1 10/30/06 linq-

What if...

Syntactic sugar patterned after SQL.

3

struct Person { string first, last; int phone; }IContainer<Person> staff = ...Console.WriteLine( from x in staff where x.phone % 2 == 0 orderby x.last select new { x.first, x.phone });

Page 4: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

cs-2006-1 10/30/06 linq-

What if...

Syntactic sugar patterned after SQL.

Actions converted into messages,Expressions converted into functions.

4

struct Person { string first, last; int phone; }IContainer<Person> staff = ...Console.WriteLine( from x in staff where x.phone % 2 == 0 orderby x.last select new { x.first, x.phone });

Page 5: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

cs-2006-1 10/30/06 linq-

What if...

Functional style [Eric Meijer]:

function as arguments,function composition for actions.

Anonymous class new { field ,... }.

5

struct Person { string first, last; int phone; }IContainer<Person> staff = ...Console.WriteLine( staff .Where(x => x.phone % 2 == 0) .OrderBy(x => x.last) .Select(x => new { x.first, x.phone }));

Page 6: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

cs-2006-1 10/30/06 linq-

Typing

6

struct Person { string first, last; int phone; }IContainer<Person> staff = ...Console.WriteLine( staff .Where(x => x.phone % 2 == 0) .OrderBy(x => x.last) .Select(x => new { x.first, x.phone }));

class C<T> { delegate R Func<R, T> (T arg); C<T> Where (Func<bool, T> predicate); C<T> OrderBy<K> (Func<K, T> keySelector); C<U> Select<U> (Func<U, T> selector);}

Page 7: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

cs-2006-1 10/30/06 linq-

Typing

7

class C<T> { delegate R Func<R, T> (T arg); C<T> Where (Func<bool, T> predicate); C<T> OrderBy<K> (Func<K, T> keySelector); C<U> Select<U> (Func<U, T> selector);}

struct Person { string first, last; int phone; }IContainer<Person> staff = ...Console.WriteLine( staff .Where(x => x.phone % 2 == 0) .OrderBy(x => x.last) .Select(x => new { x.first, x.phone }));

Page 8: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

cs-2006-1 10/30/06 linq-

Typing

8

class C<T> { delegate R Func<R, T> (T arg); C<T> Where (Func<bool, T> predicate); C<T> OrderBy<K> (Func<K, T> keySelector); C<U> Select<U> (Func<U, T> selector);}

struct Person { string first, last; int phone; }IContainer<Person> staff = ...Console.WriteLine( staff .Where(x => x.phone % 2 == 0) .OrderBy(x => x.last) .Select(x => new { x.first, x.phone }));

Page 9: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

cs-2006-1 10/30/06 linq-

Typing Pattern

9

class C<T> { delegate R Func<R, T> (T arg); C<T> Where (Func<bool, T> predicate); C<T> OrderBy<K> (Func<K, T> keySelector); C<U> Select<U> (Func<U, T> selector);}

struct Person { string first, last; int phone; }IContainer<Person> staff = ...Console.WriteLine( staff .Where(x => x.phone % 2 == 0) .OrderBy(x => x.last) .Select(x => new { x.first, x.phone }));

Page 10: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

cs-2006-1 10/30/06 linq-

Query Expression Pattern

10

from join... introduce iteration variable(s)

fromlet ...where

introduces iteration variable(s)introduces variablefilters

orderby rearranges sequence

selectgroup by

produces flat resultproduces structured result

into join...

produces iteration variable(s) to start over

Page 11: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

cs-2006-1 10/30/06 linq-

Query Class Pattern

11

from join...

class C<T> { C<V> Join (...) C<V> GroupJoin (...)

fromlet ...where

C<T> Where (...)

orderby

O<T> OrderBy[Descending] (...) class O<T> : C<T> { O<T>ThenBy[Descending] (...) }

selectgroup by

C<U> Select[Many] (...) C<G<Key,E>> GroupBy (...) class G<Key,E> : C<E> {...}

into join...

Page 13: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

cs-2006-1 10/30/06 linq-

from let where ... / Where

13

from x in xs

where f

xs.Where(x => f)

retains only records satisfying a condition.

let x = e

defines name for value.

Page 14: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

delegate R Func<R,A> (A a);

IEnumerable<T> Where<T> (IEnumerable<T> container, Func<bool,T> predicate) { foreach (var c in container) if (predicate(c)) yield return c;}

Implementation

Page 15: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

public interface Func<R,A> { R f (A a); }public<T> Iterable<T> where (Iterable<T> container, Func<Boolean,T> predicate) { List<T> result = newList(); for (T c: container) if (predicate.f(c)) result.add(c); return result;}

delegate R Func<R,A> (A a);

IEnumerable<T> Where<T> (IEnumerable<T> container, Func<bool,T> predicate) { foreach (var c in container) if (predicate(c)) yield return c;}

Implementation

Page 16: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

int[] xs = { 1, 2, 3, 4, 5 };

from x in xs where x % 2 == 0 select x

Where(xs, x => x % 2 == 0)

Use

Page 17: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

List<Integer> xs = Arrays.asList(new Integer[]{ 1, 2, 3, 4, 5});where(xs, new Func<Boolean,Integer>() { public Boolean f (Integer x) { return x % 2 == 0; }})

int[] xs = { 1, 2, 3, 4, 5 };

from x in xs where x % 2 == 0 select x

Where(xs, x => x % 2 == 0)

Arrays are not Iterable.

Use

Page 18: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

cs-2006-1 10/30/06 linq-

select / Select

16

from x in xs

select f

xs.Select(x => f)

creates new records.

Page 19: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

delegate R Func<R,A> (A a);

IEnumerable<U> Select<U,T> (IEnumerable<T> container, Func<U,T> selector) { foreach (var c in container) yield return selector(c);}

Implementation

Page 20: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

public interface Func<R,A> { R f (A a); }public<U,T> Iterable<U> select (Iterable<T> container, Func<U,T> selector) { List<U> result = newList(); for (T c: container) result.add(selector.f(c)); return result;

}

delegate R Func<R,A> (A a);

IEnumerable<U> Select<U,T> (IEnumerable<T> container, Func<U,T> selector) { foreach (var c in container) yield return selector(c);}

Implementation

Page 21: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

cs-2006-1 10/30/06 linq-

select / Select[Many]

18

from x in xs

select f

xs.Select(x => f)

creates new records.

from x in xs

from y in ys

select f

xs.SelectMany(x => ys.Select(y => f))

creates new records.

Page 22: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

delegate R Func<R,A> (A a);

IEnumerable<U> SelectMany<U,T> (IEnumerable<T> container, Func<IEnumerable<U>,T> selector) { foreach (var c in container) foreach (var u in selector(c)) yield return u;}

Implementation➦

Page 23: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

public interface Func<R,A> { R f (A a); }public<U,T> Iterable<U> selectMany (Iterable<T> container, Func<Iterable<U>,T> selector) { List<U> result = newList(); for (T c: container) for (U u: selector.f(c)) result.add(u); return result;

}

delegate R Func<R,A> (A a);

IEnumerable<U> SelectMany<U,T> (IEnumerable<T> container, Func<IEnumerable<U>,T> selector) { foreach (var c in container) foreach (var u in selector(c)) yield return u;}

Implementation➦

Page 24: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

int[] xs = { 1, 2, 3, 4, 5 };double[] ys = { 6.0, 7.0, 8.0, 9.0, 10.0 };

from x in xs where x > 3 from y in ys where y < 8.0 select new { y, x }

SelectMany(Where(xs, x => x > 3), x => Select(Where(ys, y => y < 8.0), y => new { y, x }))

Use

Page 25: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

List<Integer> xs = Arrays.asList(new Integer[]{ 1, 2, 3, 4, 5});final List<Double> ys = Arrays.asList(new Double[]{ 6.0, 7.0, 8.0, 9.0, 10.0 });

selectMany( where(xs, new Func<Boolean,Integer>() { public Boolean f (Integer x) { return x > 3; } }), new Func<Iterable<HashMap<String,Object>>,Integer>() { public Iterable<HashMap<String,Object>> f (final Integer x) { return select( where(ys, new Func<Boolean,Double>() { public Boolean f (Double y) { return y < 8.0; } }), new Func<HashMap<String,Object>,Double>() { public HashMap<String,Object> f (Double y) { HashMap<String,Object> result = new HashMap<String,Object>(); result.put("y", y); result.put("x", x); return result; } }); } })

Page 26: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

Teach functional!

A computer scientist invents this...

Action sequence: compositionCondition/selection: lambdas as argumentsNested selections: application of lambda Sorting: compose Comparator based on lambda arguments

No chance to see this in Java [56]...

Page 27: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

Teach functional!

A computer scientist invents this...

Action sequence: compositionCondition/selection: lambdas as argumentsNested selections: application of lambda Sorting: compose Comparator based on lambda arguments

No chance to see this in Java [56]...

Page 28: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

Teach functional!

A computer scientist invents this...

Action sequence: compositionCondition/selection: lambdas as argumentsNested selections: application of lambda Sorting: compose Comparator based on lambda arguments

No chance to see this in Java [56]...

Page 29: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

Teach functional!

A computer scientist invents this...

Action sequence: compositionCondition/selection: lambdas as argumentsNested selections: application of lambda Sorting: compose Comparator based on lambda arguments

No chance to see this in Java [56]...

Page 30: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

Teach functional!

A computer scientist invents this...

Action sequence: compositionCondition/selection: lambdas as argumentsNested selections: application of lambda Sorting: compose Comparator based on lambda arguments

No chance to see this in Java [56]...

Page 31: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

Teach functional!

A computer scientist invents this...

Action sequence: compositionCondition/selection: lambdas as argumentsNested selections: application of lambda Sorting: compose Comparator based on lambda arguments

No chance to see this in Java [56]...

Page 32: LinQ - cs.rit.eduats/talks/linq/linq.pdf · cs-2006-1 10/30/06 linq-Principles Map a query language to cascading method calls. Implement the necessary methods for IEnumerable, XML-

References

Erik Meijerhttp://research.microsoft.com/~emeijer/

C#: LinQhttp://msdn.microsoft.com/netframework/future/linq/

C# 3.0http://msdn2.microsoft.com/en-us/vcsharp/aa336745.aspx

Java 7: Closureshttp://www.javac.info/closures-v03.html