liveBook · Manning

Generics

As explained in Data-Oriented Programming: Reduce software complexity

A generic data manipulation function processes structured data according to its accessible structure rather than a specific domain class.

A polymorphic function manipulates structured data through accessible keys, paths, or collection shapes instead of depending on a specific domain class. Records represented as heterogeneous string maps can therefore share operations such as retrieving, filtering, merging, renaming, and serializing fields. This flexibility weakens code–data dependencies, though it gives up compile-time field safety. section 3.0 section 3.6

A polymorphic function typically works by:

  • Receiving a map, array, information path, or field name rather than a domain-specific object. section 3.3
  • Applying the same operation across different records, collections, or database results. section 10.5
  • Composing small operations to express business logic and web-service data flows. section 11.8
  • Moving complex manipulation details into reusable custom functions when library operations are insufficient. section 14.0

Joining records from two sources

A generic joinArrays function accepts two arrays and two key names, indexes each array with keyBy, merges the resulting maps, and returns their values. The same function can combine database records with external-service records without knowing a Book class. section 11.6

Updating nested data

An update function accepts a map, path, and transformation function: it retrieves the current value, transforms it, and writes the result back. Thus removing duplicate authors becomes update(book, "authors", _.uniq). section 14.1

Lodash functions introduced in this chapter

Function

Description

get(map, path)

Gets the value of map at path

has(map, path)

Checks if map has a field at path

merge(mapA, mapB)

Creates a map resulting from the recursive merges between mapA and mapB

values(map)

Creates an array of values of map

filter(coll, pred)

Iterates over elements of coll, returning an array of all elements for which pred returns true

map(coll, f)

Creates an array of values by running each element in coll through f

Browse the generic collection and map operations used for paths, presence checks, merging, mapping, filtering, and value extraction. section 3.6

How Data-Oriented Programming: Reduce software complexity covers it

How deep it goes
Demonstrates paths, collection operations, custom updates, joins, and composition, while trading away compile-time field safety.
Examples
Uses catalog records, nested book titles, database rows, external book information, array joins, and duplicate-author removal.
What it assumes
Assumes basic maps, arrays, field names, and functions while teaching information paths and generic data manipulation through examples.
What you can do after
Builds reusable data transformations and applies them to heterogeneous records without creating a class for every variation.
How it connects
Connects data-oriented programming with maps, arrays, information paths, JSON serialization, late binding, and web services.

Also explained in

Compare how 20 books explain generics →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming: Reduce software complexity. The section links point to the parts of the book it draws on.