xunker / array-subindex Goto Github PK
View Code? Open in Web Editor NEWUse floats as array indexes in Ruby: [ 1, 2 ][0.5] == 1.5 (Does your brain hurt yet?)
License: MIT License
Use floats as array indexes in Ruby: [ 1, 2 ][0.5] == 1.5 (Does your brain hurt yet?)
License: MIT License
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?
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
[1] pry(main)> require 'array/subindex'
NoMethodError: undefined methodto_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 methodto_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 methodto_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.
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.