1. Standard types

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 message » categorize

2. Re: Standard types

Juergen Luethje wrote:

> global type complex (sequence s)
[...]

I'm working on a library for dealing with complex numbers at the moment, but
it relies some other math libs I'm working on.

Those who were around on the old mailing list a few years back (tempus
fugit)might remember me mentioning (and even releasing) some math libs back
then. The new ones are the just old ones rehashed and (hopefully) optimised
for speed.

I'm not setting myself a deadline though, so if anyone desparately needs a
math routine in the meantime I'd be willing to see if I can cobble something
together from what I already have.

> --=========================[ date and time ]==========================--
>
> function leap_year (integer year)
[...]
> constant
>    DaysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

> function days_in_month (integer year, integer month)
[...]

> global type datetime (sequence s)
[...]

There are creepy similarities here to bits of a library I submitted this
time last year. Search for 'Carl' and 'datetime' at:
  http://www.rapideuphoria.com/archive.htm

Great minds think alike. ;)

Carl

new topic     » goto parent     » topic index » view message » categorize

3. Re: Standard types

Hello Carl,

you wrote:

> Juergen Luethje wrote:

>> global type complex (sequence s)
> [...]

> I'm working on a library for dealing with complex numbers at the moment, but
> it relies some other math libs I'm working on.

> Those who were around on the old mailing list a few years back (tempus
> fugit)might remember me mentioning (and even releasing) some math libs back
> then. The new ones are the just old ones rehashed and (hopefully) optimised
> for speed.

> I'm not setting myself a deadline though, so if anyone desparately needs a
> math routine in the meantime I'd be willing to see if I can cobble something
> together from what I already have.

At the moment, I don't need to calculate with complex numbers, but a
library for dealing with them will IMO be a valuable supplement to
Euphoria.


>> --=========================[ date and time ]==========================--
>>
>> function leap_year (integer year)
> [...]
>> constant
>>    DaysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

>> function days_in_month (integer year, integer month)
> [...]

>> global type datetime (sequence s)
> [...]

Some time ago, I wrote a .dll file in another language, containing
numerous date- and time-functions, including calculating Easter,
Advent, and more. Now I just have to translate the functions from Basic
to Euphoria.

> There are creepy similarities here to bits of a library I submitted this
> time last year. Search for 'Carl' and 'datetime' at:
>   http://www.rapideuphoria.com/archive.htm

Good work, also containing interesting historical information!

> Great minds think alike. ;)

I agree. blink

... or maybe it's just because we live under the same sun, and have the
same calendar. smile
Of course things become more complicated, if we begin to deal with
different calendars. I did not write routines for that purpose myself,
but here is a web site that I find interesting:
   http://www.fourmilab.ch/documents/calendar/

> Carl

Best regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu