Haxe utility method to reverse a map?

97 views
Skip to first unread message

Aaron Pieper

unread,
Feb 24, 2017, 7:20:48 PM2/24/17
to Haxe
I'd like to reverse a map, so given input like ["a" => 1, "b" => 2] i'd like to generate output like [1 => "a", 2 => "b"]. Seems simple enough but I'm encountering some headaches related to generics:

public static function reverse<T:Dynamic, U:Dynamic>(map:Map<T, U>):Map<U, T> {
 
var result = new Map<U, T>();
 
for (key in map.keys()) {
  result
[map[key]] = key;
 
}
 
return result;
}

The above code compiles OK, but produces a runtime error:

TypeError: Error #1034: Type Coercion failed: cannot convert Object@73f1509 to flash.utils.Dictionary.

Confusingly, the error is occurring on the map.keys() invocation. The error even occurs if my method simply invoke map.keys() and does nothing else. Does anybody understand why this error is occurring? Presumably it's a problem with my method declaration as the implementation is quite straightforward.

Also, I'm not trying to reinvent the wheel here, so if there is some Haxe equivalent of MapUtils.invertMap() that I overlooked, that's great too.

Justin Donaldson

unread,
Feb 24, 2017, 9:39:06 PM2/24/17
to Haxe
You've got some of the type declarations as "Dynamic" there.  If you remove those and leave T and U as free parameters, you'll see that the compiler is telling you that it can't swap them.

I believe this boils down to runtime limitations.  But, I'm not 100% sure.

-Justin






--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Justin Donaldson

unread,
Feb 25, 2017, 3:25:39 AM2/25/17
to Haxe
For fun, here's an abstract type that converts between Map<Int,String> and Map<String,Int> on demand, depending on what kind of map you need.


Note that these automatic conversions are destructive operations.  Keys are guaranteed to be unique, values are not.  Converting one to the other stands the chance of losing information.

-Justin

Justin L Mills

unread,
Feb 26, 2017, 12:58:51 PM2/26/17
to haxe...@googlegroups.com

The problem seems to be that Haxe compiler needs to know the real type of a Map when creating it, and by putting it in a function like that, it can't know the type. The trick is to create the Map outside the function and only populate it within your function, this does not really seem a big compromise most of the work is still implemented in your static function and you won't get additional runtime overheads.

    public static function reversed<T,U>(map1:Map<T, U>,map2:Map<U,T>):Map<U,T> {
         for (k in map1.keys()) map2.set( map1.get(k), k );
         return map2;
    }

http://try-haxe.mrcdk.com/#b3B1F

Hope that helps :)

szczepan

unread,
Feb 26, 2017, 1:19:15 PM2/26/17
to Haxe
But what about '@:generic'? :D

http://try-haxe.mrcdk.com/#2CD40

David Elahee

unread,
Feb 26, 2017, 1:37:18 PM2/26/17
to haxe...@googlegroups.com
In haxe, maps are unordered, reversing them doesn't "mean" something... you have to use a third party structure to enforce an order.


2017-02-26 19:19 GMT+01:00 szczepan <szcze...@gmail.com>:
But what about '@:generic'? :D

http://try-haxe.mrcdk.com/#2CD40
--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.



--
David Elahee


Aaron Pieper

unread,
Feb 26, 2017, 10:13:20 PM2/26/17
to Haxe
Great thanks, I'll try this out!
Reply all
Reply to author
Forward
0 new messages