Java interop

69 views
Skip to first unread message

xificurC

unread,
May 28, 2015, 4:38:42 AM5/28/15
to yeti...@googlegroups.com
Hi there,

I just started playing around with Yeti. I love ML and would appreciate one running on top of the JVM. The language so far seems nice enough but I have issue doing reasonable java interop. Let's say I want to filter a ~java.lang.Object[] some way. The most concise way would be

-------
filter do o: o#toString() == 'foo' done my_obj_list
-------

but as the tutorial points out this doesn't work (I'm not sure of the technical reasons) :( . So my next attempt was (just a little bit more verbose)

-------
filter do o is ~java.lang.Object -> boolean: o#toString() == 'foo' done my_obj_list
-------

but I get another error on `is'. Does it mean anonymous functions can't be type annotated? In the end I end up writing

-------
my_filt o is ~java.lang.Object -> boolean = o#toString() == 'foo';
filter my_filt my_obj_list
-------

which is now too verbose for my taste. I have to declare a function that I'm only going to use once and I have to write out the types too. Is there a simpler way to this? I wish something similar to yeti's field notation (.foo) would be available on java objects, then one could just write

-------
filter ((=='foo') . (#toString())) my_obj_list
-------

As I said, the language seems nice but the interop seems a bit clunky and one will surely use java libraries when writing yeti code.

Thanks for this project!

xificurC

unread,
May 28, 2015, 4:38:42 AM5/28/15
to yeti...@googlegroups.com
Hi, I just started playing around with Yeti. On the first glance it's very nice, but I'm having issues doing java interop in a nice fashion. Let's say I have a list of java.lang.Objects (or any other java objects) and I want to filter this list, I first try

filter do x: x#toString() == 'foo' done my_list

This (as the tutorial states) doesn't work as for java objects often types need to be declared. So next I try

filter do x is ~java.lang.Object -> boolean: x#toString() == 'foo' done my_list

but that doesn't work either, I get a `bad argument: is' error. Can anonymous functions not be type annotated? I end up having to write

my_filter x is ~java.lang.Object -> boolean = x#toString() == 'foo';
filter my_filter my_list

This is rather verbose. Is there any shorter way to do this?

Another example - map a method over a list. Yeti has nice syntax so that you can write

map (.foo) my_list

for a list of structs that have a foo field. I don't see an equivalently nice solution to e.g. calling a java method like

map (#foo()) my_list

I end up writing

my_fun x is ~java.foo.bar -> string = x#foo();
map my_fun my_list

Am I missing something? Any tips appreciated. I really like ML and would love to play around with Yeti, but one needs to use java libs extensively anyway and I'd need to clean up the interop a bit.

xificurC

unread,
May 28, 2015, 4:46:50 AM5/28/15
to yeti...@googlegroups.com
Apologies for almost identical posts, the first one I actually posted yesterday but didn't seem to get published for some reason so I rolled my sleeves down and rewrote it today. Now both came out :S

Madis Janson

unread,
May 28, 2015, 4:50:34 AM5/28/15
to yeti...@googlegroups.com

On Wed, 27 May 2015, xificurC wrote:

> Hi, I just started playing around with Yeti. On the first glance it's very nice, but I'm having
> issues doing java interop in a nice fashion. Let's say I have a list of java.lang.Objects (or any
> other java objects) and I want to filter this list, I first try
>
> filter do x: x#toString() == 'foo' done my_list
>
> This (as the tutorial states) doesn't work as for java objects often types need to be declared. So
> next I try
>
> filter do x is ~java.lang.Object -> boolean: x#toString() == 'foo' done my_list
>
> but that doesn't work either, I get a `bad argument: is' error. Can anonymous functions not be
> type annotated? I end up having to write
>
> my_filter x is ~java.lang.Object -> boolean = x#toString() == 'foo';
> filter my_filter my_list
>
> This is rather verbose. Is there any shorter way to do this?

You can write:

filter do x: (x is ~Object)#toString() == 'foo' done my_list;

or:

filter ((== 'foo') . string) my_list

or:

my_list |> map string |> filter (== 'foo')

> Another example - map a method over a list. Yeti has nice syntax so that you can write
>
> map (.foo) my_list
>
> for a list of structs that have a foo field. I don't see an equivalently nice solution to e.g.
> calling a java method like
>
> map (#foo()) my_list
>
> I end up writing
>
> my_fun x is ~java.foo.bar -> string = x#foo();
> map my_fun my_list
>
> Am I missing something? Any tips appreciated. I really like ML and would love to play around with
> Yeti, but one needs to use java libs extensively anyway and I'd need to clean up the interop a
> bit.

Here shortest would be about the same as above:

map (_ x = (x is ~java.foo.bar)#foo()) my_list;

xificurC

unread,
May 28, 2015, 7:11:20 AM5/28/15
to yeti...@googlegroups.com, ma...@cyber.ee
Hi Madis,

thank you for the quick response. Type annotation inside the function definition is concise enough, thanks for that.

One more question that just came to my mind: in java you can e.g. import javax.swing.* but this doesn't work in yeti, is there an equivalent?

Mateusz Czaplinski

unread,
May 28, 2015, 10:21:42 AM5/28/15
to yeti...@googlegroups.com
On Thu, May 28, 2015 at 10:50 AM, Madis Janson <ma...@cyber.ee> wrote:
On Wed, 27 May 2015, xificurC wrote:
filter do x is ~java.lang.Object -> boolean: x#toString() == 'foo' done my_list

but that doesn't work either, I get a `bad argument: is' error. Can anonymous functions not be
type annotated? I end up having to write

my_filter x is ~java.lang.Object -> boolean = x#toString() == 'foo';
filter my_filter my_list

This is rather verbose. Is there any shorter way to do this?

You can write:

filter do x: (x is ~Object)#toString() == 'foo' done my_list;

Woooo, I wasn't aware of that! I always only did (x unsafely_as ~Object), but this will propagate the type properly (in opposition to my old approach) IIUC. This should hopefully simplify some of my code nicely!

Btw, @xificurC, if you're interested by any chance, I've written an Android app with Yeti, you can see it at: https://github.com/akavel/bookshelf (though I totally haven't written a pretty README yet, sorry; that's one of the things somewhere on my veeeeeeery long "Hobby Projects Todo List"...)

/Mateusz.
Reply all
Reply to author
Forward
0 new messages