CS 2510 Fundamentals of Computer Science 2 Honors Taken February 24 2014 Best read in an editor that shows Java syntax Function Objects in Java Map OrMap Filter and Generic Types Example 1 Data Representation of the Boston Marathon Class Runner FIELDS String name int bib Note The number on the runner s bib int minutes int age boolean isMan METHODS bool isMan Returns true if the runner is male bool isOlderThan int age Returns true if the runner is older than given age bool underTime int mins Returns true if the runner is faster than given time in minutes We added these methods to FORCEFULLY make filters even though Java doesn t have them built the way Racket does due to its lack of functions These are the predicates or checks we will use to filter out elements in lists of Runners e g Filtering out all male or all female runners from a list However the code for each of the filters is going to be repetitive How can we make one filter that works for any predicate We ll create predicates as classes that implement the interface IRunnerPred Discussed later in these lecture notes ILoRunner Note This is an interface for a List of Runners It includes all methods for such a list Runner findWinner bool mensRace int length ILoRunner sortByTime Implement ILoRunner In other words What classes will use the methods from the interface above What are their fields MtLoR ConsLoR Runner first ILoRunner rest Method Wishlist Things we want to implement 1a find all runners older than 50 1b find all runners finishing in 4 hrs 1c find all runners who are women These would be done with the equivalent of FILTER Filters list based on criteria 2a Is there a runner older than 50 2b Is there a runner finishing in 4 hrs 2c Is there a runner who is a woman Completed with a FOLD given a list runs a given method on elements and combines them returns single Boolean We can also use an ORMAP which the Professor chose to use in this lecture 3a Compute the names of all runners older than 50 3b Compute the names of all runners finishing in 4hrs 3c Compute the names of all runners who are women MAP Takes a List of Runners runs a given method returns List of Names The problem is in functional programming we could pass functions into other functions these were called higher order functions Higher order functions are necessary to use MAP ORMAP FOLD and FILTER How can we create something similar in object oriented Java BUILDING FILTER Predicate on Runner interface IRunnerPred boolean test Runner r Let s create filter Pass in a predicate class our Java version of a higher order function as a parameter Written in the ConsLoR class ILoRunner filter IRunnerPred p if p test this first If the test returns true on the first element keep it and run the filter on the rest of the list return new ConsLoR this first this rest filter p else Else remove it and just return the result of filter on the rest of the list return this rest filter p In MtLoR ILoRunner filter IRunnerPred p Nothing to filter out so just returns an empty list return this Here s an example predicate class aka test object that you might pass in class CheckWomen implements IRunnerPred public boolean test Runner r return r isMan false Here s an ExampleMarathon with example runners It shows how testing would work class ExampleMarathon Note We did not write the Runner class out in lecture We assumed it had been written With a constructor that sets its fields in the order they were listed at the beginning of the file We put in random numbers in class for the constructor Runner alice new Runner Alice 4 243 60 false Runner bob new Runner Bob 17 196 43 true Runner cheryl new Runner Cheryl 2 200 35 false Runner dave new Runner Dave 10 182 26 true Example list of Runners ILoRunner roster new ConsLoR this alice new ConsLoR this bob new ConsLoR this cheryl new ConsLoR this dave new MtLoR A test for an example filter boolean testFilter Tester t return t checkExpect new CheckWomen is the predicate object our equivalent of a Higher order function this roster filter new CheckWomen This is the result we expect when the above is run new ConsLoR this alice new ConsLoR this cheryl new MtLoR Here s another potential predicate object that checks if a Runner finished the race under the given time in minutes If you passed this into filter it would return only the racers who finished under the time whereas the one above returned a list of women class TimeUnder implements IRunnerPred int time TimeUnder int t this time t public boolean test Runner r return r minutes this time That s how filter is done OR MAP The predicate objects we created above can also be used to write an ormap runs a test on all elements in the list runs OR accross the booleans returns a single boolean as a result boolean ormap IRunnerPred p return p test this first this rest ormap p MAP Map uses other types of functions on lists not just predicates So we want to create FUNCTION OBJECTS to pass in This will be similar to the process we did to create predicate objects Functions convert one value into another How can we start to make these These are ideas for interfaces that convert Runners into other values By convention we say the method for a function object is apply IRunner2String String apply Runner r IRunner2Number int apply Runner r IRunner2Bool bool apply Runner r Seems repetitive doesn t it Let s take the repeated parts factor them out and make them into a parameter This is called ABSTRACTION In this case for all of the IRunner2 s the only difference is the TYPES they convert a Runner to We re can make that a Type Parameter called T T must be specified when these types of objects are created Professor Lerner demonstrated this in detail during future lectures Converts a Runner to another type T interface IRunner2T T T apply Runner r We can make these functions even more general so that they convert from anything to anything This is a function that converts object of type S to objects of type T S source T Target This is for unary functions only have one parameter argument interface IFunction S T T apply S s Angle brackets must be used when you first say you re going to use TYPE PARAMETERS aka GENERIC TYPES Only classes or object types can be parameterized types Primitives int bool lowercase things can t be used So if I wanted to make an IFunction from Runners to booleans IFunction Runner boolean would NOT WORK IFunction Runner Boolean would work Capital B Boolean is an object IFunction Runner int doesn t work
View Full Document