Forum > Beginners

Centralising code

(1/3) > >>

Kaller:
Suppose I have a bunch of enumerated states that are basically bistates, like left/right, up/down/ top/bottom on/off etc. and instead of writing a bunch of flip functions can I re-use a single Flip() function that toggles a state so that if I Flip(on) I get off and if I flip(up) I get down? Will generics do that for me?

Fibonacci:
If your enums have only 2 states, then they are 0 and 1, you can use something like this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure flip(e: PInteger);begin  e^ := integer(not boolean(e^));end;
And use it like this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---flip(@yourswitch);

Kaller:
That's pretty slick.

Fibonacci:

--- Quote from: Kaller on September 20, 2023, 08:02:04 am ---That's pretty slick. So I could assign values and Bob's your uncle.
type  direction = (up = 0, down = 1);

--- End quote ---

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  onoff     = (on, off);  updown    = (up, down);  leftright = (left, right); procedure flip(e: PInteger);begin  e^ := integer(not boolean(e^));end; var  switch: onoff = on; //initial "on" begin  writeln('switch = ', switch);  flip(@switch); //off  writeln('switch = ', switch);  flip(@switch); //on  writeln('switch = ', switch);  flip(@switch); //off  writeln('switch = ', switch);   readln;end.

--- Quote ---switch = on
switch = off
switch = on
switch = off
--- End quote ---

TRon:

--- Quote from: Kaller on September 20, 2023, 05:49:04 am ---Will generics do that for me?

--- End quote ---
Dunno tbh.

I like the following a bit more but I don't believe generic type helpers are supported (or planned).

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program test; {\$mode objfpc}{\$H+}{\$modeswitch typehelpers} type  THorDir = (hdLeft, hdRight);   THorDirHelper = type helper for THorDir    procedure Flip;  end; procedure THorDirHelper.Flip;begin  case ord(Self) of    0 : Ord(Self) := 1;    1 : Ord(Self) := 0;  end;end; var  Horizontal: THorDir; begin  Horizontal := hdLeft;  writeln('Horizontal = ', Horizontal);  Horizontal.Flip;  writeln('Horizontal = ', Horizontal);  Horizontal.Flip;  writeln('Horizontal = ', Horizontal);end. And ofc you can use Fibonacci's flip implementation code as well I just fancied mine  :).