Skip to content

bowbahdoe/forever

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Forever

JUnit PMD Checkstyle

😊 Fluent 😊, ✨Modern ✨, and 🚀 Blazing Fast 🚀endless loops, made easy.

Requires Java 22 or better.

Get it

implementation('dev.mccue:forever:2024.07.09')

Tutorial

Let's build a game where the computer gives words of encouragement to the user. To start, we'll make a small little program that asks for their name.

void main() {
    var scanner = new Scanner(System.in);
    
    System.out.print("What's your name?: ");
    var name = scanner.nextLine();
    System.out.println("You're doing a great job " + name + "!");
}

But whoopsie! It only asks them for their name and gives positive feedback once! We want that to go on forever!!

So let's import the library and get this train going.

import dev.mccue.forever.For;

void main() {
    var scanner = new Scanner(System.in);

    for (; For.ever ;) {
        System.out.print("What's your name?: ");
        var name = scanner.nextLine();
        System.out.println("You're doing a great job " + name + "!");
    }
}

And now we will run that code for forever!

For those who love terse code (and you should, less code is best code 😊) you can leave off one of the fors.

import static dev.mccue.forever.For.ever;

void main() {
    var scanner = new Scanner(System.in);

    for (;ever;) {
        System.out.print("What's your name?: ");
        var name = scanner.nextLine();
        System.out.println("You're doing a great job " + name + "!");
    }
}

But we can do even better 💖. In 2014, Java SE 8 saw the introduction of the concept of lambda expressions. Lambda expressions were a powerful addition to the Java language starting in Java 8.

import dev.mccue.forever.For;

void main() {
    var scanner = new Scanner(System.in);

    For.ever(() -> {
        System.out.print("What's your name?: ");
        var name = scanner.nextLine();
        System.out.println("You're doing a great job " + name + "!");
    });
}

Get Help

Get Involved

Stay Informed

About

Utility for running a bit of code "forever"

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages