oralee
Uploaded by
22 SLIDES
389 VUES
230LIKES

Map, filter and reduce

DESCRIPTION

This document outlines the fundamental concepts of mapping operations in functional programming. It explains how to apply unary operations to each element of a list, aggregating results in a new list. An example illustrates how to obtain string lengths from a list of strings, detailing the process through conceptual diagrams and recursive approaches. Additionally, it covers the implementation of map functions in code, demonstrating both empty and non-empty cases while maintaining code clarity and efficiency. This guide is essential for anyone looking to grasp functional programming techniques.

1 / 22

Download Presentation
Télécharger la présentation

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
SlideServe
Audio
Live Player
Audio Wave
Play slide audio to activate visualizer