Re: Integer() operation on a sequence?

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

Michael Raley wrote:
> 
> 
> not too suprisingly I cannot do this 
> 
>  s =  integer({0.12321,0.44343,1,1,0.555}) 
> - s becomes {0,0,1,1,0} 

What were you expecting to achieve? 

** Did you wish to convert each of the elements into an integer?
  seq s s = floor({0.12321,0.44343,1,1,0.555})

** Did wish to see if each element was an integer?
  seq s s = integer({0.12321,0.44343,1,1,0.555}) 

** Did you wish to see if the entire paramter was an integer?
  integer s if integer({0.12321,0.44343,1,1,0.555}) then
    s = 1 else s = 0 end if

** Did you wish to convert the entire parameter into a single integer?
    sequence s s = value({0.12321,0.44343,1,1,0.555}) 


 
>  So howabout? 
>  s = integers({sequence})

What is supposed to return? The number of integers in the sequence?

   s = integer (x)
   y = 0
   for i = 1 to length(x)
        y += x[i]
   end for
 
>  Secondly; a sequence summation operator
>  i= ~sigma ({sequence s from above}) 
>  i = 2 

Euphoria does allow a fair degree of flexibility. How about this version
of sigma for you...

    function i_qual(object x)
        if integer(x) then
            return 1
        else 
            return 0
        end if
    end function
    function a_qual(object x)
        if atom(x) then
            return 1
        else 
            return 0
        end if
    end function
    function p_qual(object x)
        if atom(x) and x > 0 then
            return 1
        else 
            return 0
        end if
    end function

    function n_qual(object x)
        if atom(x) and x < 0 then
            return 1
        else 
            return 0
        end if
    end function

    constant OnlyIntegers = routine_id("i_qual"),
             OnlyAtoms    = routine_id("a_qual"),
             OnlyPos      = routine_id("p_qual"),
             OnlyNeg      = routine_id("n_qual")

    function sigma(sequence x, object qual)
    object y
    integer t
    if atom(qual) then qual ={qual} end if
    y = 0
    for i = 1 to length(x) do
        t = 0
        for j = 1 to length(qual) do
            t += call_func(qual[j],{x[i]})
        end for
        if t = length(qual) then
            y += x[i]
        end if
    end for
    return y
    end function
    sequence s
    s = {0.12321,-0.44343,1,-2,0.555}
    ? sigma(s, OnlyIntegers) 
    ? sigma(s, OnlyAtoms) 
    ? sigma(s, {OnlyIntegers,OnlyPos}) 
    ? sigma(s, {OnlyAtoms,OnlyNeg}) 
    ? sigma(s, {OnlyAtoms,OnlyPos}) 
    ? sigma(s, {OnlyIntegers,OnlyNeg}) 


What I'm trying to say is that you could try out writing your own function
first and if that is not ever going to be fast enough for your needs,
then you could lobby RDS for a new built-in routine. 

-- 
Derek Parnell
Melbourne, Australia

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

Search



Quick Links

User menu

Not signed in.

Misc Menu