Coder Social home page Coder Social logo

java8-functional-playpen's Introduction

java8-functional-playpen

I experiment my Java 8 functional adventures here

CircleCI

TernaryDriver

This is to scratch an itch I had. With Java, I can do this:

String name = (hasLastName)
	? createFullName()
	: useFirstName();

Only one expression is allowed for each of those. There is no way to in-line more than one lines of code. I will have to extract that as method and to do that. Sometimes I don't want to do that.

So here is my attempt:

TernaryDriver<String> underTest = TernaryDriver.<String> builder()
	.whenTrue(() -> {
		// lots of code
		return "Hello World!"
	})
	.whenFalse(() -> {
		// lots of code
		return "Goodbye World!"
	})
	.build();

String response = underTest.apply(() -> "ABC".length() == 1); // response == "Goodbye World!"

Itch scratched. This also makes the code block reusable.

SwitchDriver

Functional way of creating switch.

Example:

	SwitchDriver<String, Integer> underTest = SwitchDriver.<String, Integer> builder()
		.defaultClause(word -> word.length())
		.addCase(word -> word.equals("BCD"), word -> word.length() + 10)
		.addCase(word -> word.equals("CDE"), word -> word.length() + 100)
		.build();
	
	//Use a supplier to pass a value to SwitchDriver
	underTest.apply(() -> "ABC").intValue();//returns 3
	
	//Or, use a direct value to SwitchDriver
	underTest.apply("ABC").intValue();//returns 3

IfDriver

Coming up!

java8-functional-playpen's People

Contributors

wahidsadik avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.