<commit_before>import { combineReducers } from "redux";

// State: Todo[]
function byId(state = {}, action) {
  switch (action.type) {
    case "RECEIVE_TODOS":
      // mutating a shallow clone of state is fine inside
      //  a reducer, since the function stays pure.
      const nextState = { ...state };
      action.response.forEach(t => {
        nextState[t.id] = t;
      })

      return nextState;
    default:
      return state;
  }
}

function allIds(state = [], action) {
  if (action.filter !== "all") {
    return state;
  }

  switch (action.type) {
    case "RECEIVE_TODOS":
      return action.response.map(t => t.id);
    default:
      return state;
  }
}

function activeIds(state = [], action) {
  if (action.filter !== "active") {
    return state;
  }

  switch (action.type) {
    case "RECEIVE_TODOS":
      return action.response.map(t => t.id);
    default:
      return state;
  }
}

function completedIds(state = [], action) {
  if (action.filter !== "complete") {
    return state;
  }

  switch (action.type) {
    case "RECEIVE_TODOS":
      return action.response.map(t => t.id);
    default:
      return state;
  }
}

const idsByFilter = combineReducers({
  all: allIds,
  active: activeIds,
  complete: completedIds
})

export default combineReducers({
  byId,
  idsByFilter
});

//
// Selectors
//

// getFilteredTodo is a selector: it is tightly coupled to the state
//  structure, so it's best to keep it with the reducer that controls
//  that slice of the state tree. If we split up reducers into several files,
//  it would go with the `todos` reducer.
export function getFilteredTodos(state, filter) {
  const ids = state.idsByFilter[filter];
  return ids.map(id => state.byId[id]);
}<commit_msg>Higher-order reducer for filtered id sets<commit_after>import { combineReducers } from "redux";

// State: Todo[]
function byId(state = {}, action) {
  switch (action.type) {
    case "RECEIVE_TODOS":
      // mutating a shallow clone of state is fine inside
      //  a reducer, since the function stays pure.
      const nextState = { ...state };
      action.response.forEach(t => {
        nextState[t.id] = t;
      })

      return nextState;
    default:
      return state;
  }
}

// factory for a reducer that manages an array of todoIds
//  that match te given filter.
function createList(filter) {
  return (state = [], action) => {
    if (action.filter !== filter) {
      return state;
    }

    switch (action.type) {
      case "RECEIVE_TODOS":
        return action.response.map(t => t.id);
      default:
        return state;
    }
  }
}

const idsByFilter = combineReducers({
  all: createList("all"),
  active: createList("active"),
  complete: createList("complete")
})

export default combineReducers({
  byId,
  idsByFilter
});

//
// Selectors
//

// getFilteredTodo is a selector: it is tightly coupled to the state
//  structure, so it's best to keep it with the reducer that controls
//  that slice of the state tree. If we split up reducers into several files,
//  it would go with the `todos` reducer.
export function getFilteredTodos(state, filter) {
  const ids = state.idsByFilter[filter];
  return ids.map(id => state.byId[id]);
}

export function getTodo(state, id) {
  return state.byId[id];
}
{'fraction_non_alphanumeric': 0.125, 'fraction_numerical': 0.0, 'mean_word_length': 2.8280701754385964, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 12, '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': '7575299', 'n_tokens_mistral': 1065, 'n_tokens_neox': 1047, 'n_words': 318}