stdlib-js / stdlib

✨ Standard library for JavaScript and Node.js. ✨
https://stdlib.io
Apache License 2.0
4.38k stars 447 forks source link

Pretty print ndarrays when displayed as a return value in the stdlib REPL #368

Open kgryte opened 3 years ago

kgryte commented 3 years ago

Checklist

Please ensure the following tasks are completed before filing a bug report.

Description

Description of the issue.

Encountered the issue when creating an ndarray in the REPL. Currently, when displaying an ndarray as a return value, the following is shown:

In [1]: var x = array( [[1,1],[1,1]])
Out[1]: ndarray {
  _byteLength: 32,
  _buffer: Float64Array(4) [ 1, 1, 1, 1 ],
  _length: 4,
  _offset: 0,
  _order: 'row-major',
  _shape: [ 2, 2 ],
  _strides: [ 2, 1 ],
  _iterationOrder: 1,
  _flags: { ROW_MAJOR_CONTIGUOUS: true, COLUMN_MAJOR_CONTIGUOUS: false }
}

Preferably, the REPL would not display "private" (underscore prefixed properties). These properties seem to be inherited from the base ndarray class. They are not set as non-enumerable properties for performance reasons.

When setting as non-enumerable in the non-"base" ndarray constructor, there was a non-negligible performance impact which subsequently affected downstream usage of ndarrays. This was documented here.

In general, return value representation should be standardized across the stdlib REPL. Possibly by implementing a custom inspect method which is invoked prior to displaying the return value in the REPL, similar to the built-in Node.js REPL.

Related Issues

Does this issue have any related issues?

No.

Questions

Any questions for reviewers?

No.

Other

Any other information relevant to this issue? This may include screenshots, references, stack traces, sample output, and/or implementation notes.

Reproduction

What steps are required to reproduce the unexpected output?

In order to reproduce this bug, do the following:

Expected Results

What are the expected results?

The following results are expected:

<ndarray>[ <contents> ]

or something like what is generated by ndarray#toString().

Actual Results

What are the actual results?

The following are the actual results:

(see above)

Environments

What environments are affected (e.g., Node v0.4.x, Chrome, IE 11)? If Node.js, include the npm version, operating system, and any other potentially relevant platform information.

The following environments are affected:

vexora-0 commented 2 months ago

hey @kgryte I would like to work in this, could you assign me this

kgryte commented 2 months ago

@vexora-0 We don't assign issues, but you are free to submit a PR.

vexora-0 commented 2 months ago

oh sure, Ill try