HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Clarify Your Code in the Functional Style"
Volume: 4 Issue: 4, p. 58

	

Listing 1.
 
// this is the class that 
// implements the Scheme ımapı command 
class Map extends BuiltIn 
{ 
    public SchemeObject Apply(SchemeObject args, Environment env) 
    { 
        return map_aux(args.first(), args.second(), 
            (args.Length() > 2) ? (args.restl()).restl() : null, 
            env); 
    } 
    private SchemeObject map_aux(SchemeObject f, 
        SchemeObject ls, SchemeObject more, Environment env) 
    { 
        if(more == null) return map_aux1(f, ls, env); 
        else             return map_aux_more(f, ls, more, env); 
    } 
    private SchemeObject map_aux1(SchemeObject f, 
        SchemeObject ls, Environment env) 
    { 
        if(ls.Nullp()) return SchemeObject.False; 
        else 
            return SchemeObject.cons( 
                f.Apply(SchemeObject.cons(ls.first(), 
                SchemeObject.False), 
                env), 
                map_aux1(f, ls.restl(), env)); 
    } 
    private SchemeObject map_aux_more(SchemeObject f, 
        SchemeObject ls, SchemeObject more, Environment env) 
    { 
        if(ls.Nullp()) return SchemeObject.False; 
        else 
            return SchemeObject.cons( 
                f.Apply( 
                    SchemeObject.cons( 
                        ls.first(), 
                        map_aux(First.car, more, null, env) 
                    ), 
                    env), 
                    map_aux_more(f, ls.restl(), 
                        map_aux(Rest.cdr, more, null, env), 
                        env)); 
    } 
} 
  

Listing 2.
 
// This code implements the Scheme "begin" syntax, which 
//  executes a number of expressions in sequence. 
//  Syntax: (begin exp1 Š) 
public final SchemeObject Apply(SchemeObject args, 
    Environment env) 
  throws SchemeException 
{ 
  return begin_aux(args, args.Length(), env); 
} 
private final SchemeObject begin_aux(SchemeObject args, int len, 
    Environment env) 
  throws SchemeException 
{ 
  switch(len) 
  { 
    case 0: return False; 
    case 1: return args.first().Eval(env); 
    default: 
      args.first().Eval(env); 
// we evaluate the first statement, then call begin_aux 
// recursively on the rest of the statements 
      return begin_aux(args.restl(), len - 1, env); 
  } 
} 


  

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.