This is the mail archive of the kawa@sources.redhat.com mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

example contribution


A little example for the docs...
Marco

package scheme;
import gnu.lists.*;
import gnu.expr.*;
import kawa.lang.*;
import java.util.*;
/**
 * Assoc.java
 * exports hash->assoc and assoc->hash
 * load with (require <scheme.Assoc>)
 * Created: Tue Oct 23 14:35:00 2001
 *
 * @author 
 * @version
 */

public class Assoc extends ModuleBody {
  
  public Assoc() {
    
  }

  public final ModuleMethod hash$To$Assoc=new
ModuleMethod(this,0,"hash->assoc",1);
  public final ModuleMethod assoc$To$Hash=new
ModuleMethod(this,1,"assoc->hash",1);

  public LList hash$To$Assoc(Hashtable h){
    LList ret=LList.Empty;
    for(Enumeration e=h.keys();e.hasMoreElements();){
      Object key=e.nextElement();
      Object value=h.get(key);
      ret=new Pair(new Pair(key,new Pair(value,LList.Empty)),ret);
    }
    return ret;
  }
  
  public Hashtable assoc$To$Hash(LList a){
    Hashtable ret=new Hashtable();
    for (Enumeration e=a.elements();e.hasMoreElements();){
      Object thing=e.nextElement();
      if (thing instanceof Pair){
	Pair pair=(Pair) thing;
	if (pair.cdr instanceof Pair){
	  ret.put(pair.car,((Pair)pair.cdr).car);
	}else{
	  ret.put(pair.car,pair.cdr);
	}
      }
    }
    return ret;
  }

  public Object applyN(ModuleMethod m, Object [] arg){
    switch (m.selector){
    case 0:
      return hash$To$Assoc((Hashtable)arg[0]);
    case 1:
      return assoc$To$Hash((LList)arg[0]);
    default:
      throw new IllegalArgumentException();
    }
  }
} // Assoc

-- 
	(--cafe babe--) 
Marco Vezzoli	marco.vezzoli@st.com
CR&D Intranet Developement   STMicroelectronics
tel. +39 039 603 6852 fax. +39 039 603 5055


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]