1 / 22

Map, filter and reduce

Map, filter and reduce. Programming by composition. 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). Conceptual diagram (specific case).

oralee
Download Presentation

Map, filter and reduce

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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”) String length MAP (4 4 2 9 2 2)

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

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

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

  7. Code (Map) publicclass Map<Domain, Range> implementsIAlgo<IUnaryOperation<Domain,Range>,Domain,LRStruct<Range>> { publicLRStruct<Range> emptyCase(LRStruct<Domain> host, IUnaryOperation<Domain,Range> arg) { returnnewLRStruct<Range>(); } publicLRStruct<Range> nonEmptyCase(LRStruct<Domain> host, IUnaryOperation<Domain,Range> arg) { returnhost.getRest().execute(this,arg).insertFront(arg.apply(host.getDatum())); } }

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

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

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

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

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

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

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

  15. The IUnaryOperation interface publicinterfaceIUnaryOperation<Domain, Range> { public Range apply(Domainarg); }

  16. Mapping: Domain  Rangef: xf(x) DOMAIN RANGE x f(x)

  17. A concrete operation publicclassStringLenimplementsIUnaryOperation<String,Integer>{ public Integer apply(Stringarg) { returnarg.length(); } } DOMAIN RANGE

  18. A concrete operation publicclass Times2 implementsIUnaryOperation<Integer,Integer> { public Integer apply(Integerarg) { return 2*arg; } public String toString() { return"x -> 2*x"; } } DOMAIN 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 publicinterfaceIBinaryOperation<Domain1,Domain2,Range> { 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 publicinterfaceIBinaryOperation<Domain1,Domain2,Range> { public Range apply(Domain1 arg1, Domain2 arg2); public Range identityElement(); }

More Related