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]
Other format: [Raw text]

Re: LList makeList


Thanks for the suggestions, Chris! I decided to take your suggestion of
using a function to map over the collection. Instead of writing one
specifically for collections, though, I took the original map.java file
and created a mapx.java that works with scheme lists & sequences
(vector,string), java collections, iterators, enumerations and arrays.
You pay a penalty in instanceof calls, but at least it is per invocation
and not per list item.
If anyone wants the code, you can download it from:
http://www.wutka.com/download/mapx.java

Since it is really just Kawa's map.java with some mods, the license is GPL.

If mapx.class is visible in your classpath, you can just do:
(require <mapx>)
This gives you both mapx and for-eachx.

(require <mapx>)

(define (display-nl x) (display x)(newline))

(define jht (make <java.util.Hashtable>))
(java.util.Hashtable:put jht "Moe" "Howard")
(java.util.Hashtable:put jht "Larry" "Fine")
(java.util.Hashtable:put jht "Curly" "Howard")

; Enumeration
(for-eachx display-nl (java.util.Hashtable:keys jht))
; Iterator
(for-eachx display-nl (java.util.Set:iterator java.util.Hashtable:keySet jht)))
; Enumeration again
(for-eachx display-nl (java.util.Hashtable:elements jht))
; Collection
(for-eachx display-nl (java.util.Hashtable:values jht))

(define ja ((primitive-array-new <int>) 20))
(do ((i 0 (+ i 1))) ((> i 19) #t) ((primitive-array-set <int>) ja i i))

(define sl '(3 8 57 46 25 39 40))
(define vec (make-vector 15 4))

; Map over java array, scheme list & scheme vector at the same time
(display-nl (mapx (lambda (a b c) (+ (* a b) c)) ja sl vec))(newline)

  Mark

" 
" > I was wondering what kind of support Kawa had for Java collections and
" > saw the messages about using gnu.lists.LList:makeList to convert a Java
" > list into a Scheme list. 
" 
" There are many ways to do this.  The simplest Scheme functions I use
" are:
" 
"     (define (iterator->list iter)
"       (let loop ((acc '()))
"         (if (java.util.Iterator:hasNext iter) 
"             (loop (cons (java.util.Iterator:next iter) acc))
"             (reverse! acc))))
" 
"     (define (collection->list coll)
"       (iterator->list (java.util.Collection:iterator coll)))
" 
" But it "real code" I'll just use a function that maps over collection
" directly rather than convert it to a list and then call the R5RS map.
" 
" I can send you hash-table-map etc. if you send me a private email.
" 
" Regards,
" Chris Dean
" 


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