<commit_before>import {Reducer} from 'redux';

import {Action, Type} from 'action';
import {Element, Kind, make, series, shunt} from 'lib/element';

export type State = {
  readonly entry: Element,
  readonly active: number[],
};

const init: State = {
  entry: series(),
  active: [2],
};

export const reducer: Reducer<State> = (state = init, action: Action): State => {
  switch (action.type) {
    case Type.activate:
      return {...state, active: action.id};

    case Type.insert:
      if (state.active.length === 1 && state.active[0] === 0) {
        return ({
          entry: make(action.kind, state.entry),
          active: [1],
        });
      } else if (state.active.length > 1 && state.active[0] === 0) {
        if (state.entry.kind !== Kind.shunt) {
          throw new Error(`expected '${Kind.shunt}', got '${state.entry.kind}'`);
        }

        const nested = reducer(
          {
            entry: state.entry.value,
            active: state.active.slice(1),
          },
          action,
        );

        if (nested.entry.kind !== Kind.series) {
          throw new Error(`expected '${Kind.series}', got '${nested.entry.kind}'`);
        }

        return ({
          entry: shunt(state.entry.next, nested.entry),
          active: [0, ...nested.active],
        });

      } else {
        if (state.entry.kind === Kind.connector) {
          throw new Error(`unexpected '${Kind.connector}'`);
        }

        const nested = reducer(
          {
            entry: state.entry.next,
            active: [state.active[0] - 1, ...state.active.slice(1)],
          },
          action,
        );

        return ({
          entry: make(state.entry.kind, nested.entry, state.entry.value),
          active: [nested.active[0] + 1, ...nested.active.slice(1)],
        });
      }

    default:
      return state;
  }
};
<commit_msg>handle the Action to remove existing Elements<commit_after>import {Reducer} from 'redux';

import {Action, Type} from 'action';
import {Element, Kind, make, series, shunt} from 'lib/element';

export type State = {
  readonly entry: Element,
  readonly active: number[],
};

const init: State = {
  entry: series(),
  active: [2],
};

const advance = (id: number[], i: number, k: number) => [...id.slice(0, i), id[i] + k, ...id.slice(i + 1)];

const patch = (entry: Element, path: number[], f: (_: Element) => Element): Element => {
  if (path.length === 1 && path[0] === 0) {
    return f(entry);
  } else if (path.length > 1 && path[0] === 0) {
    if (entry.kind !== Kind.shunt) {
      throw new Error(`expected '${Kind.shunt}', got '${entry.kind}'`);
    }

    const next = patch(entry.value, path.slice(1), f);

    if (next.kind !== entry.value.kind) {
      throw new Error(`expected '${entry.value.kind}', got '${next.kind}'`);
    }

    return shunt(entry.next, next);

  } else {
    if (entry.kind === Kind.connector) {
      throw new Error(`unexpected '${Kind.connector}'`);
    }

    return make(entry.kind, patch(entry.next, advance(path, 0, -1), f), entry.value);
  }
};

export const reducer: Reducer<State> = (state = init, action: Action): State => {
  switch (action.type) {
    case Type.activate:
      return {...state, active: action.id};

    case Type.insert:
      return {
        entry: patch(state.entry, state.active, (entry) => make(action.kind, entry)),
        active: advance(state.active, state.active.length - 1, 1),
      };

    case Type.remove:
      const lead = action.id.slice(0, -1);

      return {
        entry: patch(state.entry, action.id, (entry) => {
          if (entry.kind === Kind.connector) {
            throw new Error(`unexpected '${Kind.connector}'`);
          }

          return entry.next;
        }),
        active: !(
          lead.length < state.active.length
          && lead.every((x, i) => x === state.active[i])
          && action.id[lead.length] < state.active[lead.length]
        ) ? state.active : advance(state.active, lead.length, -1),
      };

    default:
      return state;
  }
};

{'fraction_non_alphanumeric': 0.17644147488245485, 'fraction_numerical': 0.008166295471417966, 'mean_word_length': 2.267582861762328, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5364038', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1268, 'n_words': 317}