We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I was wondering if it was possible to pass functions in an array.
I have seven functions that produce patterns, I wanted to know if I could put them in an array which I can then use in a 'for i' statement and rotate each odd number in the array anti-clockwise and each even number clockwise.
Answers
You have to wrap each function in a class.
Example, using anonymous classes made after an interface:
Java doesn't have the
function
type! Hence it can't store such references in variables! 8-XHowever, we can use methods from interfaces & classes as sorta replacement as @PhiLho has shown above.
Some other languages can though. Like JavaScript, Lua, C, etc.! :ar!
Java 8's gonna get something similar soon! <:-P
Wow, cool (Java 8)
Yea, just use a class.
I wouldn't have my hopes too high too soon in regards to Processing + Java 8.
Shamefully, Processing's pre-processor got problems even w/ something as basic as
enum
.Imagine when dealing w/ lambda expressions or method references!
I usually just store a commandID and name the different commands (which are just int)
and then I have a function somewhere
Java 7 accepts String for
switch ()
! :-cTo be pedantic, encapsulating a single function inside a general class is called the Command pattern. It is a very useful design pattern to know. :-)