deadinsi.de is part of the decentralized social network powered by Mastodon.

Administered by:

Server stats:

60
active users

Learn more

Programming, beginner

What I'll do for the first three hours of the day, is have coffee and do this practice.
pastebin.com/raw/59Uqyz0h
This is the control, it's a sequence of timed events with one indefinite loop and one conditional branch.

I'll have to do some async version in Java, and then a promise-based version in JavaScript, then maybe an alternative for either. I'm thinking wrapping the blocking-wait engine inside one future/promise so that I can simply wait for the whole thing to finish before "unlocking the UI"

Programming, Java

Trying to do this with java.util.concurrent raises the difficulty 8x lol.

The loop part is really messing me up, because I cannot have a future that returns a future (to recursively call )

Programming, Java

Gonna shave first and make some coffee

I'm doing something wrong by blockingly waiting for futures anyways (implies the current thread can block, in which case I might as well time-based sleep). I'm supposed to schedule a chain of events

snowyfox

Programming, Java

This one technically works, but it's obviously wrong. It's basically a contiguous block of code that split themselves into Runnables so that they can use ScheduledExecutorService as a delaying mechanism. It's better off just plopping the contiguous block in its own thread or a giant Runnable.

I wanted to follow this one up with one that uses return values and Callables. But given the advice not to ever mix blocking await and constructs like futures and promises... I also feel it's wrong, what I /want/ to do is specify a whole schedule up front, each step only concerned with fulfilling something, and where to route upon success or failure, with delays, and then the executor just goes and does all of it. Maybe I'm willing to put delays within the steps if there's no feature in the executors to facilitate that, it's just all threads down there anyways

1/2

Programming, beginner

I think what I'm /supposed/ to do is, if I want to define a schedule as a series of steps with results, then I create a bunch of primitives that accepts a result and outputs a result. For JavaScript that'd be functions directly, for Java that'd be tougher as java.util.concurrent demands at a mininum breaking out into Runnable/Callable, and at worse FutureTasks and all that stuff.

Once I make sure I have the conformant primitives, there should be a chaining mechanism somewhere, I shouldn't try to implement the chaining myself through looping or internally pushing the next step to the scheduler

2/2

Programming, Java

I know that this is one of the places, if not the only place, that Java has for chaining things.
java.util.concurrent.CompletableFuture
docs.oracle.com/javase/8/docs/

Interestingly they distinguish between a completable future and a future. I would think every future is finite, but maybe they think there's some tasks that vanish to whatever or run infinitely.

Programming, Java

And like, the way they declare "a primitive that accepts a result" is Consumer.

I can already tell that, my Masto client, I can replace the Image field with a CompletableFuture<Image>, that the ImageFetcher provides a Runnable for fetching into, then the windows can do (Consumer<Image> themselves). Without me needing to create a listener list or whatever, for the ImageFetcher Runnable to manually go through and notify.

Programming, Java

Okay, two more implementations, then I'll port those to JavaScript.

Programming, Java

There, this one is correct.
pastebin.com/raw/G6q0XrmG
No optional or externally-defined delay, which would need excessively fine breaking up, but generally a schedule I created separately and can run async.

Programming, Java

pastebin.com/raw/e7TUD6b8
Schedule iteration is surprisingly convoluted. When you have a case like mine, where a loop is needed, and steps have different ending delays based on a condition, you need to break things up a bit more and add either to the complexity of the schedule spec or weird variables to control iteration.

When it's not trivial, it really is far better to just sleep manually or to do what I did in C3.

I won't bother with this in JavaScript then, I'll do one final exercise using promises, then stop for today.

Programming, JavaScript, beginner

Okay, I think I got a hang of it. And the difference between a function directly as the argument for versus a lambda that returns a new Promise of that function
pastebin.com/raw/ACe9XnUQ