11

I have a problem where I'm generating many values and need to make sure I only work with unique ones. Since I'm using node js, with the --harmony flag, and have access to harmony collections, I decided that a Set may be an option.

What I'm looking for is something similar to the following example:

'use strict';

function Piece(x,y){
  this.x = x  
  this.y = y
}

function Board(width,height,pieces){
 this.width = width 
 this.height = height
 this.pieces = pieces
}

function generatePieces(){
 return [
  new Piece(0,0),
  new Piece(1,1) 
 ] 
}

//boardA and boardB are two different but equivalent boards
var boardA = new Board(10,10,generatePieces()) 
var boardB = new Board(10,10,generatePieces())

var boards = new Set()
boards.add(boardA)
boards.has(boardB) //return true

Now normally to achieve this in another language, say c#, I would expect to have to implement an equals function, as well as a hash code generating function for both Board and Piece. Since I'd expect the default object equality to be based on references. Or perhaps use a special immutable value type (say, a case class in scala)

Is there a means to define equality for my objects to solve my problem?

Mike McFarland
  • 657
  • 4
  • 17

2 Answers2

8

Is there a means to define equality for my objects to solve my problem?

No not really. There has been some discussion about this on the mailing list. The result is:

  • Build your own Set/Map abstraction on top of Set/Map, which would convert the objects to a primitive value according to your hashing function.
  • Wait for value objects coming in ES7.
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
-2

This will do it for constructors like what you're working with.

var sameInstance = function(obj1, obj2){
    return obj2 instanceof ob1.constructor;
};

Some other types of objects you might need something different, but this should be ok for what you need.

The above is what you need in function form. To get it to work with Set you will have to inherit the Set object, and override the has method with your own has.

Quentin Engles
  • 2,744
  • 1
  • 20
  • 33
  • I'm not looking for an equality function, nor to change the behavior of the has method. That was only an example, I'm looking to be able to define equality for my own object. That way duplicate objects wouldn't be contained within the set. – Mike McFarland Nov 24 '14 at 03:31
  • Apologies mike. – Quentin Engles Mar 01 '17 at 14:46
  • No problem! I appreciate the answer; didn't mean to sound so coarse, just wanted to be clear. – Mike McFarland Mar 01 '17 at 21:40
  • No biggy. You could try a WeakMap which uses unique object keys. Set the WeakMap key, and value as the same object. It would look kind of weird (vs Set), but you'd have unique values. – Quentin Engles Mar 01 '17 at 21:57
  • I don't see how that would help, and the main usage for weakmap is to have weak references, for garbage collection. The objects above are actual, different, references, but I want to have the set recognize them as equal values (often done by redefining equality for a type). [this article](https://en.wikipedia.org/wiki/Value_object) might be helpful – Mike McFarland Mar 02 '17 at 13:29
  • Looks like you said you wanted to prevent duplicate objects above. Oh never mind. – Quentin Engles Mar 02 '17 at 14:27