assets/src/services/char.js
/**
 * Generate a position between p1 and p2. The generated position will be heavily
 * biased to lean towards the left since character insertions tend to happen on
 * the right side.
 *
 * @param {Identifier.Identifier} position1
 * @param {Identifier.Identifier} position2
 * @param {Identifier.Site} site
 * @return {Array<Identifier.Identifier>}
 */
Generate a position between p1 and p2. The generated position will be heavily
biased to lean towards the left since character insertions tend to happen on
the right side.


Generate a position between p1 and p2 . The generated position will be heavily biased to lean towards the left since character insertions tend to happen on the right side .
function generatePositionBetween (position1, position2, site) {
  const head1 = head(position1) || Identifier.create(0, site)
  const head2 = head(position2) || Identifier.create(Decimal.BASE, site)
  if (head1.digit !== head2.digit) {
    const n1 = Decimal.fromIdentifierList(position1)
    const n2 = Decimal.fromIdentifierList(position2)
    const delta = Decimal.subtractGreaterThan(n2, n1)
    const next = Decimal.increment(n1, delta)
    return Decimal.toIdentifierList(next, position1, position2, site)
  } else {
    if (head1.site < head2.site) {
      return cons(head1, generatePositionBetween(rest(position1), [], site))
    } else if (head1.site === head2.site) {
      return cons(head1, generatePositionBetween(rest(position1), rest(position2), site))
    } else {
      throw new Error('invalid site ordering')
    }
  }
}
function generatePositionBetween ( position1 , position2 , site ) { const head1 = head ( position1 ) || Identifier . create ( 0 , site ) const head2 = head ( position2 ) || Identifier . create ( Decimal . BASE , site ) if ( head1 . digit !== head2 . digit ) { const n1 = Decimal . fromIdentifierList ( position1 ) const n2 = Decimal . fromIdentifierList ( position2 ) const delta = Decimal . subtractGreaterThan ( n2 , n1 ) const next = Decimal . increment ( n1 , delta ) return Decimal . toIdentifierList ( next , position1 , position2 , site ) } else { if ( head1 . site < head2 . site ) { return cons ( head1 , generatePositionBetween ( rest ( position1 ) , [ ] , site ) ) } else if ( head1 . site === head2 . site ) { return cons ( head1 , generatePositionBetween ( rest ( position1 ) , rest ( position2 ) , site ) ) } else { throw new Error ( 'invalid site ordering' ) } } }
Generate a position between p1 and p2.
Generate a position between p1 and p2 .
// Get either the head of the position, or fallback to default value // Case 1: Head digits are different // It's easy to create a position to insert in-between by doing regular arithmetics. // Increment n1 by some amount less than delta // Case 2: Head digits are the same, sites are different // Since the site acts as a tie breaker, it will always be the case that // cons(head1, anything) < position2 // Case 3: Head digits and sites are the same // Need to recurse on the next digits