Skip to content

Implement Runnable concept #11

@bsutton

Description

@bsutton

In addition to the ideas below:

The one problem with the below concept is that if you write:
find('*.jpg')
nothing will happen.
Consider having the 'find' command return a 'Runnable' object.
The Runnable object contains a future with a 500ms delay.
If the runnable isn't executed in 500ms then the future outputs a warning to the console
telling the user that the forgot to place a verb after the find.
This is likely to be dumped out in the midst of other code but given the importance I think this is ok.
Will need to include a stacktrace with line no. give the above noted disassociation with the original find line.

I've been struggling with the find function

find('*.jpg').forEach((file) => print(file));

The above syntax is elegant but it has a fundamental problem.

The mandate for dshell is that all builtin functions are to be sync so users don't have to worry about futures.

This causes a problem.

In the above find example the order of execution is:

find - runs to completion and finds all files

returns a ForEach object

ForEach.forEach runs and prints all the files.

What this means is that you don't get to see any output until the find completes and all of the files found are stored in a stream consuming memory which could exhaust heap.

So I had this neat idea.

We have a class of functions that (for what of a better term) I refer to as runnables.

Runnables don't run.

Rather the require a 'verb' to run.

So the verbs would be:

forEach(...)

run

print

asList - potential to cause memory issues but that would be the users decision.

So if you write:

find('*.jpg')

nothing happens, the find doesn't run because its a runnable and you must provide a verb

if you now write:

find('*.jpg').print

The find command creates a runnable, returns it and the 'print' verb is executed on the runnable.

The runnable can now output the file names as the find command runs rather than waiting for the find command to complete.

We now get progressive output and eliminate our heap problems.

So now when you write:

find('*.jpg').forEach((file) => print(file));

The file names are printed as you go.

What do you think of the syntax?

Is it confusing that writing:

find('*.jpg')

does nothing?

Currently there would only a small no. of runnables:

head

cat

read

find

and of course the existing string runnables:

'tail -f /var/log/syslog'.run

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions