'unzipping' an Iterable of Iterables

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

'unzipping' an Iterable of Iterables

Justin Donaldson
I wanted to make a static method that would unzip a list of lists into a single list of elements.  Basically, I thought the method headers should look like:

public static function unzip<T>(it:Iterable<Iterable<T>>) : List<T>

which you could then use as:

unzip( [ [1,2], [3,4] ] );

which should produce

{1,2,3,4}

unfortunately, I end up getting an error...

ListDemo.hx:39: characters 8-31 : Array<Array<Int>> should be Iterable<Iterable<Unknown<0>>>
ListDemo.hx:39: characters 8-31 : Array<Array<Int>> should be { iterator : Void -> Iterator<Iterable<Unknown<0>>> }
ListDemo.hx:39: characters 8-31 : Invalid type for field iterator :
ListDemo.hx:39: characters 8-31 : Void -> Iterator<Null<Array<Int>>> should be Void -> Iterator<Iterable<Unknown<0>>>
ListDemo.hx:39: characters 8-31 : Array<Int> should be Iterable<Unknown<0>>
ListDemo.hx:39: characters 8-31 : Array<Int> should be { iterator : Void -> Iterator<Unknown<0>> }
ListDemo.hx:39: characters 8-31 : For function argument 'it'

I've tried a couple of other things, but I bet I missing something simple.  TIA...

-Justin


--
Justin Donaldson
PhD Candidate, Informatics
Indiana University
http://www.scwn.net

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: 'unzipping' an Iterable of Iterables

Nicolas Cannasse
Justin Donaldson a écrit :
> I wanted to make a static method that would unzip a list of lists into a
> single list of elements.  Basically, I thought the method headers should
> look like:
>
> public static function unzip<T>(it:Iterable<Iterable<T>>) : List<T>

There is no variance on type parameters in haXe. This is a bit tricky
and has been discussed several times, please check the archives.

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: 'unzipping' an Iterable of Iterables

clemos
Maybe this helps :
http://ncannasse.fr/blog/haxe_polymorphism_and_variance

On Wed, Nov 12, 2008 at 12:33 PM, Nicolas Cannasse
<[hidden email]> wrote:

> Justin Donaldson a écrit :
>>
>> I wanted to make a static method that would unzip a list of lists into a
>> single list of elements.  Basically, I thought the method headers should
>> look like:
>>
>> public static function unzip<T>(it:Iterable<Iterable<T>>) : List<T>
>
> There is no variance on type parameters in haXe. This is a bit tricky and
> has been discussed several times, please check the archives.
>
> Best,
> Nicolas
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: 'unzipping' an Iterable of Iterables

Justin Donaldson
Thanks for that post and info... that plus some more digging help explain the situation in more detail.  It seems like I keep running into this when I try to do something fancy with Iterables.

so...  unzip([ [1,2,3], [4,5,6] ]) won't work... even though the types match

but...

unzip([ [1,2,3], [4,5,6.0] ]) will work, even though the types are different...

I remember this same sort of thing coming up earlier in the list, I'll do some more digging.

Best,
-Justin






Best,
-Justin





 

On Wed, Nov 12, 2008 at 12:03 PM, clemos <[hidden email]> wrote:
Maybe this helps :
http://ncannasse.fr/blog/haxe_polymorphism_and_variance

On Wed, Nov 12, 2008 at 12:33 PM, Nicolas Cannasse
<[hidden email]> wrote:
> Justin Donaldson a écrit :
>>
>> I wanted to make a static method that would unzip a list of lists into a
>> single list of elements.  Basically, I thought the method headers should
>> look like:
>>
>> public static function unzip<T>(it:Iterable<Iterable<T>>) : List<T>
>
> There is no variance on type parameters in haXe. This is a bit tricky and
> has been discussed several times, please check the archives.
>
> Best,
> Nicolas
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

--
haXe - an open source web programming language
http://haxe.org



--
Justin Donaldson
PhD Candidate, Informatics
Indiana University
http://www.scwn.net

--
haXe - an open source web programming language
http://haxe.org