Solved

Java Lambda expression help

Posted on 2016-07-25
6
59 Views
Last Modified: 2016-07-26
Hello there,

I am going through a tutorial on Vert.x and came across this lambda code which I want to replace it with ordinary code i.e. without using lambda expression. Can somebody please help me to replace this lambda expression so i know what is going on

public class MyFirstVerticle extends AbstractVerticle
{
	@Override
	public void start(Future<Void> fut)
	{
		vertx.createHttpServer().requestHandler(r -> {
			r.response().end("<h1>Hello from my first " + "Vert.x 3 application</h1>");
		}).listen(8080, result -> {
			if (result.succeeded())
			{
				fut.complete();
			} else
			{
				fut.fail(result.cause());
			}
		});
	}
}

Open in new window

0
Comment
Question by:zolf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 36

Accepted Solution

by:
mccarl earned 300 total points
ID: 41728836
Something like this.... (untested but should give you the idea)

public class MyFirstVerticle extends AbstractVerticle
{
	@Override
	public void start(Future<Void> fut)
	{
		vertx.createHttpServer().requestHandler(r -> {
			r.response().end("<h1>Hello from my first " + "Vert.x 3 application</h1>");
		}).listen(8080, new Handler<AsyncResult<HttpServer>>() {
                     public void handle(AsyncResult<HttpServer> result) {
			if (result.succeeded())
			{
				fut.complete();
			} else
			{
				fut.fail(result.cause());
			}
                    }
		});
	}
}

Open in new window

0
 

Author Comment

by:zolf
ID: 41728927
thanks for your comments, what about the r parameter.can you also please tell me what that r is
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 41728950
I think you might find it more like this (again untested)

        HttpServer server = vertx.createHttpServer();
        server.listen(8080,
            new Handler<AsyncResult<HttpServer>>() {
                public void handle(AsyncResult<HttpServer> result) {
                    if (result.succeeded()) {
                        fut.complete();
                    } else {
                        fut.fail(result.cause());
                    }
                }
            });
    }

Open in new window

Has to be said though - if the examples use FP then if you find you're having to translate things back to 'old style' code then you're probably using the wrong API
0
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 
LVL 36

Assisted Solution

by:mccarl
mccarl earned 300 total points
ID: 41728961
Oops, sorry I missed that one, although it shouldn't be too hard to apply similar logic to what I did the first time...

public class MyFirstVerticle extends AbstractVerticle
{
	@Override
	public void start(Future<Void> fut)
	{
		vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
                    public void handle(HttpServerRequest r) {
			r.response().end("<h1>Hello from my first " + "Vert.x 3 application</h1>");
		}}).listen(8080, new Handler<AsyncResult<HttpServer>>() {
                     public void handle(AsyncResult<HttpServer> result) {
			if (result.succeeded())
			{
				fut.complete();
			} else
			{
				fut.fail(result.cause());
			}
                    }
		});
	}
}

Open in new window

0
 

Author Closing Comment

by:zolf
ID: 41729002
cheers
0
 
LVL 36

Expert Comment

by:mccarl
ID: 41729516
You're welcome
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question