From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 73529 invoked by alias); 3 Feb 2017 23:24:18 -0000 Mailing-List: contact kawa-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: kawa-owner@sourceware.org Received: (qmail 73430 invoked by uid 89); 3 Feb 2017 23:24:03 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.5 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 spammy=universe, ar, 212, 820 X-HELO: aibo.runbox.com Received: from aibo.runbox.com (HELO aibo.runbox.com) (91.220.196.211) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 03 Feb 2017 23:23:53 +0000 Received: from [10.9.9.211] (helo=mailfront11.runbox.com) by mailtransmit03.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1cZnCn-0005GN-L4; Sat, 04 Feb 2017 00:23:49 +0100 Received: from 70-36-239-8.dsl.dynamic.fusionbroadband.com ([70.36.239.8] helo=localhost.localdomain) by mailfront11.runbox.com with esmtpsa (uid:757155 ) (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.82) id 1cZnCR-0002pK-W9; Sat, 04 Feb 2017 00:23:28 +0100 Subject: Re: Java arrays to Scheme To: Peter Lane , kawa@sourceware.org References: <875fc6cc-c8e6-7eca-4b47-4f02e6195df4@peterlane.info> From: Per Bothner Message-ID: <24011b6d-cc5f-36a5-1ff5-6118e8aea307@bothner.com> Date: Fri, 03 Feb 2017 23:24:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.6.0 MIME-Version: 1.0 In-Reply-To: <875fc6cc-c8e6-7eca-4b47-4f02e6195df4@peterlane.info> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2017-q1/txt/msg00048.txt.bz2 On 02/03/2017 12:33 PM, Peter Lane wrote: > Hi list, > > I'm trying to understand how to interact with Java functions which return arrays, e.g. double[][] or int[]. > > When I call a function which returns an int[] or double[][] they display as numbers within [ ... ] square brackets. I can extract their length and elements, e.g. using (x 0) etc where x is the array. > > What kind of object are these in the Scheme universe? array? vector? They are Java arrays. They are not Scheme vectors or arrays. However, they are "vector-like", and can be converted to other sequence types using splices. (The '!' operator is roughly the same as define-constant. Using either improves type inference in the REPL.) #|kawa:1|# (! ar (int[] 4 -5 6)) #|kawa:2|# ar [4 -5 6] > I would like to convert these Java arrays into a Scheme list or vector, which should be a simple function to write. But is there a function/trick like that already? #|kawa:3|# (vector @ar) #(4 -5 6) #|kawa:4|# (list @ar) (4 -5 6) #|kawa:5|# (s32vector @ar) #s32(4 -5 6) #|kawa:6|# (u32vector @ar) #u32(4 4294967291 6) Note the splice operator makes a copy - it does not share the array. However, you can use the ->sequence cast to make them share. (The syntax (->TYPE VALUE) is generally the same as (as TYPE VALUE).) #|kawa:7|# (->sequence ar) #s32(4 -5 6) #|kawa:8|# (! seq (->sequence ar)) #|kawa:9|# seq #s32(4 -5 6) #|kawa:10|# (set! (ar 2) 16) #|kawa:11|# ar [4 -5 16] #|kawa:12|# seq #s32(4 -5 16) #|kawa:13|# (set! (seq 0) 8) java.lang.ClassCastException: gnu.math.IntNum cannot be cast to java.lang.Integer at gnu.lists.S32Vector.setRaw(S32Vector.java:10) at gnu.lists.AbstractSequence.set(AbstractSequence.java:214) at gnu.kawa.functions.Setter$SetList.apply2(Setter.java:133) at gnu.mapping.Procedure.apply(Procedure.java:138) at gnu.mapping.Procedure.apply(Procedure.java:118) at gnu.mapping.CallContext.runUntilDone(CallContext.java:227) at gnu.expr.ModuleExp.evalModule2(ModuleExp.java:350) at gnu.expr.ModuleExp.evalModule(ModuleExp.java:212) at kawa.Shell.run(Shell.java:283) at kawa.Shell.run(Shell.java:196) at kawa.Shell.run(Shell.java:183) at kawa.repl.processArgs(repl.java:714) at kawa.repl.main(repl.java:820) Ooops - that really should work. But we can use a cast: #|kawa:14|# (set! (seq 0) (->int 8)) #|kawa:15|# seq #s32(8 -5 16) #|kawa:16|# ar [8 -5 16] Casting to a u32vector is trickier: #|kawa:17|# (->u32vector ar) /dev/stdin:17:14: warning - type int[] is incompatible with required type u32vector java.lang.ClassCastException: [I cannot be cast to gnu.lists.U32Vector at atInteractiveLevel-17.run(stdin:17) at gnu.expr.ModuleExp.evalModule2(ModuleExp.java:293) at gnu.expr.ModuleExp.evalModule(ModuleExp.java:212) at kawa.Shell.run(Shell.java:283) at kawa.Shell.run(Shell.java:196) at kawa.Shell.run(Shell.java:183) at kawa.repl.processArgs(repl.java:714) at kawa.repl.main(repl.java:820) Oops - that really should work, too ... You can use a raw constructor, though: #|kawa:19|# (gnu.lists.U32Vector ar) #u32(8 4294967291 16) -- --Per Bothner per@bothner.com http://per.bothner.com/