Skip to content

IEnumerable.toSet() method

Marcel Kloubert edited this page Feb 22, 2015 · 1 revision

IEnumerable->toSet([$comparer]) method

Returns a new set of that sequence.

Syntax

public ISet toSet([callable $comparer = null]);

Parameters

Name Type Description
$comparer callable [OPTIONAL] The custom item comparer to use.

Result

The new set.

Examples

Default behavior

use \System\Linq\Enumerable;

$seq = Enumerable::fromValues(1, 2, 3, 2);

$set = $seq->toSet();
        
foreach ($set as $item) {
    // [0] 1
    // [1] 2
    // [2] 3
}

Custom comparer

use \System\Linq\Enumerable;

$seq = Enumerable::fromValues(1, 2, 3, "2");

$set = $seq->toSet(function($x, $y) {
                       return $x === $y;
                   });
        
foreach ($set as $item) {
    // [0] 1
    // [1] 2
    // [2] 3
    // [3] '2'
}
Clone this wiki locally