Giter VIP home page Giter VIP logo

array-subindex's People

Contributors

bitdeli-chef avatar xunker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

array-subindex's Issues

Do something better with ranges and lengths

Array indexes can be ranges, or can be integers passed with a length.

Right now the gem handles these in the same way that the base Array class does:

[1,2,3][0..1]
# => [1,2]

[1,2,3][1,2]
# => [2,3]

That's all well and good, but they should do something... better. But what?

Ruby (2.0.0 at least) supports Ranges with Float boundaries. I'd like to see this:

[1,2,3,4][0.5..2.5]
# => [1.5, 2.5, 3.5]

%w[foo bar baz qux][0.5..2.5]
# => ["oob", "arb", "azq"]

That's easy. That's approach would also work verbatin with a length argument with the array index. But what about irregular ranges like [0.5..10.9]? What would the stepping be?

Non-half fractional indexing of numbers seems discontinuous

I noticed the following in the docs:

[1,2,3][0.25] == 1.75
#1/4 of index 0 + 3/4 of index 1

I would have expected the output values to be more continuous, and to follow the following pattern:

[1,2,3][0.25] == 1.25
#3/4 of index 0 + 1/4 of index 1

That is to say, the limit of #[] as the index approaches n from above where n is an integer should be #[n], not #[n+1]. Similarly, the limit of #[] as the index approaches n from below should be #[n], not #[n-1].

Similarly, I would not have expected the following:

[1,2,3][1.001] == 2.999

I would have instead expected:

[1,2,3][1.001] = 2.001

Now it's TLDR time!

I think it's fair to say that linear interpolation is a reasonable assumption. I wouldn't necessarily expect it to figure out the following:

[1,4,9][1.5] = 6.25

Especially since fitting functions always makes assumptions, and while it is easy for us to assume the following is an exponential function, one could also make the case that it's just a funky 3rd order polynomial and get a different answer.

[1,2,4,8][1.5] = 2.8284

pry & no to_f method for range

[1] pry(main)> require 'array/subindex'
NoMethodError: undefined method to_f' for 0..-1:Range from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/array-subindex-1.2.0/lib/array/subindex.rb:5:in[]'
[2] pry(main)>
NoMethodError: undefined method to_f' for 0..-1:Range from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/array-subindex-1.2.0/lib/array/subindex.rb:5:in[]'
[2] pry(main)> puts 2
NoMethodError: undefined method to_f' for 0..-1:Range from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/array-subindex-1.2.0/lib/array/subindex.rb:5:in[]'

Everything I press it shows this error. IRB/ruby(as ruby file.rb) works fine.

Handle infinite, endless and beginless ranges

We can do endless ranges in Ruby < 2.6 with (0..Float::INFINITY), and in Ruby >= 2.6 with (0..) or (0..nil).

Also, in Ruby 2.7 we now have beginless ranges: [..100]

These may already work, because they can be worked with as an array, but we need to add tests for that.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.