The Magnet Is a Predicate You Drag Through a River
A magnet on a rope has no idea where anything is on the riverbed. It knows one thing: given an object, does it stick or not? You drag it through a heap of mud, aluminum cans, quartz pebbles, one lost bike chain, and it silently drops everything that fails the test and keeps everything that passes. That is a predicate — a function from a thing to yes-or-no — and pulling it through a collection is filter.
In Clojure the magnet is a value you can hold and pass around:
(defn ferrous? [item]
(contains? #{:iron :steel :nickel} (:metal item)))
(def riverbed
[{:name "horseshoe" :metal :iron}
{:name "beer can" :metal :aluminum}
{:name "old key" :metal :steel}
{:name "quartz" :metal :none}
{:name "bike chain" :metal :steel}])
(doseq [x (filter ferrous? riverbed)]
(println (:name x) "->" (:metal x)))
horseshoe -> :iron
old key -> :steel
bike chain -> :steel
ferrous? gets handed to filter like a magnet clipped onto a rope. The rope is generic; the magnet decides.
Ada 83, which the U.S. Department of Defense was busy mandating across every avionics contract in the same decade, won’t let you do that. Subprograms aren’t first-class values there — you can’t pass Ferrous into a general dragging routine — so the test lives welded inside the loop:
with Ada.Text_IO; use Ada.Text_IO;
procedure Magnet is
type Metal is (Iron, Steel, Nickel, Aluminum, Glass);
type Find is record
Name : String (1 .. 9);
Made : Metal;
end record;
Riverbed : constant array (1 .. 4) of Find :=
(("horseshoe", Iron),
("beer can", Aluminum),
("old key", Steel),
("quartz ", Glass));
begin
for I in Riverbed'Range loop
if Riverbed (I).Made = Iron
or Riverbed (I).Made = Steel
or Riverbed (I).Made = Nickel
then
Put_Line (Riverbed (I).Name & " sticks");
end if;
end loop;
end Magnet;
horseshoe sticks
old key sticks
Same catch, same discard pile. But notice where the magnet went: in Clojure it’s an object you carry between routines; in Ada 83 it’s fused to the loop and can’t be lifted out. To change what sticks, the Clojure fisher swaps the magnet; the Ada fisher rewrites the boat.