liveBook · Manning

Rem

As explained in Clojure: The Essential Reference

rem is a Clojure function that returns the remainder of dividing two numbers, with sign behavior determined by the dividend.

rem returns the remainder left by dividing two numbers. Both arguments must be numbers; a zero divisor raises ArithmeticException, and double inputs produce a double result. section 5.7

Use these results to distinguish rem from related numeric operations:

  • Use quot for the whole-number division result and rem for the leftover amount. section 5.7

Sizing containers

Combine quot and rem to calculate a container size: (optimal-size 900 22) returns 41, then partition-all groups the items into containers of that size.

How Clojure: The Essential Reference covers it

How deep it goes
Defines the contract and demonstrates positive and negative results, while deliberately deferring detailed negative-number rules.
Examples
Demonstrates ordinary division, negative operands, container sizing, and grouping 900 items into partitions.
What it assumes
Assumes basic division terminology, while establishing quotient, remainder, divisor, and dividend distinctions.
What you can do after
Apply rem to obtain leftover quantities and detect whether division is exact using zero?.
How it connects
Connects rem with quotient, mod, integer division, and partition-all.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.