• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Tricky Function

 
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all;)

I have come across a tricky Function (at least for me), that I don't
quite understand...

It's this here:




What is going on here is:

in the Function "func2" the Method that acts up First, is compose(),
after it comes "andThen(). And last but not least func gets to
run, am I right here?

Because of this what I don't understand is the following:

If compose() converts a String to an int-Value, how
can andThen() still compile? As specified here, it does
also expect a String Value (String s) but it gets an int,
because the compose() method converted the String to in
before it reached the anThen() method...

This is what I don't get...or did I oversee something here?

Kind regards:)

Florian
 
Saloon Keeper
Posts: 15491
363
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's incorrect. andThen() is performed first, and then compose() is performed, because compose() is to the right in the method chain.

First you have func, which is a Integer -> String. Then you perform andThen(), which turns it into another Integer -> String. Finally you perform compose(), which turns it into a String -> String.

Why would compose() be executed first?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Florian Jedamzik wrote:What is going on here is:

in the Function "func2" the Method that acts up First, is compose(),
after it comes "andThen(). And last but not least func gets to
run, am I right here?


This is what happens:

In the following line:

1. The method 'andThen' is called on 'func' with a lambda expression as its argument. This returns a value, which is another Function object.
2. The method 'compose' is called on the Function that was returned in step 1. This returns another Function object.

When you then call func2.apply("4") then the following happens:

3. You are calling 'apply' on the Function object that was returned by the 'compose' method in step 2. This method first executes the lambda expression that was passed to it, which is in this case: (String s) -> Integer.parseInt(s) - so the "4" that you pass to apply() is converted to an int.

4. The 'compose' method then calls 'apply' on the Function object that it was called on in step 2 - so it calls 'apply' with the output of the lambda (the integer 4) on the Function object that was returned in step 1, which was created by the 'andThen' method.

5. The Function object returned by the 'andThen' method does this: it first calls the 'apply' of the Function object on which it was called itself, and then it calls the lambda expression that you passed to it on the result of the first 'apply' (so, this is exactly the opposite order as what 'compose' does). So: func.apply() is called with the integer 4. This translates the integer 4 back into a string "4". And then the lambda (String s) -> s+"2" is called, which concatenates strings "4" and "2", returning "42".
 
Florian Jedamzik
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:That's incorrect. andThen() is performed first, and then compose() is performed, because compose() is to the right in the method chain.

First you have func, which is a Integer -> String. Then you perform andThen(), which turns it into another Integer -> String. Finally you perform compose(), which turns it into a String -> String.

Why would compose() be executed first?



Thank you for the Explanation;)

I thought compose() executes first by default every time...didn't think
about the order of my code that much...;)

Kind regards
Florian
 
Florian Jedamzik
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:

Florian Jedamzik wrote:What is going on here is:

in the Function "func2" the Method that acts up First, is compose(),
after it comes "andThen(). And last but not least func gets to
run, am I right here?


This is what happens:

In the following line:

1. The method 'andThen' is called on 'func' with a lambda expression as its argument. This returns a value, which is another Function object.
2. The method 'compose' is called on the Function that was returned in step 1. This returns another Function object.

When you then call func2.apply("4") then the following happens:

3. You are calling 'apply' on the Function object that was returned by the 'compose' method in step 2. This method first executes the lambda expression that was passed to it, which is in this case: (String s) -> Integer.parseInt(s) - so the "4" that you pass to apply() is converted to an int.

4. The 'compose' method then calls 'apply' on the Function object that it was called on in step 2 - so it calls 'apply' with the output of the lambda (the integer 4) on the Function object that was returned in step 1, which was created by the 'andThen' method.

5. The Function object returned by the 'andThen' method does this: it first calls the 'apply' of the Function object on which it was called itself, and then it calls the lambda expression that you passed to it on the result of the first 'apply' (so, this is exactly the opposite order as what 'compose' does). So: func.apply() is called with the integer 4. This translates the integer 4 back into a string "4". And then the lambda (String s) -> s+"2" is called, which concatenates strings "4" and "2", returning "42".




Thank you for the exhaustive and exact explanation Jesper;)
 
Florian Jedamzik
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, to conclude;

1. compose() runs
2. func runs
3. andThen() runs (last)

Kind regards
Flo
 
reply
    Bookmark Topic Watch Topic
  • New Topic