Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

optimze function 1

Status
Not open for further replies.

whosrdaddy

Vendor
Mar 11, 2003
4,231
BE
I have this:

Code:
private string CreateUTCValues()
        { //create this string: 
          //  "-12:-12;-11:-11;-10:-10;-9:-9;-8:-8;-7:-7;-6:-6;-5:-5;-4:-4;-3:-3;-2:-2;-1:-1;0:0;1:+1;2:+2;3:+3;4:+4;5:+5;6:+6;7:+7;8:+8;9:+9;10:+10;11:+11;12:+12"
            var str = "";
            for (var i = -12; i <= 12; i++)
            {
                if (!String.IsNullOrEmpty(str))
                    str += ";";
                if (i > 0)
                    str += String.Format("{0}:+{0}", i);
                else 
                    str += String.Format("{0}:{0}", i);
            }
            return str;
        }

I believe there must be better (or more efficient) ways to generate the desired output.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
this should work. i didn't test it, so there maybe bus, but it's the general idea.
Code:
private IEnumerable<int> Numbers(int startingAt)
{
   for(x=startingAs;x<int.MaxValue;x++)
   {
        yield return x;
   }
}
private string CreateUTCValues()
{
   return Numbers(-12)
             .TakeWhile(n => n < 13)
             .Select(n => new {n, abs=Math.Abs(n)})
             .Select(n => new {n.n, n.abs, sign=n.n==n.abs? "+" ? "-"})
             .Select(n => return string.Format("{0}:{1}{0}", n.n, n.sign))
             .Aggregate(new StringBuilder(), (b,t) => b.Append(t), b.ToString());
}
you could event make [tt]Numbers[/tt] a static function to clean up the code.
Code:
public static class Int32Extensions
{
    public static IEnumerable<int> StartHere(this int i)
    {
       for(x=i;x<int.MaxValue;x++)
       {
          yield return x;
       }
    }

    public static int Abs(this int i)
    {
       return Math.Abs(i);
    }
}
Code:
private string CreateUTCValues()
{
   return -12.StartHere()
             .TakeWhile(n => n < 13)
             .Select(n => new {N=n, n.Abs()})
             .Select(n => new {n.Abs, Sign=n.N==n.Abs? "+" ? "-"})
             .Select(n => return string.Format("{0}:{1}{0}", n.Abs, n.Sign))
             .Aggregate(new StringBuilder(), (b,t) => b.Append(t), b.ToString());
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Not to question your logic, but why not just use a constant?

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
well, it WAS a constant :).
but having such a long string makes the code unreadable and ugly(at least for me).

Jason,

nice examples,
but I want make a real "compact" function without loosing readability too much.

I was just wondering if there are other "ways" to build a string like this.
stars will be given for good examples :)

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
I want make a real "compact" function without loosing readability too much.
then a constant is what you want. this makes much more sense then creating a function to generate a string which is always the same.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
whosrdaddy,

By creating a function like this, you are:

1.) adding processing overhead to your application that is unnecessary

2.) making the code less maintainable

3.) adding more potential for bugs and errors into your program

I would highly recommend leaving it as a constant in your program.

Phyrstorm Technologies, Inc.
 
that is exactly what I did.

[thumbsup]

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top