Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

How do I do this?|

Name: javaboi 2009-04-23 11:43

Hi dudes, I'm wondering about how I can do something like this in java

func(){print "hi"}
func2(){print " there"}

tab[] funcs = {"func", "func2"}
for(int i = 0; i < 2; i++){
funcs[i]()}

resulting in "hi there"

I think this is called anonymous functions in some other codans.

Name: Anonymous 2009-04-23 11:52


func(){print "hi"}
func2(){print " there"}

tab[] funcs = {"func", "func2"}
for(int i = 0; i < 2; i++){
funcs[i]()}


with tags

Name: Anonymous 2009-04-23 12:01

>>1
Did you mean: Javascript.

Name: Anonymous 2009-04-23 12:04

I think this is called anonymous functions in some other codans.
Anonymous functions don't have names!!!

Name: Anonymous 2009-04-23 12:11

>>4
Lies. Anonymous people have names, they just choose to not let them be known. The same goes for anonymous functions.

Name: Anonymous 2009-04-23 12:17

Sorry about mentioning the anonymous functions.

>>3
No, I mean java

>>4
sorry about that

I just want to do something like >>2

Name: HMA ENTHUSIAST 2009-04-23 12:24

Use Clojure.

Name: Anonymous 2009-04-23 12:53

ENTERPRISE enough for you?

public class stofnlok {
    public static void func() { System.out.print("hi"); }
    public static void func2() { System.out.println(" there"); }

    public static void main(String args[])
    {
        try {
            String funcs[] = { "func", "func2" };
            for (String f : funcs)
                stofnlok.class.getMethod(f).invoke(null);
        } catch (Throwable e) {
            System.out.println("WHO GIVES A FUCK, YOU PATHETIC EXCUSE FOR A LANGUAGE?");
        }
    }
}
/*
 GRUNNUR
 ; */

Name: Anonymous 2009-04-23 13:04

ArrayList<Runnable> funcs = new ArrayList<Runnable>();
funcs.add(new Runnable() {
     public void run() {
         System.out.print("hi");
     }
});
funcs.add(new Runnable() {
    public void run() {
         System.out.print(" there");
    }
});

for (int i = 0; i < funcs.size(); i++) {
     funcs.get(i).run();
}

Name: Anonymous 2009-04-23 13:05

>>8
Thanks, seems like I won't use it very much. At least not in java

Name: Anonymous 2009-04-23 13:08

>>8
No, you didn't import Class, the class itself has a lower case first letter, the indentation of the first two functions- above and beyond being ``perversely awful'' is inconsistent with the main function. Your array declaration style implies you are creating a String which encodes or otherwise represents an array of "funcs" where it should be an array of strings, each representing a func. Using a foreach loop for an array with two elements in it not only reduces readability, but effeciency of the program and compiler as well. Catching "Throwable" in itself is a laughable idea, but what is worse is that you have caught it at the end of a main method, and in the catch clause proceed to print out a wholly worthless message before letting the program terminate. Simply adding a throws clause to your main function declaration will be quicker, more elegant and actually provide useful debugging messages if an error occurs. The function names func and func2 do nothing to tell the reader what it is they do and if you were attempting to black box this class for abstraction purposes other programmers would have a relatively difficult time figuring out what they do. Invoking a method with null arguments will produce a compiler cast warning, and as such it is bad style to do so. You should explicitly cast the arguments yourself. On top of all of this, you asked if the program was ``enterprise'' enough and yet makes little use of enterprise best practices and development techniques. func and func2, would be better off as seperate classes that extend an abstract printing class to ensure there is no code duplication and allow for maximum maintainability. The only comment you have is cyptic and by and large would be ignored by any programmer reading and trying to understand this code. You have also enclosed your "funcs" declaration within the try catch block which will render it unusable later in the function should you decide to add to it. I hope you enjoy your Haskell and Lisp, and unemployment.

Name: Anonymous 2009-04-23 13:13

>>11
Your ideas intrigue me and I would like to subscribe to your newsletter.

Name: Anonymous 2009-04-23 13:16

>>11
td;wihr

Name: Anonymous 2009-04-23 13:33

I think this is called anonymous functions in some other codans.
Those aren't anonymous functions, in the sense that you already named them func and func2.

Name: Anonymous 2009-04-23 19:21

Here, but you need to be running a Java7 (Dolphin) JDK to compile it.
[sub]In before YOU HELPED HIM!!!!

public class Funcs {
  private Function func = void () { System.out.println "hi"; };
  private Function func = void () { System.out.println " there"; };

  public static void main(String[] args) {
    Function[] funcs = new Function[] {func, func2};
    for(Function func : funcs){
      func.invoke(null, null);
    }
  }
}

Name: Anonymous 2009-04-23 19:47

>>11
I loved your post until you used faggot quotes. Why would you go and hax my anus after haxxing >>8's anus?

Name: Anonymous 2009-04-23 20:03

>>16
Just a quick reminder, we all know you posted >>8, as you're still the only one using your forced failmemes.

Name: Anonymous 2009-04-23 21:41

>>8
Silly Java programmer, stofnlok goes at the end of a code block!

"funcs" < aðal
{
aðal ->
    stef(;)
    staðvær funcs, f
    stofn
        funcs := [stef func1(0;0), stef func2(0;0)],
        fyrir(; funcs; funcs:=\hali funcs) lykkja
            f := \haus funcs,
            f(;),
        lykkjulok,
    stofnlok
}
*
{
    func1 ->
        stef(;)
        stofn
            skrifalínu(;"hi"),
        stofnlok
    func2 ->
        stef(;)
        stofn
            skrifalínu(;" there"),
        stofnlok
}
*
"GRUNNUR"
;

Name: Anonymous 2009-04-23 21:52

>>17
>>16 here. I can actually properly spout memes such as ENTERPRISE TROLLING unlike >>8. As you might notice, >>8's "GRUNNUR" is also incomplete.

I regret to inform you that I am not the same as >>8.

Name: Anonymous 2009-04-23 22:03

>>19
Not sure if believe, but don't really care.
Anyway, what I wanted to say is, you both suck. HAND.

Name: Anonymous 2009-04-23 23:55

>>17
>>8 here. what.

Name: Anonymous 2009-04-24 0:37

The proper way to do this is to use an anonymous object where you declare a new instance of an interface together with an inline implementation of the callback method of the interface, like what >>9 did.

Name: Anonymous 2009-04-24 1:28

>>22
ENTERPRISE JAVA CONSULTANT

Name: Anonymous 2009-04-24 2:12

The proper way to do this in java is not to do it.  If you want function pointers, go program in another language.  Function pointers are only supported in java through code ugliness and performance degradation.  Best to avoid whenever possible.

Name: Anonymous 2009-04-24 2:26

>>24
Wholly incorrect. I don't think you understand how Java deals with methods and functions. Enjoy your ``elegant'' void *(*funcs[2])(void).

Name: Anonymous 2009-04-24 2:53

>>25
Java doesn't have functions, only methods

Name: Anonymous 2009-04-24 3:23

>>26
It's arguable. Some say methods belong to instances of a class, so call static methods functions. Either way, to say method and function are not interchangable words is silly- although admittedly, what I would expect from /prog/.

Name: Anonymous 2009-04-24 3:42

>>26
`import static` disagrees.

Name: Anonymous 2009-04-24 3:43


print "hi there";

Name: >>29 2009-04-24 3:45

i mean
System.out.print "hi there";

Name: Anonymous 2009-04-24 4:16

>>1-2
How are they functions if they have side effects?

Name: Anonymous 2009-04-24 4:29

>>31 In computer science, a function or expression is said to produce a side effect if it modifies some state in addition to returning a value.

Name: Anonymous 2009-04-24 4:55

>>31
Their are impure functions.

Name: Anonymous 2009-04-24 5:03

>>33
The word you're looking for is ``subroutines''.

Name: Anonymous 2009-04-24 7:25

>>34
The acronym your looking for is "IHBT"

Name: Anonymous 2009-04-24 11:33

>>35
your

Name: Anonymous 2009-04-24 12:33

>>36
This just in: YHBMT

Name: Anonymous 2010-12-17 1:22

Are you GAY?
Are you a NIGGER?
Are you a GAY NIGGER?

If you answered "Yes" to all of the above questions, then GNAA (GAY NIGGER ASSOCIATION OF AMERICA) might be exactly what you've been looking for!

Don't change these.
Name: Email:
Entire Thread Thread List