question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide a cleaner way to transform Ranges

See original GitHub issue

I have several use cases that involve transforming the values held by Ranges and RangeSets. In particular, I often have a Range<LocalDate> and need a Range<Integer> (where the latter represents a range of years) or the inverse. While it’s possible to manually extract the endpoints and construct a new Range, ideally there would be some cleaner standard way to do this.

I currently have several Function generator methods that I use which works reasonably well. Hopefully this example will help trigger discussion, I’m happy to contribute the backing code if this pattern in fact seems desirable.

/**
 * Returns a function which transforms the endpoints of a range using the passed function.
 * Take note that losses of precision can change ranges, e.g. [1.2..1.9) => [1..1)
 * becomes an empty range.
 */
public static <S extends Comparable<? super S>, E extends Comparable<? super E>>
    Function<Range<S>, Range<E>> rangeTransformer(Function<S,E> f)

/**
 * Returns a function which canonicalizes a range, then transforms the endpoints using
 * the passed function.  Useful for broadening ranges, e.g. [1..2] => [1..3) => [1.0..3.0),
 * which is difficult to define in non-canonical form.
 */
public static <S extends Comparable<? super S>, E extends Comparable<? super E>>
    Function<Range<S>, Range<E>>
    canonicalizedRangeTransformer(Function<S,E> f, DiscreteDomain<S> domain) 

/**
 * Transforms a RangeSet to use a different type of Range.  Intended to be used in
 * conjunction with the rangeTransformer() methods.
 */
public static <S extends Comparable<? super E>, E extends Comparable<? super E>>
    Function<RangeSet<S>, RangeSet<E>> rangeSetTransformer(Function<Range<S>,Range<E>> f)

This can then be used to (reasonably) concisely define a transformation on Ranges and RangeSets, without needing to worry about as much boiler-plate.

public static final Function<Range<Integer>, Range<LocalDate>> YEAR_RANGE_TO_LD_RANGE = 
  canonicalizedRangeTransformer(new Function<Integer, LocalDate>() {
    @Override
    public LocalDate apply(Integer year) {
      // By canonicalizing first, we can safely always return Jan 1st
      // The range [2000..2002] becomes [2000-1-1..2003-1-1) which is equivalent.
      return new LocalDate(year, 1, 1);
    }},
    DiscreteDomain.integers());

public static final Function<RangeSet<Integer>, RangeSet<LocalDate>> YEAR_RANGESET_TO_LD_RANGESET =
  rangeSetTransformer(YEAR_RANGE_TO_LD_RANGE);

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cpovirkcommented, Dec 9, 2014

(One thing that I took for granted in my previous post: My guess is that we would provide Range<B> transform(Range<A>, function) rather than Function<Range<A>, Range<B>> transformer(function). The good news is that Java 8 will make constructing the latter simpler: range -> transform(range, function.)

0reactions
Kurrucommented, Aug 10, 2021

Any update on this? Would still be a great enhancement for manipulating Ranges

Read more comments on GitHub >

github_iconTop Results From Across the Web

As a marketplace cleaner, how do I change my range or ...
If you have joined the marketplace and want to change your current location or range, you can do this in your settings in...
Read more >
Top ten ways to clean your data - Microsoft Support
The basics of cleaning your data · Insert a new column (B) next to the original column (A) that needs cleaning. · Add...
Read more >
The Do's and Don'ts of Range Cleaning - Vulcan Equipment
Cleaning your electric or gas range not only keeps your investment running optimally, but serves as an invaluable preventative maintenance ...
Read more >
Should I Choose A Range with Steam or Self Cleaning?
Self-cleaning and steam-cleaning ovens make cleanup sweet and to the point. Learn more in this side-by-side comparison!
Read more >
8 Best Stovetop Cleaners 2022 - Top Cooktop Cleaning ...
You don't have to do much cooking to know how quickly a stovetop goes from clean to caked-on. Fry a little bacon or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found