473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

If

Hello,

I have the following:

if (tp.Weight >= 80) {
tp.Weight = 80;
}
else if (tp.Weight >= 60) {
tp.Weight = 60;
}
else if (tp.Weight >= 40) {
tp.Weight = 40;
}
else if (tp.Weight >= 20) {
tp.Weight = 20;
}
else {
tp.Weight = 0;
}

Is there a way to make this code shorter? Just wondering ..
Note that I always give the same value as the one I am testing ...

Thanks,
Miguel
Sep 5 '08 #1
9 1033
How about:

static int MyFunc(int weight)
{
weight = (weight / 20) * 20;
return weight 80 ? 80 : weight;
}

and tp.Weight = MyFunc(tp.Weight);

As in:

Console.WriteLine(MyFunc(0));
Console.WriteLine(MyFunc(5));
Console.WriteLine(MyFunc(20));
Console.WriteLine(MyFunc(25));
Console.WriteLine(MyFunc(40));
Console.WriteLine(MyFunc(45));
Console.WriteLine(MyFunc(60));
Console.WriteLine(MyFunc(65));
Console.WriteLine(MyFunc(80));
Console.WriteLine(MyFunc(85));
Console.WriteLine(MyFunc(100));
Console.WriteLine(MyFunc(105));

Marc
Sep 5 '08 #2
Actually, as part of some functional-programming work I've been
looking at recently (investigating F#), I've also been looking at more
versatile versions of switch (to compare to the F# approach)...

"don't do this", but I have some working code that allows:

var func = new Switch<int, int>()
.Case(x =x >= 80, 80)
.Case(x =x >= 60, 60)
.Case(x =x >= 40, 40)
.Case(x =x >= 20, 20)
.Default(0);

int result = func.Evaluate(65);

the "x =x >= 80" looks slightly unnatural because of the lambda (=>)
so close to the inequality (>=) - and note that the right-hand-side
could also have been a lambda...

I'm not saying that this is a good idea, but an interesting
investigation ;-p Maybe the C# team will be inspired by the better
aspects of F# to provide more flexible pattern constructs... who
knows...

Marc
Sep 5 '08 #3
"shapper" <md*****@gmail.comwrote in message
news:66**********************************@59g2000h sb.googlegroups.com...
Hello,

I have the following:

if (tp.Weight >= 80) {
tp.Weight = 80;
}
else if (tp.Weight >= 60) {
tp.Weight = 60;
}
else if (tp.Weight >= 40) {
tp.Weight = 40;
}
else if (tp.Weight >= 20) {
tp.Weight = 20;
}
else {
tp.Weight = 0;
}

Is there a way to make this code shorter? Just wondering ..
Yes. For example:

bool SnapWeightTo(Whatever tp, int weight)
{
if (tp.Weight >= weight)
{
tp.Weight = weight;
return true;
}
return false;
}

and then:

SnapWeightTo(tp, 80) || SnapWeightTo(tp, 60) || SnapWeightTo(tp, 40) ||
SnapWeightTo(tp, 20) || SnapWeightTo(tp, 0);

though the readability of this solution is suspect.
Sep 5 '08 #4
shapper wrote:
Hello,

I have the following:

if (tp.Weight >= 80) {
tp.Weight = 80;
}
else if (tp.Weight >= 60) {
tp.Weight = 60;
}
else if (tp.Weight >= 40) {
tp.Weight = 40;
}
else if (tp.Weight >= 20) {
tp.Weight = 20;
}
else {
tp.Weight = 0;
}

Is there a way to make this code shorter? Just wondering ..
Note that I always give the same value as the one I am testing ...

Thanks,
Miguel
How about:

tp.Weight %= 100;
tp.Weight -= tp.Weight % 20;

--
Göran Andersson
_____
http://www.guffa.com
Sep 5 '08 #5

"Marc Gravell" <ma**********@gmail.comwrote in message
news:4f**********************************@e39g2000 hsf.googlegroups.com...
Actually, as part of some functional-programming work I've been
looking at recently (investigating F#), I've also been looking at more
versatile versions of switch (to compare to the F# approach)...

"don't do this", but I have some working code that allows:

var func = new Switch<int, int>()
.Case(x =x >= 80, 80)
.Case(x =x >= 60, 60)
.Case(x =x >= 40, 40)
.Case(x =x >= 20, 20)
.Default(0);

int result = func.Evaluate(65);

the "x =x >= 80" looks slightly unnatural because of the lambda (=>)
so close to the inequality (>=) - and note that the right-hand-side
could also have been a lambda...

I'm not saying that this is a good idea, but an interesting
investigation ;-p Maybe the C# team will be inspired by the better
aspects of F# to provide more flexible pattern constructs... who
knows...
To be honest, all we really need is a let-expression that could be used to
introduce variables within the scope of a (sub)expression. Then your example
could be written as:

var result =
let x = Foo.Bar.Baz in
x >= 80 ? 80 :
x >= 60 ? 60 :
x >= 40 ? 40 :
x >= 20 ? 20 :
0;

which is quite readable, in my opinion.

Of course, we can explicitly do what Scheme does under the hood with "let",
and desugar it into a lambda...

TResult Bind<T, TResult>(this T x, Func<T, TResultbody) { return
body(x); }

var result =
Foo.Bar.Baz.Bind(x =>
x >= 80 ? 80 :
x >= 60 ? 60 :
x >= 40 ? 40 :
x >= 20 ? 20 :
0);

But this doesn't quite look as nice, and has an obvious performance penalty.
Sep 5 '08 #6
To be honest, all we really need is a let-expression that could be used to
introduce variables within the scope of a (sub)expression.
Well, I'm not sure the "introduce variables" gives us anything we
don't already have; the ternary conditional approach is quite nice,
but the bracketing gets tricky if the right-hand-side of any is non-
trivial. If anything, I'd rather an extension to the ternary
conditional syntax to make this more fluent (like how null coalescing
is fluent).

Re the Bind - this is unnecessary; you can do the same just with an
inline ternary (or a static method, which would remove the need for
delegate invoke):

(here I've added the missing brackets to show the worst-case of what
it would need to support)

static int MyFunc(int x)
{
return (x >= 80) ? (80)
: ((x >= 60) ? (60)
: ((x >= 40) ? (40)
: ((x >= 20) ? (20)
: (0))));
}

Marc
Sep 5 '08 #7

"Marc Gravell" <ma**********@gmail.comwrote in message
news:f2**********************************@y38g2000 hsy.googlegroups.com...
>To be honest, all we really need is a let-expression that could be used
to
introduce variables within the scope of a (sub)expression.

Well, I'm not sure the "introduce variables" gives us anything we
don't already have
It does, because it blurs the separation between statements and expressions
further, and allows you to do some things inline which right now have to be
split into several statements (and therefore cannot be done in the middle of
a LINQ query, for example, and has to be refactored into a method of its
own).

It's actually rather telling that LINQ itself has "let" - obviously there
are many scenarios where it's vital. Unfortunately, there are just as many
scenarios which don't involve LINQ, but which could use "let" for the same
reasons.
Re the Bind - this is unnecessary; you can do the same just with an
inline ternary (or a static method, which would remove the need for
delegate invoke):
You can do it if you already have the value of an expression bound to a
variable. The point of Bind is to take an expression, evaluate it once, bind
it to a name, and reuse that name inside some other expression several times
without the original expression being re-evaluated every time. In essence,
Bind is the precise equivalent of "let".
(here I've added the missing brackets to show the worst-case of what
it would need to support)

static int MyFunc(int x)
{
return (x >= 80) ? (80)
: ((x >= 60) ? (60)
: ((x >= 40) ? (40)
: ((x >= 20) ? (20)
: (0))));
}
Declaring a separate method for every such conditional (or indeed, any
expression that needs to reuse the value of some other expression) is very
inconvenient. That's why we have lambdas, after all...
Sep 5 '08 #8
tp.Weight = (tp.Weight / 20) * 20;

"shapper" <md*****@gmail.comwrote in message
news:66**********************************@59g2000h sb.googlegroups.com...
Hello,

I have the following:

if (tp.Weight >= 80) {
tp.Weight = 80;
}
else if (tp.Weight >= 60) {
tp.Weight = 60;
}
else if (tp.Weight >= 40) {
tp.Weight = 40;
}
else if (tp.Weight >= 20) {
tp.Weight = 20;
}
else {
tp.Weight = 0;
}

Is there a way to make this code shorter? Just wondering ..
Note that I always give the same value as the one I am testing ...

Thanks,
Miguel
Sep 8 '08 #9
CLGan wrote:
"shapper" <md*****@gmail.comwrote in message
news:66**********************************@59g2000h sb.googlegroups.com...
>Hello,

I have the following:

if (tp.Weight >= 80) {
tp.Weight = 80;
}
else if (tp.Weight >= 60) {
tp.Weight = 60;
}
else if (tp.Weight >= 40) {
tp.Weight = 40;
}
else if (tp.Weight >= 20) {
tp.Weight = 20;
}
else {
tp.Weight = 0;
}

Is there a way to make this code shorter? Just wondering ..
tp.Weight = (tp.Weight / 20) * 20;
Try create a unit tests for a couple of values like
5, 37, 64, 83 and 117 !

Arne
Sep 8 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.