This course will be retired on July 14, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Introduction to Functional Programming!
You have completed Introduction to Functional Programming!
Preview
Let's review the remaining basic function shapes, Function and Predicate.
Learn more
Function Shapes
Shape | Functional method signature |
---|---|
Consumer | void accept(T t) |
Supplier | T get() |
Function | R apply(T t) |
Predicate | boolean test(T t) |
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Okay, so we've got to look at consumer and
supplier.
0:00
So next up in our list of
important function shapes
0:04
is the root of all of these, function.
0:08
Now, it gets a little bit weird
when we start looking at this.
0:11
So this represents a function that accepts
a one argument and produces a result.
0:14
So we're gonna click into here and
this is a function functional interface.
0:18
It gets a little weird talking about it.
0:24
Don't worry, I didn't pick up a stutter.
0:25
So, a function takes a single argument and
produces a result.
0:27
And both the parameter type and
the return value are specified, right?
0:33
That's what the T and the R are.
0:38
So T is the type that's passed in and
R is the result type.
0:40
So let's swing down.
0:44
Let's take a look at the single
abstract method and it's called apply.
0:46
So again, it accepts a T and
it returns an R.
0:50
So, when you define the function,
you specify what type it expects and
0:54
then the type of the return value.
0:59
So as you can imagine, these are great for
1:01
transforming values from one to
another and going between types.
1:03
They both can be strings but
you can switch between types.
1:08
We'll get our feet wet with these here
in a bit when we explore streams.
1:12
Another thing that I wanted to point out
here in the docs, while we're here.
1:15
There are these things called default
methods which are new to java eight
1:19
interfaces and they allow you to
change several functions together.
1:24
So if we click here, if we click this
default methods, we'll see that you can go
1:27
on every function that has this
method that says andThen and
1:31
you can pass in a function okay.
1:34
So you can kind of chain several
of these functions together.
1:36
You call your function and
then call another and then another and
1:40
it just sort of goes flowing between them.
1:43
This is called functional composition and
as you guessed, I'm gonna go ahead and
1:45
park that right now.
1:50
Let's flip over and let's write that here.
1:51
We'll write functional composition.
1:54
We are getting a pretty healthy looking
list for the parking lot there.
1:57
Can't wait to go over this stuff, but
we'll get to it in a bit, don't worry.
2:01
So about these default methods,
2:06
check the teacher's notes if it needs
some more information about that.
2:07
Other super powerful and
2:11
there's a couple of these interfaces that
have some pretty handy default methods.
2:12
Okay and last but definitely not least,
let's check out Predicate.
2:18
Predicate is our shape here,
2:25
and it represents a Predicate, thank you,
boolean-valued function of one argument.
2:27
So what happens here is if we look at
this abstract method, it has an abstract
2:34
method of name test and it will take
a type t and always return true or
2:39
false so it's really kind of a specific
type of function right, of the function,
2:44
functional interface so it will take
a type and return a true or false.
2:49
Pretty nice, right?
2:55
It's very specific.
2:55
So, just like functions,
it has a default method and
2:57
you can kind of chain these together.
3:00
So you can say, this and this or not this,
so you can negate it or do an or.
3:02
We'll get our hands wet with
these real soon as we use these
3:08
often when trying to filter values.
3:12
The java.util.function package has
some wonderful functional interfaces
3:14
that we can lean on when diving
deeper into functional programming.
3:19
I'm glad you got a chance to see how
to explore the out of the box options.
3:22
Let's review the four basic
shapes that I'd like you to be
3:26
familiar with real quick.
3:29
Consumer, a function that accepts
something and returns nothing.
3:30
These are useful when you need to
be provided a value in context.
3:35
Supplier, this accepts nothing but
returns a value.
3:39
This is great when you need something
initially, like we did in our yell method.
3:43
Function, this takes a single argument,
and returns a value.
3:47
This is great for transformation.
3:51
And Predicate accepts a parameter, and
3:54
returns a boolean value,
helpful when filtering value.
3:56
You probably noticed that the remaining
functional interfaces in the package were
4:00
either a typed version of those four or
4:05
a version that changes the amount of
parameters that the function accepts.
4:07
The amount of parameters that a function
accepts is referred to as parity.
4:11
If it helps, I remember the bi prefix
like the bicycle, like two wheels,
4:15
like this bike here, two wheels.
4:19
Now, that is versus the uni prefix
like unicycle which means one wheel.
4:21
So now, we've got our basic function
shapes, we are ready to move on to
4:26
the next level of functional programming
in Java, the super powerful streams API.
4:30
Let's go plug in those shapes.
4:35
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up