Standard types

new topic     » topic index » view thread      » older message » newer message

Hello all,

I just had the idea to put some standard types together in one file, so
that it's easier to reuse them. Here they are.

Best regards,
   Juergen


------------------------------------------------------------------>8----
--  Collection of standard types (Euphoria 2.3)
--  > by Juergen Luethje (http://luethje.de.vu/)
--  Freeware
--  Standard disclaimer: Use at your own risk!

--  These are types included in the Euphoria 2.3 distribution, which
--  in my opinion are of general use, and some more.

------------------------------------------------------------------------
--  History
--  ¯¯¯¯¯¯¯
--  2002, June 26 - v0.10


--==============================[ math ]==============================--
--  math definitions partly after http://mathworld.wolfram.com/

global type boolean (integer i)
   return i = 0 or i = 1
end type

global type positive_atom (atom a)
   return a > 0
end type

global type positive_int (integer i)
   return i > 0
end type

global type nonnegative_atom (atom a)
   return a >= 0
end type

global type nonnegative_int (integer i)
   return i >= 0
end type

global type negative_atom (atom a)
   return a < 0
end type

global type negative_int (integer i)
   return i < 0
end type

global type nonpositive_atom (atom a)
   return a <= 0
end type

global type nonpositive_int (integer i)
   return i <= 0
end type

global type huge_int (atom a)
   -- Euphoria's built-in integer type cannot hold huge numbers.
   -- This type is technically an atom, but mathematically an integer.
   return a = floor(a)
end type

global type complex (sequence s)
   return length(s) = 2 and atom(s[1]) and atom(s[2])
end type

global type trig_range (atom a)
   --  values passed to arccos and arcsin must be [-1,+1]
   return a >= -1 and a <= 1
end type


--=========================[ date and time ]==========================--

function leap_year (integer year)
   -- returns TRUE or FALSE
   return (remainder(year, 4) = 0) and
         ((remainder(year, 100) != 0) or (remainder(year, 400) = 0))
end function


constant
   DaysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

function days_in_month (integer year, integer month)
   integer ret

   if month < 1 or month > 12 then return 0 end if
   ret = DaysInMonth[month]
   if (month = 2) and leap_year(year) then ret += 1 end if
   return ret
end function


global type datetime (sequence s)
   -- s must be a sequence of the form {year, month, day, hour, minute, second},
   -- which is a valid date and time of day >= 0001-01-01, 00:00:00,
   -- if always the Gregorian Calendar had been used
   object year, day, hour, minute, second

   if length(s) != 6 then
      return 0
   end if

   year = s[1]
   if not integer(year) or year < 1 or year > 9999 then
      return 0
   end if

   -- s[2] is checked by days_in_month()
   day = s[3]
   if not integer(day) or day < 1 or day > days_in_month(year, s[2]) then
      return 0
   end if

   hour = s[4]
   if not integer(hour) or hour < 0 or hour > 23 then
      return 0
   end if
   minute = s[5]
   if not integer(minute) or minute < 0 or minute > 59 then
      return 0
   end if
   second = s[6]
   if not atom(second) or second < 0 or second >= 60 then
      return 0
   end if

   return 1
end type


--==============================[ misc ]==============================--

global type byte (integer i)
   return i >= 0 and i <= 255
end type

global type string (sequence s)
   object x

   for i = 1 to length(s) do
      x = s[i]
      if (not integer(x)) or (x < 0) or (x > 255) then
         return 0
      end if
   end for
   return 1
end type

global type char (integer i)
   -- true if i is a character that can be printed on the screen
   return i >= 0 and i <= 127
end type

global type digit_char (integer i)
   return i >= '0' and i <= '9'
end type

global type keycode (integer i)
   -- a keyboard code
   return i >= -1 and i <= 511
end type

global type file_number (integer i)
   return i >= 0
end type

global type valid_routine_id (integer i)
   return i >= 0 and i <= 1000
end type

global type sorted_ascending (sequence s)
   -- return TRUE if s is in ascending order
   for i = 1 to length(s)-1 do
      if compare(s[i], s[i+1]) > 0 then
         return 0
      end if
   end for
   return 1
end type


--====================================================================--

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu