LoopCrement Utility Function

This is a moderated forum that collects tutorials, guides, and references for creating Transcendence extensions and scripts.
Post Reply
User avatar
Star Weaver
Militia Commander
Militia Commander
Posts: 311
Joined: Sun Nov 07, 2010 10:20 pm
Location: . . . between the stars and the warm black sky . . .

This is a handy little thing that I have in python and wanted to reipmlement in tlisp, plus a couple functions I used to test it:

Code: Select all

<Globals>(block nil
(setq loopCrement (lambda (current start stop increment) (block (new)
	(setq new (add current increment))
	(switch 
		(and (gr increment 0) (gr new stop))
		(setq new (add start (subtract new stop) -1 ))
		(and (ls increment 0) (ls new start))
		(setq new (add 1 (subtract stop (subtract start new))))
	)
	new
)))

(setq lctest (lambda () (block nil
	(setq x 5)
	(for i 1 10 (dbgOutput (setq x (loopCrement x 5 12 1))))
	(dbgOutput "-----------")
	(for i 1 10 (dbgOutput (setq x (loopCrement x 5 12 -1))))
)))

(setq lctest2 (lambda () (block nil
	(setq x 5)
	(for i 1 10 (dbgOutput (setq x (loopCrement x 5 12 3))))
	(dbgOutput "-----------")
	(for i 1 10 (dbgOutput (setq x (loopCrement x 5 12 -3))))
)))
)</Globals>
Um, yeah, what's it good for: It's good for any time you have, say, a list of items, and you want to let the user page through them with looping in either direction and with the option to move more than one item at a time. So, a hypothetical setup -- this is totally ass pull pseudocode at the moment, I know screens don't look like this but I don't have the format handy.

Code: Select all


<init screen>
  (setq pages (list 'page1 'page2 'page3 'page4 'page5))
  (setq curpage 0)
</init screen>

<init pane>
  (setPaneDesc gScreen (cat "Currently viewing " (item pages curpage) "."))
</init pane>

<action next>
  (setq curpage (loopCrement curpage 0 (count pages) 1)
</action next>

<action previous>
  (setq curpage (loopCrement curpage 0 (count pages) -1)
</action previous>

<action pagedown>
  (setq curpage (loopCrement curpage 0 (count pages) 5)
</action pagedown>

<action pageup>
  (setq curpage (loopCrement curpage 0 (count pages) -5)
</action pageup>


I got the idea to reimplement this function when I saw TransGeek doing something like this with his inventory categories mod, and doing some clunky loop-end checkings in the up/down actions for it.
Last edited by Star Weaver on Thu May 10, 2012 10:07 pm, edited 1 time in total.
Image
Image
Image
User avatar
Atarlost
Fleet Admiral
Fleet Admiral
Posts: 2391
Joined: Tue Aug 26, 2008 12:02 am

At the moment this is almost completely unhelpful. You need to answer a couple questions and preferably edit the answers into the first post as well.
  • What does it do?
  • What is it handy for?
Literally is the new Figuratively
User avatar
Star Weaver
Militia Commander
Militia Commander
Posts: 311
Joined: Sun Nov 07, 2010 10:20 pm
Location: . . . between the stars and the warm black sky . . .

Atarlost wrote:At the moment this is almost completely unhelpful. You need to answer a couple questions and preferably edit the answers into the first post as well.
  • What does it do?
  • What is it handy for?
It changes a variable by the given value while forcing it to stay within the given range, by looping over the range when you go off either end of it. It's handy for letting a user move over a bounded set of things, especially if the size of that set is dynamic.

In retrospect, there should probably be a modulo in there, to handle cases where the increment is larger than the entire range, but that's not been an issue I ran into with it before.
Image
Image
Image
Post Reply