Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

A tomato pasta recipe (in programming languages)

cli345

Coder
Hi, I've already told you about FuncSug :)
Here is a tomato pasta recipe.

In a recipe, there are actions done together with other actions. For example, the sauce is prepared while the pasta is cooking.

I suggest to code it in your favorite language and give me your opinion about my code.

Code:
parallel ||
    heatWaterToBoilingPoint()
    putPastaIntoWater()
    waitSeconds(10*60)
    turnOffTheWaterHeat()
||
    turnOnTheOilHeat()
    putGarlicIntoOil()
    putTomatoesIntoOil()
    putBasilIntoOil()
    waitSeconds(3*60)
    turnOffTheOilHeat()
transferPastaFromWaterPanToOilPan()
season()
putCheese()

What is your version?
 
Originally Java didn't have anything so neat BUT the latest Java release includes new threading classes that work in much the same way. A StructuredTaskScope lets you fork multiple threads then wait for them all to complete before continuing
Java:
        var scope = new StructuredTaskScope();
        scope.fork(() -> {
            heatWaterToBoilingPoint();
            putPastaIntoWater();
            waitSeconds(10 * 60);
            turnOffTheWaterHeat();
        });
        scope.fork(() -> {
            turnOnTheOilHeat();
            putGarlicIntoOil();
            putTomatoesIntoOil();
            putBasilIntoOil();
            waitSeconds(3 * 60);
            turnOffTheOilHeat();
        });
        scope.join(); // wait for all forks to complete
        transferPastaFromWaterPanToOilPan();
        season();
        putCheese();

of course, being Java there's also a ton of stuff fo handing errors, timeouts etc
 
Last edited:
Thank you for your feedback :).
Yes, this is included in structured concurrency which, nowadays, is available for more and more programming languages (see this list). That's very good indeed :).
Few programming languages include structured concurrency natively: Apart from Java and Swift, I don't know of any others.
 
I could also have written:

Code:
parallel ||
    @heatWater_branch
    heatWater()
||
    waitBoiling()
    putPastaIntoWater()
    waitSeconds(10*60)
    break heatWater_branch
||
    @heatOil_branch
    heatOil()
||
    putGarlicIntoOil()
    putTomatoesIntoOil()
    putBasilIntoOil()
    waitSeconds(3*60)
    break heatOil_branch
transferPastaFromWaterPanToOilPan()
season()
putCheese()

This time, I need to be able to:
  • name a branch (example: @heatWater_branch),
  • interrupt (example: break heatWater_branch) a named branch from another branch.

What is your version?
 
Java:
The fork method returns an instance of Future that you can use to check its status, get any returned results, cancel it etc eg



Java:
var hwBranch = scope.fork( …
…
    hwBranch.cancel(true);
 
Yes, that's very good! :)

Now more detailed (waitBoiling and heatWater are explicitized):

Note: waitBoiling() is coded: awaitBool (waterTemperature = 100)

Code:
var waterTemperature := 20

def heatWater():
    while true:
        waitSeconds(2)
        if waterTemperature < 100:
            waterTemperature += 1

parallel ||
    @heatWater_branch
    heatWater()
||
    # --> waitBoiling() is explicitized
    awaitBool (waterTemperature = 100)
    
    putPastaIntoWater()
    waitSeconds(10*60)
    break heatWater_branch
||
    @heatOil_branch
    heatOil()
||
    putGarlicIntoOil()
    putTomatoesIntoOil()
    putBasilIntoOil()
    waitSeconds(3*60)
    break heatOil_branch
transferPastaFromWaterPanToOilPan()
season()
putCheese()

What is your version?
 

New Threads

Buy us a coffee!

Back
Top Bottom