How to make a "shortcut"?

Notices and updates
Locked
ale870
Posts: 297
Joined: Mon Nov 26, 2007 8:01 pm
Location: Italy

How to make a "shortcut"?

Post by ale870 »

Sorry, but I could not find any good subject title for my question!

Well, I have this problem:

I created a function which manage text in a 2D engine... here an example:

Code: Select all

(simple-text-create "fldOne" "Content-One!")
It works but, if someone wants to create many text fields, he needs to write a lot of text:

Code: Select all

(simple-text-create "fldOne" "Content-One!")
(simple-text-create "fldTow" "Content-Two!")
(simple-text-create "fldThree" "Content-Three!")

(simple-text-update "fldOne" "My New Text")
(simple-text-update "fldTwo" "My New Text")
(simple-text-update "fldThree" "My New Text")
In order to avoid to repeat "simple-text-create" everytime, I wanted to create a "shortcut", like this (just a concept, not real code):

Code: Select all

(simple-text 
    create ( ("fldOne" "Content-One!")
              "fldTwo" "Content-One!"
    create "fldThree" "Content-One!"
    
    update "fldOne" "My New Text"
    update "fldTwo" "My New Text"
    update "fldThree" "My New Text"
    )
My problem is I don't want to write-down another function "simple-text" that wraps the other ones.

I could even imagine a syntax shortcut like this:

Code: Select all

(simple-text
	(create 
		("fldOne" "My content")
		("fldTwo" "My content")
		("fldThree" "My content")
	) )
Can I use a function like MAP (or another one!) to speed-up this process?

I don't want to write these functions again since I'm mapping hundreds of them, so if I need to write references more than once I could easily make mistakes, and my program could become unreliable.

Thank you for your help!
--

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

There two ways to do it, first using 'map':

Code: Select all

(set 'ids '(
"fldOne"
"fldTow"
"fldThree"
))

(set 'content '(
"Content-One!"
"Content-Two!"
"Content-Three!"
))

(map simple-text-create ids content)
the other method has both parameters in a list and uses 'apply':

Code: Select all

(set 'params '(
("fldOne" "Content-One!")
("fldTow" "Content-Two!")
("fldThree" "Content-Three!")
))

(dolist (p params)
	(apply simple-text-create p)
)

; or using 'map' again with 'curry'

(map (curry apply simple-text-create)  params)

ale870
Posts: 297
Joined: Mon Nov 26, 2007 8:01 pm
Location: Italy

Post by ale870 »

Thank you, I will check if (and how) I can implement in my code.
--

Locked