Search code examples
listequalitywren

How do I test if two lists are equal?


var a=[1,2,3]
var b=[1,2,3]
var c=[1,2,4]
System.print(a==b)
System.print(a==c)

How do I check if two lists are equal? I tried the equality operator == but this doesn't work; it prints false and false


Solution

  • There is no built-in operator or algorithm for list equality in wren - you must check the elements individually.

    An example algorithm is available here: https://github.com/minirop/wren-helpers/blob/master/algorithms/algorithm.wren#L6-L17

        static equal(list1, list2) {
            if (list1.count != list2.count) {
                return false
            }
            
            for (i in 0...list1.count) {
                if (list1[i] != list2[i]) {
                    return false
                }
            }
            return true
        }
    

    There is an open discussion on whether to add support for an equality list function: here https://github.com/wren-lang/wren/issues/606