Thursday, December 14, 2017

#1 2009-09-13 01:58:13 pm

tellboy
Member
Registered: 2005-06-23
Posts: 152

The min challenge

A small challenge to any ASOC guru's out there.

How would you code the following objective-c in ASOC:

Applescript:

float minX = MIN(MIN(MIN(point0.x, point1.x), point2.x), point3.x);

--Terry

Offline

 

#2 2009-09-14 01:43:35 pm

tellboy
Member
Registered: 2005-06-23
Posts: 152

Re: The min challenge

I have come up with this, does anyone have a better solution?

Applescript:


   set tList to {tPoint0's x, tPoint1's x, tPoint2's x, tPoint3's x}
   set minX to min_(tList)

   on min_(theList)
       set minVal to theList's item 1
       repeat with i from 2 to count theList
           if theList's item i < minVal then
               set minVal to theList's item i
           end if
       end repeat
       return minVal
   end min_

Offline

 

#3 2009-09-15 06:10:18 pm

dougtallman
Member
Registered: 2008-05-18
Posts: 17

Re: The min challenge

This works too:

Applescript:

on min(a, b)
   if a < b then
       return a
   else
       return b
   end if
end min

set x to min(min(min(min(5, 4), 3), 2), 9)

The advantage is that you can use it on as many variables as you need to. Yours is a bit easier to read, IMHO

doug

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)