ranges/ranges.go
func (l *InclusiveRanges) normalized(invert bool) *InclusiveRanges {
	normalized := &InclusiveRanges{}

	var (
		start     int
		end       int
		step      int
		current   int
		pending   int
		keepValue bool
	)

	totalRange := NewInclusiveRange(l.Min(), l.Max(), 1)

	checkValue := l.Contains
	if !invert {
		checkValue = func(value int) bool {
			return !l.Contains(value)
		}
	}

	for it := totalRange.IterValues(); !it.IsDone(); {

		current = it.Next()

		keepValue = checkValue(current)

		// Short-circuit if we encounter a value that
		// is not in the original sequence.
		if keepValue {

			// fmt.Println("  Existing value")

			// If we haven't accumulated 2+ values to
			// add, just continue now and keep trying
			if pending < 2 {
				step++
				// fmt.Println("  Increasing step to:", step)
				continue
			}

			// If the step has changed from what we have
			// already accumulated, then add what we have
			// and start a new range.
			if (current + 1 - end) != step {
				// fmt.Println("  Step changed. Adding range:", start, end, step)
				normalized.Append(start, end, step)
				step = 1
				start = current
				pending = 0
			}

			continue
		}

		// fmt.Println("  Unique value")

		// If the value we want to keep is a different
		// stepping from the pending values, add what
		// we have and start a new range again.
		if pending >= 2 && current-end != step {
			// fmt.Println("  Step changed. Adding range:", start, end, step)
			normalized.Append(start, end, step)
			pending = 0
		}

		end = current

		// Start a new range
		if pending == 0 {
			// fmt.Println("  Starting new range")
			start = end
			step = 1
		}

		pending++
		continue
	}

	// Flush the remaining values
	if pending > 0 {
		// fmt.Println("  Flushing and adding remaining range:", start, end, step)
		normalized.Append(start, end, step)
	}

	return normalized
}
func (l *InclusiveRanges) normalized(invert bool) *InclusiveRanges {
	normalized := &InclusiveRanges{}

	var (
		start     int
		end       int
		step      int
		current   int
		pending   int
		keepValue bool
	)

	totalRange := NewInclusiveRange(l.Min(), l.Max(), 1)

	checkValue := l.Contains
	if !invert {
		checkValue = func(value int) bool {
			return !l.Contains(value)
		}
	}

	for it := totalRange.IterValues(); !it.IsDone(); {

		current = it.Next()

		keepValue = checkValue(current)

		// Short-circuit if we encounter a value that
		// is not in the original sequence.
		if keepValue {

			// fmt.Println("  Existing value")

			// If we haven't accumulated 2+ values to
			// add, just continue now and keep trying
			if pending < 2 {
				step++
				// fmt.Println("  Increasing step to:", step)
				continue
			}

			// If the step has changed from what we have
			// already accumulated, then add what we have
			// and start a new range.
			if (current + 1 - end) != step {
				// fmt.Println("  Step changed. Adding range:", start, end, step)
				normalized.Append(start, end, step)
				step = 1
				start = current
				pending = 0
			}

			continue
		}

		// fmt.Println("  Unique value")

		// If the value we want to keep is a different
		// stepping from the pending values, add what
		// we have and start a new range again.
		if pending >= 2 && current-end != step {
			// fmt.Println("  Step changed. Adding range:", start, end, step)
			normalized.Append(start, end, step)
			pending = 0
		}

		end = current

		// Start a new range
		if pending == 0 {
			// fmt.Println("  Starting new range")
			start = end
			step = 1
		}

		pending++
		continue
	}

	// Flush the remaining values
	if pending > 0 {
		// fmt.Println("  Flushing and adding remaining range:", start, end, step)
		normalized.Append(start, end, step)
	}

	return normalized
}
// Normalized returns a new instance, where all values have
// been sorted and compacted (where possible).
// If invert is true, then return all values within the start/end that
// are not in the current range.