Persistance function Demo (4 simple examples)

Topic Specific Tutorials and Tips.

Moderator: Rathinagiri

User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Persistance function Demo (4 simple examples)

Post by Pablo César »

Hi all,

For anyone who needs an example of persistence, something very simple but very useful follows:

Code: Select all

/* 
   Persistance function Demo1 (4 simple examples)
   by Pablo César on February 13th, 2017
   
   - First parameter: CodeBlock calling function with logical returns.
   - Second parameter: Number of tries. Optonal and default value is 10 tries.
   - You can use Persist function just without any conditional.
     Just executing Persist function In the attempt to increase the chances
	 when they occur unsuccessful return.
   - You can try to do it successively by asking the user if you want to 
     repeat the persistence.
   - nHowMany var can be removed. It's just for ilustration propose only.
     Not belongs to the persistance function.
   - bQuestion vars can be done inside when calling Persist function.
   - MsgInfo can be removed.  It's just for ilustration propose only.
   - First bQuestion is proposital (4, very few) for short persisting tries.
   - Another logical bQuestion can be done for dates returns.
*/

#include <hmg.ch>

Function Main()
LOCAL bQuestion1:={|| Test_L() = .T.}
LOCAL bQuestion2:={|| Test_N() > 0}
LOCAL bQuestion3:={|| !Empty(Test_C())}
LOCAL bQuestion4:={|| ValType(Test_Nil())="U"}

PRIVATE nHowMany := 0 // This var can be removed

Persist({|| Test_L() = .T.})

While !Persist(bQuestion1,4) .and. MsgYesNo("Do you want to try 4 times again ?")=.T. ; EndDo

If Persist(bQuestion1,4)
   MsgInfo("Successful #1"+CRLF+"Tries: "+AllTrim(Str(nHowMany)),"Upto 4")
Else
   MsgInfo("Unsucceeded #1","Upto 4")
Endif

If Persist(bQuestion2,10)
   MsgInfo("Successful #2"+CRLF+"Tries: "+AllTrim(Str(nHowMany)),"Upto 10")
Else
   MsgInfo("Unsucceeded #2","Upto 10")
Endif

If Persist(bQuestion3,20)
   MsgInfo("Successful #3"+CRLF+"Tries: "+AllTrim(Str(nHowMany)),"Upto 20")
Else
   MsgInfo("Unsucceeded #3","Upto 20")
Endif

If Persist(bQuestion4,2)
   MsgInfo("Successful #4"+CRLF+"Try: "  +AllTrim(Str(nHowMany)),"Upto 2")
Else
   MsgInfo("Unsucceeded #4","Upto 2")
Endif
Return Nil

Function Persist(bCondition, nTryUpTo)
LOCAL nTries := 0, lRet := .F.

If !ValType(bCondition)="B"
   MsgStop("First parameter must be a CodeBlock with logical return","First parameter is not a CodeBlock")
   Return .F.
Endif

DEFAULT nTryUpTo := 10

nHowMany := 0 // This var can be removed
Do While nTries < nTryUpTo
   nHowMany++ // This var can be removed
   If Eval(bCondition)
      lRet := .T.
	  nTries := nTryUpTo
   Endif
   nTries++
EndDo
Return lRet

Function Test_L()
STATIC nTimes_L := 0
LOCAL i, lRet := .F.

nTimes_L++
If nTimes_L = 8
   lRet := .T.
Endif
Return lRet

Function Test_N()
STATIC nTimes_N := 0
LOCAL i, nRet := 0

nTimes_N++
If nTimes_N = 9
   nRet := 1
Endif
Return nRet

Function Test_C()
STATIC nTimes_C := 0
LOCAL i, cRet := ""

nTimes_C++
If nTimes_C = 10
   cRet := "Yes"
Endif
Return cRet

Function Test_Nil()
Return Nil // Always returns Nil (Not good return value for logical interest)
The best part in this demo is:
You can also use Persist just without any logical returns.
Just executing Persist function in the attempt to increase the chances when it occurs unsuccessful return.
For example:
  • Persist( bQuestion1, 4 )
  • Persist( {|| Test_L() = .T.}, 4 )
Or you can try, and try as many time the user wants. ( Patience in their failed attempts... or maybe not ;) )

For example:
  • While !Persist( bQuestion1, 4 ) .and. MsgYesNo("Do you want to try 4 times again ?")=.T. ; EndDo
  • While !Persist( {|| Test_L() = .T.}, 4 ) .and. MsgYesNo("Do you want to try 4 times again ?")=.T. ; EndDo
The number of tries (here the value 4) is upto you.

Short, simple, practical, powerful and very useful. I guess.

I hope you enjoy.
Last edited by Pablo César on Mon Feb 13, 2017 6:12 pm, edited 1 time in total.
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Persistance function Demo (4 simple examples)

Post by Pablo César »

I do not want to be presumptuous but it would be great to have this function in HMG and I need to say. :roll:

If you think Claudio might be useful to be a part of HMG, please feel free to include it by changing what you think is necessary.

Including its name. (Hint of the new function name: HMG_Persist) :D

I will be using this function to detect and register the problem that exists in the _HMG_PRINTER_SavePages function.
Last edited by Pablo César on Mon Feb 13, 2017 6:12 pm, edited 1 time in total.
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
serge_girard
Posts: 3165
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Persistance function Demo1 (4 simple examples)

Post by serge_girard »

Great Idea Pablo, thanks!
Serge
There's nothing you can do that can't be done...
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Persistance function Demo (4 simple examples)

Post by Pablo César »

Good thing you liked it, Serge! :)
Last edited by Pablo César on Mon Feb 13, 2017 6:13 pm, edited 1 time in total.
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
mol
Posts: 3720
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Persistance function Demo1 (4 simple examples)

Post by mol »

I don't understand what's idea with this function...
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Persistance function Demo (4 simple examples)

Post by Pablo César »

Hi Marek, thanks for your interest.

Here's Wikipedia says:
https://en.wikipedia.org/wiki/Persisten ... r_science)

For example: Let's say you have created a series of files generated by an application and want to delete those files where they are stored and accessed on a computer network. And in your attempt, the files can not be deleted because someone is using it or else someone left it open.

Theoretically, you would identify what you might be using and would ask them to exit the application and thus be able to delete such files.

If you did not use a persistence routine, you would have to repeat the whole process in which you created it, committing unnecessary re-processing and in some situations this would not be allowed. Hence comes this question of the persistence routine. Where it the user will remain for a condition that can be:
  1. By number of determined attempts
  2. Or by both cases, option #1 plus choosing the user to do repetitions by responding to the simple "Try again?"
This routine also all of us have already made use of in the attempt to open a DBF in exclusive mode (say to give a PACK) and that would be being used by another user.

In SQL it is also necessary to implement a similar routine to ensure certain openings and thus allow updates.

Recently in this case, we are not sure which of the functions would be failing ... so I used the persistence function so that when the condition is not met (for example, the x image is not generated) then repeat for a number of times and after that (if still negative) it gives a message asking "Try again ?" and also providing the name of the file to be analyzed.

Anyway, this routine that I made available is something simple but often necessary and/or convenient. It's good to keep it in mind.

I hope I have contributed something useful and have clarified your doubts.
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Persistance function Demo (4 simple examples)

Post by Pablo César »

But what do you think about this kind of function ? May you comment, Marek ?

I probably know what you gonna say about that...

I do not see any use, and I'll probably never use it. :|

Sorry to do any pre-trial. But as you only thanked... :roll:

For me it is important to know what others use and to adopt what I consider to be good or necessary.

Sorry for my curiosity. :)
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
mol
Posts: 3720
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Persistance function Demo (4 simple examples)

Post by mol »

I don't know if it will be useful for me. Maybe little sample of usage will give any idea... Sometimes you don't see possiblilitity of using, until the time you will be enlightened
User avatar
serge_girard
Posts: 3165
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Persistance function Demo (4 simple examples)

Post by serge_girard »

I agree with Marek. A concrete sample would be useful although I understand now the meaning (I think)
Serge
There's nothing you can do that can't be done...
Post Reply