is iterable

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

is iterable

Tony Polinelli



How do i do a check to see if a value is iterable?

eg:

if (Std.is(value, Iterable) {
      Lib.println("yes");
  } else {
      Lib.println("no");
  }

if i do this it say "Iterable is not a value"

cheers

--
Tony Polinelli
http://www.touchmypixel.com

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

Re: is iterable

Franco Ponticelli
The only way I know is to check if your object has both the next() and hasNext() functions (Reflect.hasField and optionalli Reflect.isFunction). The check cannot assure that they exist with the correct number of arguments.

Franco.

On Sun, Nov 30, 2008 at 6:41 AM, Tony Polinelli <[hidden email]> wrote:



How do i do a check to see if a value is iterable?

eg:

if (Std.is(value, Iterable) {
      Lib.println("yes");
  } else {
      Lib.println("no");
  }

if i do this it say "Iterable is not a value"

cheers

--
Tony Polinelli
http://www.touchmypixel.com

--
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: is iterable

Nicolas Cannasse
Franco Ponticelli a écrit :
> The only way I know is to check if your object has both the next() and
> hasNext() functions (Reflect.hasField and optionalli
> Reflect.isFunction). The check cannot assure that they exist with the
> correct number of arguments.

You can additionally check if it has an .iterator() method.

Nicolas

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

Re: is iterable

Franco Ponticelli
The only way I know is to check if your object has both the next() and hasNext() functions (Reflect.hasField and optionalli Reflect.isFunction). The check cannot assure that they exist with the correct number of arguments.

You can additionally check if it has an .iterator() method.

Nicolas is right, my check is for Iterator, not Iterable.

Franco

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

Re: is iterable

Tony Polinelli
thanks guys :P
i was thinking this would work but didnt know if there was a better way



On Sun, Nov 30, 2008 at 2:00 AM, Franco Ponticelli <[hidden email]> wrote:
The only way I know is to check if your object has both the next() and hasNext() functions (Reflect.hasField and optionalli Reflect.isFunction). The check cannot assure that they exist with the correct number of arguments.

You can additionally check if it has an .iterator() method.

Nicolas is right, my check is for Iterator, not Iterable.

Franco

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



--
Tony Polinelli
http://www.touchmypixel.com

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

Re: is iterable

Mike Stead
Was just wondering if this check had been placed in the core API yet? Iterators are one of the nice features of Haxe. Testing if something is Iterable would be very handy.
Reply | Threaded
Open this post in threaded view
|

Re: is iterable

Yanis Benson

Something is Iterable<T> if it has method iterator() returning Iterator<T>. There is no specific check.

On 3 Oct 2011 04:16, "Mike Stead" <[hidden email]> wrote:
> Was just wondering if this check had been placed in the core API yet?
> Iterators are one of the nice features of Haxe. Testing if something is
> Iterable would be very handy.
>
> --
> View this message in context: http://haxe.1354130.n2.nabble.com/is-iterable-tp1594477p6853566.html
> Sent from the Haxe mailing list archive at Nabble.com.
>
> --
> 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: is iterable

Mike Stead
There are Iterator<T> and Iterable<T> typedefs in StdTypes. If an object implements the api of one or the other it may be iterated over, i.e. it is then iterable. Because of this we need to check for both. Not a big issue but it means writing your own util, like the one below, for checking a core type, which seems like something which should be found in the Haxe.

public static function isIterable(value:Dynamic):Bool
{
        if (value == null)
                return false;
               
        var field = Reflect.field(value, "iterator");
        if (field != null && Reflect.isFunction(field))
                return true;
       
        field = Reflect.field(value, "hasNext");
        if (field != null && Reflect.isFunction(field))
        {
                field = Reflect.field(value, "next");
                if (field != null && Reflect.isFunction(field))
                        return true;
        }
        return false;
}

This can't check for params or return types so is not totally bullet-proof.