Giter Club home page Giter Club logo

ranges.jl's Introduction

Some ideas about ranges of integers in Julia. The package provides half-open ranges:

RightOpen(a, b)          # [a → b)
Reverse(RightOpen(a, b)) # [a ← b)
LeftOpen(a, b)           # (a → b]
Reverse(LeftOpen(a, b))  # (a ← b]

and some helper functions a : until : b and b : downuntil : a.

For example:

> using Ranges
> for i = 0 : until : 5
    print(i, ' ')
  end
0 1 2 3 4
> for i = LeftOpen(0, 5)
    print(i, ' ')
  end
1 2 3 4 5
> for i = rev(RightOpen(0, 5))
    print(i, ' ')
  end
4 3 2 1 0
> for i = 5 : downuntil : 0
    print(i, ' ')
  end
5 4 3 2 1

It solves a couple problems in the native range a:b-1 of Julia:

  1. No overflow of length(a : until : b) by using UInts.
    > length(-10:typemax(Int))
    ERROR: OverflowError()
    vs
    > length(-10 : until : typemax(Int))
    0x8000000000000009
  2. Iterate over unsigned integers in reverse order.
    > for i = 0x03:-1:0x00
        print(i, ' ')
      end
    ERROR: InexactError()
    vs
    > for i = rev(0x00 : until : 0x04)
        print(i, ' ')
      end
    3 2 1 0
  3. Ability to construct an empty range 0x00 : until : 0x00.

At the moment one cannot iterate all the way from typemin(IntT) to typemax(IntT) inclusive; but this issue is best fixed with the new iterator protocol in Julia 1.0 combined which makes implementing a ClosedRange type a breeze.

ranges.jl's People

Contributors

haampie avatar

Watchers

 avatar James Cloos avatar  avatar

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.