Jump to content
Mike Torrettinni

How to switch condition position?

Recommended Posts

Is it possible to switch the condition to the end:

 

from this:

If (condition) Then Result := True;

 

into something like this:

 

Result := True if (condition);

Or similar variation, where result is shown first.

I know that Case works like that, but I need it to work on string values.

 

When visually looking at the statement, sometimes the Result value has a 'priority' over condition - my personal preference.
 

 

 

Share this post


Link to post
1 hour ago, Mike Torrettinni said:

Is it possible to switch the condition to the end:

 

from this:


If (condition) Then Result := True;

 

into something like this:

 


Result := True if (condition);

Or similar variation, where result is shown first.

I know that Case works like that, but I need it to work on string values.

 

When visually looking at the statement, sometimes the Result value has a 'priority' over condition - my personal preference.
 

 

 

What does the documentation say? 

Share this post


Link to post
1 hour ago, Leif Uneus said:

Result := Result or (condition);

 

 

I didn't provide good example, I'm trying to work on statement like this, working on strings:

 

if Property = 'A' then Result := 'N';

 

so looking for in idea how to have something like this:

 

Result := 'N' if/when (Property = 'A');

 

Share this post


Link to post

Write your own procedure:

procedure MPC(var Result: string; const Value: string; Condition: Boolean);
// MPC stands for Mike's Preferenced Condition
begin
  if Condition then Result := Value;
end;

...
// call
MPC(Result, 'N', Property = 'A');

You can even make it a function, but then you must provide a value for the false condition, too.

  • Thanks 1

Share this post


Link to post

There's always the IfThen function.

 

Result := IfThen( aStr = 'OK', True, False );

 

The problem is, this form

If (condition) Then Result := True;

does not set Result if condition is false. So it can be undefined or random.

 

Edited by David Schwartz

Share this post


Link to post
47 minutes ago, David Heffernan said:

So you know the answer

Yes, no matter what i tried, it didn't compile. Was hoping there is simple solution, missing from help. No luck.

Share this post


Link to post
1 hour ago, Uwe Raabe said:

Write your own procedure:


procedure MPC(var Result: string; const Value: string; Condition: Boolean);
// MPC stands for Mike's Preferenced Condition
begin
  if Condition then Result := Value;
end;

...
// call
MPC(Result, 'N', Property = 'A');

You can even make it a function, but then you must provide a value for the false condition, too.

Thank you, but was trying to avoid using methods, it kind of defeats the purpose of simple If statement.

Share this post


Link to post
1 hour ago, David Schwartz said:

There's always the IfThen function.

 


Result := IfThen( aStr = 'OK', True, False );

 

The problem is, this form


If (condition) Then Result := True;

does not set Result if condition is false. So it can be undefined or random.

 

IfThen's are awesome, I use them a lot, but it's not usable for this case, I need Result to be very clear at the beginning.

Share this post


Link to post
14 hours ago, Mike Torrettinni said:

Is it possible to switch the condition to the end:

 

from this:


If (condition) Then Result := True;

 

into something like this:

 


Result := True if (condition);

 

No, and I am glad about that. having the if after the condition is something I never understood or liked in Python.

 

I mean I understand the construct but I never understood why it was done that way.

 

And I know one must be Dutch to understand some things in Python, but heck, I am Dutch, and have been for the last 58 years (I'm only 4 years younger than Van Rossum).

 

FWIW, the parentheses are not required here:

if Condition then Result := True;

Or even better:

Result := Condition;

 

Edited by Rudy Velthuis

Share this post


Link to post
3 hours ago, Rudy Velthuis said:

No, and I am glad about that. having the if after the condition is something I never understood or liked in Python.

 

I mean I understand the construct but I never understood why it was done that way.

 

And I know one must be Dutch to understand some things in Python, but heck, I am Dutch, and have been for the last 58 years (I'm only 4 years younger than Van Rossum).

 

FWIW, the parentheses are not required here:


if Condition then Result := True;

Or even better:


Result := Condition;

 

I am glad too. But to address the part about you not understanding the construct, I would suggest that it resembles 'reverse Polish notation', and that is notoriously easy to implement in a stack environment. I have an book on interpreters somewhere in the bowels of my archeology site that some would laughingly call a 'library' that discusses exactly that method for implementing a Fortran interpreter.

Share this post


Link to post
18 hours ago, Mike Torrettinni said:

Is it possible to switch the condition to the end:

 

from this:


If (condition) Then Result := True;

 

into something like this:

 


Result := True if (condition);

Or similar variation, where result is shown first.

I know that Case works like that, but I need it to work on string values.

 

When visually looking at the statement, sometimes the Result value has a 'priority' over condition - my personal preference.
 

 

 

Huh, yeah like Rudy said, how about

 

Result  := (condition)

 

So your example

if Property = 'A' then Result := 'N';

becomes

 

Result :=

{ start anonymous method } function(old, test, new: string) : string
  begin
    Result := old;
    if old := test then result := new;
  end; { end anonymous method }

NO. anonymous method bad (IMHO) Much better is:

 

function tester(old, test, new: string) : string;

begin

  Result := old;
  if old := test then result := new;

end;

 

and then call it like

 

function domything : string;

begin

  {do something to establish the initial value of result} 

  Result := tester(result, 'A', 'N');

end;

 

Fundamentally, you are trying to do something that just is not in the syntax of the language. The designer(s) of the Pascal language made syntax choices that give the language its 'flavor'. Other designers gave other languages different syntax because it appealed to them or they saw 'easy' ways to implement them or whatever. It is pointless to choose an implementation language and then bang against the design choices inherent in that language. If you are used to something working one way in one language that you don't see in Pascal, then you need to understand that you are now working in Pascal, not in that other language. Your goal is not to write a program that 'looks like a similar program in language X'; your goal is to solve a problem in the language you're are working with. EVERY language whether it is Pascal, Perl, Python, Lisp, Cobol, APL, Fortran, Simula, Visual Basic, or anything else has constructs to accomplish your goal. But they all look differently when they do it, and that is what makes them different from one another.

 

Edited by KeithLatham
spelling
  • Thanks 1

Share this post


Link to post
12 hours ago, Mike Torrettinni said:

Yes, no matter what i tried, it didn't compile. Was hoping there is simple solution, missing from help. No luck.

Seems kind of a waste of time this entire discussion. The language is well documented and is quite concise. The are only a handful of conditional statements. The syntax for if is clear. You have to write the code in the language that is provided to you. Use an if statement. End of story. 

  • Like 1

Share this post


Link to post
4 hours ago, Rudy Velthuis said:

Or even better:


Result := Condition;

That's certainly more concise but it has a completely different meaning. Think about it Rudy. 

Share this post


Link to post
if Condition then Result := True;
Result := Condition;

These are not the same! What if Result is true before that line in the first and in the second case?

  • Like 1

Share this post


Link to post
15 hours ago, Uwe Raabe said:

Write your own procedure:


procedure MPC(var Result: string; const Value: string; Condition: Boolean);
// MPC stands for Mike's Preferenced Condition
begin
  if Condition then Result := Value;
end;

...
// call
MPC(Result, 'N', Property = 'A');

You can even make it a function, but then you must provide a value for the false condition, too.

You might consider to inline it ... the call is too expensive !

Also what about that ?

Result := Condition or Result;

 

Share this post


Link to post
36 minutes ago, Mahdi Safsafi said:

Also what about that ?


Result := Condition or Result;

That is a valid approach for the Result := Condition part, but in the original example Result is a string while Condition is a Boolean;

Share this post


Link to post
49 minutes ago, Uwe Raabe said:

 That is a valid approach for the Result := Condition part, but in the original example Result is a string while Condition is a Boolean;

Ah I see ! 

If I know that the compiler can do a good job about optimisation I'd write something like this:

begin
  // ...
  // Requires Rio
  const table = [Result, 'true'];
  Result := table[integer(Condition)];
end;

 desperately waiting for :

Result := Condition ? Value : Result;

 

Share this post


Link to post

While I appreciate all the engagement in this thread, the whole question was just a simple 'is it possible' idea. A little more explanation when I got this idea (please consider my beginners level of Delphi experience):

So, I have a function that returns default value of a property and since I was just building up this feature, I wanted to know what the defaults are right away. So, this is rough example:

 

if MatchStr('PropertyName', ['Prop1', 'Prop2', 'Prop3', 'Prop4', 'Prop5', 'Prop6', 'Prop7', 'Prop8', 'Prop9', 'Prop10' ]) then Result := 'N'
else if (MatchStr('PropertyName', ['PropertyA', 'PropertyB', 'PropertyC', 'PropertyD', 'PropertyE', 'PropertyF', 'PropertyG', 'PropertyH'
                                 'PropertyI', 'PropertyJ', 'PropertyK', 'PropertyL', 'PropertyM', 'PropertyN', 'PropertyO', 'PropertyP',
								 'PropertyQ'...]) then Result := 'DefaultABC'
else if PropertyName = 'CustomProp' Result := 'X'
...

and since I was adding new property names and value, I always wanted to know exactly what is already there and what the names and values are, and a Result was an important information, so I was thinking it would be so cool to have it reversed:

 

 

Result := 'N' if MatchStr('PropertyName', ['Prop1', 'Prop2', 'Prop3', 'Prop4', 'Prop5', 'Prop6', 'Prop7', 'Prop8', 'Prop9', 'Prop10' ])
else Result := 'DefaultABC' if MatchStr('PropertyName', ['PropertyA', 'PropertyB', 'PropertyC', 'PropertyD', 'PropertyE', 'PropertyF', 'PropertyG', 'PropertyH'
                                 'PropertyI', 'PropertyJ', 'PropertyK', 'PropertyL', 'PropertyM', 'PropertyN', 'PropertyO', 'PropertyP',
								 'PropertyQ'...])
else Result := 'X' if (PropertyName = 'CustomProp')
....

So, it's more visual thing, where I first see possible default values and then which property names resolve to it.

 

I know I can do this 10s of other way with lookup table, consts... but this all means the default values and property names are not together, and I would need to jump back and forth to get the right information.

Share this post


Link to post
Quote

While I appreciate all the engagement in this thread, the whole question was just a simple 'is it possible' idea.

Well Mike. This is our community. They're all exited and each topic turns just like this one xD ... All people here love coding ... and I'm sure you will love this community too.

You said your're a new to Delphi. We welcome you here 🙂.

Now for your question about using "Result := Value if(Condition);" , the sort answer is not possible. 

I believe that using table lookup is match easy for you:


type
  TRec = record
    Patterns: array of string;
    Result: string;
  end;

  PRec = ^TRec;

const
  Table: array [0 .. 1] of TRec = (
    (Patterns: ['Prop1', 'Prop2', 'Prop3', 'Prop4', 'Prop5', 'Prop6', 'Prop7']; Result: 'N'),
    (Patterns: ['PropertyA', 'PropertyB', 'PropertyC', 'PropertyD']; Result: 'DefaultABC')
    );

function LookUp(const AProperty: string): string;
var
  I: Integer;
  Rec: PRec;
begin
  for I := 0 to Length(Table) - 1 do
  begin
    Rec := @Table[I];
    if (MatchStr(AProperty, Rec^.Patterns)) then
      exit(Rec^.Result);
  end;
  Result := 'Default';
end;

Because you're a new to Delphi, I believe that's the best way for you right now. Later you can optimise it by using binary string search, hash-code, magic-index, crc32 and binary lookup, ... 

  • Thanks 1

Share this post


Link to post
3 minutes ago, Mahdi Safsafi said:

You said your're a new to Delphi. We welcome you here 🙂.

Thank you for the example! I'm not really new to Delphi, but never grew out of the beginners's level of thinking, I guess... you can find a thread here me questioning about why should I use classes at all 🙂

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×