--  Verada.Optionals (stream-combinators.md 6.2): a value that may be absent, and the reducers
--  that make `find` and `first` valid Ada:
--
--     First_Big : constant Optional := [for X of A when X > Limit => Just (X)]'Reduce (Keep_First, None);
--
--  Keep_First keeps the first value present, Keep_Last the last. (`Just`, not Rust's `Some`,
--  which Ada reserves.)
generic
   type Element is private;
package Verada.Optionals with SPARK_Mode is

   type Optional (Present : Boolean := False) is record
      case Present is
         when True =>
            Item : Element;
         when False =>
            null;
      end case;
   end record;

   function None return Optional is ((Present => False));

   function Just (X : Element) return Optional is ((Present => True, Item => X));

   function Has_Value (O : Optional) return Boolean is (O.Present);

   function Value (O : Optional) return Element is (O.Item)
   with Pre => O.Present;

   --  the value so far if there is one, else the next
   function Keep_First (So_Far, Next : Optional) return Optional is
     (if So_Far.Present then So_Far else Next);

   --  the next value if there is one, else the value so far
   function Keep_Last (So_Far, Next : Optional) return Optional is
     (if Next.Present then Next else So_Far);

end Verada.Optionals;
