Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add d3.linspace. #150

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Add d3.linspace. #150

wants to merge 3 commits into from

Conversation

mbostock
Copy link
Member

Related #90.

@mbostock mbostock requested a review from Fil June 11, 2020 16:11
@mbostock
Copy link
Member Author

Maybe worth adding logspace and geomspace too, while we’re here?

@Fil
Copy link
Member

Fil commented Jun 11, 2020

I can see how it would be useful, esp. if it returned a Float64Array (or had some option like dtype), and satisfy #90 at the same time.

Not sure about floor(n), since it could be nice to continuously change n (in the case where * endpoint* is false). [stick to the numpy def]

For logspace and geomspace it looks like there is no particular algorithmic challenge and not much use. I wouldn't want to add weight (bytes and documentation and cognitive load) just for the sake of it :)

@curran
Copy link

curran commented Jun 11, 2020

Very nice!

@Fil Fil added the feature label Jul 10, 2020
@curran
Copy link

curran commented Aug 8, 2020

This looks like a nice PR. Could be merged I suppose.

@mbostock
Copy link
Member Author

mbostock commented Jul 3, 2022

I don’t think the positional arguments with defaults are appropriate here because they aren’t analogous to Python’s named arguments. For example in Python you would say stuff like

np.linspace(2.0, 3.0, 5)
np.linspace(2.0, 3.0, num=5)
np.linspace(2.0, 3.0, num=5, endpoint=False)
np.linspace(2.0, 3.0, endpoint=False)

and the meaning is fairly clear. However in JavaScript it would be

d3.linspace(2, 3, 5)
d3.linspace(2, 3, 5)
d3.linspace(2, 3, 5, false)
d3.linspace(2, 3, undefined, false)

which I think is much less obvious. So I wonder if instead we should do something like:

d3.linspace(2, 3, 5)
d3.linspace(2, 3, {num: 5})
d3.linspace(2, 3, {num: 5, endpoint: false})
d3.linspace(2, 3, {endpoint: false})

This would also allow us to create typed arrays:

d3.linspace(2, 3, {num: 5, type: Float64Array})

I also don’t really like feeling stuck with Python’s names for these options. I think I’d prefer n or length instead of num. I’m using type instead of dtype because in JavaScript it would be the type of the array rather than the type of the value. (And I thought about inclusive or exclusive instead of endpoint but I couldn’t make up my mind.)

And lastly I wonder if there should be more symmetry between d3.range and d3.linspace. Like, what about:

d3.range(2, 3, {length: 5, inclusive: true})

Also this feels fairly low priority and ambiguous so I’m not very motivated to think about this too much. 🤷 Sorry!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants