Presentation is loading. Please wait.

Presentation is loading. Please wait.

Map, filter and reduce Programming by composition.

Similar presentations


Presentation on theme: "Map, filter and reduce Programming by composition."— Presentation transcript:

1 Map, filter and reduce Programming by composition

2 Mapping Apply operation to each element of a list, gathering the results in a new list. Example: – list: (“Your” “call” “is” “important” “to” “us”) – operation: string length – result: (4 4 2 9 2 2)

3 Conceptual diagram (specific case) (“Your” “call” “is” “important” “to” “us”) MAP String length (4 4 2 9 2 2)

4 Conceptual diagram (generalized) Input list MAP Unary Operation Output list

5 Conceptual diagram (generalized) Input list MAP Unary Operation Output list INVARIANT

6 VARIANTS Conceptual diagram (generalized) Input list MAP Unary Operation Output list

7 public class Map implements IAlgo,Domain,LRStruct > { public LRStruct emptyCase(LRStruct host, IUnaryOperation arg) { return new LRStruct (); } public LRStruct nonEmptyCase(LRStruct host, IUnaryOperation arg) { return host.getRest().execute(this,arg).insertFront(arg.apply(host.getDatum())); } Code (Map)

8 public class Map implements IAlgo,Domain,LRStruct > { public LRStruct emptyCase(LRStruct host, IUnaryOperation arg) { return new LRStruct (); } public LRStruct nonEmptyCase(LRStruct host, IUnaryOperation arg) { return host.getRest().execute(this,arg).insertFront(arg.apply(host.getDatum())); } Code (Map) Invariant parts

9 public class Map implements IAlgo,Domain,LRStruct > { public LRStruct emptyCase(LRStruct host, IUnaryOperation arg) { return new LRStruct (); } public LRStruct nonEmptyCase(LRStruct host, IUnaryOperation arg) { return host.getRest().execute(this,arg).insertFront(arg.apply(host.getDatum())); } Code (Map) Invariant parts

10 public class Map implements IAlgo,Domain,LRStruct > { public LRStruct emptyCase(LRStruct host, IUnaryOperation arg) { return new LRStruct (); } public LRStruct nonEmptyCase(LRStruct host, IUnaryOperation arg) { return host.getRest().execute(this,arg).insertFront(arg.apply(host.getDatum())); } Map operation to rest of list, getting answer recursively Code (Map) Invariant parts

11 public class Map implements IAlgo,Domain,LRStruct > { public LRStruct emptyCase(LRStruct host, IUnaryOperation arg) { return new LRStruct (); } public LRStruct nonEmptyCase(LRStruct host, IUnaryOperation arg) { return host.getRest().execute(this,arg).insertFront(arg.apply(host.getDatum())); } Apply operation to first item of list Code (Map) Invariant parts

12 public class Map implements IAlgo,Domain,LRStruct > { public LRStruct emptyCase(LRStruct host, IUnaryOperation arg) { return new LRStruct (); } public LRStruct nonEmptyCase(LRStruct host, IUnaryOperation arg) { return host.getRest().execute(this,arg).insertFront(arg.apply(host.getDatum())); } Insert result into new list Code (Map) Invariant parts

13 public class Map implements IAlgo,Domain,LRStruct > { public LRStruct emptyCase(LRStruct host, IUnaryOperation arg) { return new LRStruct (); } public LRStruct nonEmptyCase(LRStruct host, IUnaryOperation arg) { return host.getRest().execute(this,arg).insertFront(arg.apply(host.getDatum())); } Code (Map) Variant parts VARIANT: the input list

14 public class Map implements IAlgo,Domain,LRStruct > { public LRStruct emptyCase(LRStruct host, IUnaryOperation arg) { return new LRStruct (); } public LRStruct nonEmptyCase(LRStruct host, IUnaryOperation arg) { return host.getRest().execute(this,arg).insertFront(arg.apply(host.getDatum())); } VARIANT: the operation Code (Map) Variant parts VARIANT: the operation

15 The IUnaryOperation interface public interface IUnaryOperation { public Range apply(Domain arg); }

16 Mapping: Domain  Range f: x  f(x) DOMAIN RANGE x f(x)

17 A concrete operation public class StringLen implements IUnaryOperation { public Integer apply(String arg) { return arg.length(); } DOMAI N RANGE

18 A concrete operation public class Times2 implements IUnaryOperation { public Integer apply(Integer arg) { return 2*arg; } public String toString() { return "x -> 2*x"; } DOMAI N RANGE

19 Reduction Combine all values of a list using (op,id) a binary operation op, with identity element id. Example: – list: (4 4 2 9 2 2) – operation (+,0) – result: 23

20 The IBinaryOperation interface public interface IBinaryOperation { public Range apply(Domain1 arg1, Domain2 arg2); public Range identityElement(); }

21 Filtering Apply a predicate to each element of a list, building a new list of those elements for which the predicate is true. Example: – list: (“Your” “call” “is” “important” “to” “us”) – predicate: contains exactly one vowel – result: (“call” “is” “to” “us”)

22 The IPredicate interface public interface IBinaryOperation { public Range apply(Domain1 arg1, Domain2 arg2); public Range identityElement(); }


Download ppt "Map, filter and reduce Programming by composition."

Similar presentations


Ads by Google