コード例 #1
0
ファイル: RndTest.php プロジェクト: rakesh-mohanta/Nspl
 public function testSample()
 {
     $list = ['a', 'b', 'c', 'd', 'e'];
     $this->assertTrue(isList(sample($list, 3)));
     $iterator = new \ArrayIterator($list);
     $this->assertTrue(isList(sample($iterator, 3)));
     foreach (sample($iterator, 3) as $item) {
         $this->assertTrue(in_array($item, $list));
     }
     $sample = sample($list, 3, true);
     foreach ($sample as $k => $element) {
         $this->assertTrue($list[$k] === $element);
     }
     $this->assertEquals([], sample(['a', 'b', 'c'], 0));
     $appearances = array_fill_keys($list, 0);
     for ($i = 0; $i < 50000; ++$i) {
         $sample = sample($list, 3);
         foreach ($sample as $item) {
             ++$appearances[$item];
         }
     }
     foreach ($appearances as $item => $rate) {
         $this->assertEquals(30000, $rate, '', 500);
     }
 }
コード例 #2
0
ファイル: DSTest.php プロジェクト: rakesh-mohanta/Nspl
 public function testIsList()
 {
     $this->assertFalse(isList(1));
     $this->assertFalse(isList(array(1 => 'a')));
     $this->assertFalse(isList(array(0 => 'a', 2 => 'c')));
     $this->assertFalse(isList(new \StdClass()));
     $this->assertTrue(isList([]));
     $this->assertTrue(isList([1]));
     $this->assertTrue(isList([1, 2, 3]));
     $this->assertTrue(isList([10, 11, 13]));
 }
コード例 #3
0
ファイル: f.php プロジェクト: rakesh-mohanta/Nspl
/**
 * Returns two lists, one containing values for which your predicate returned true until the predicate returned
 * false, and the other containing all the elements that left
 *
 * @param callable $predicate
 * @param array|\Traversable $sequence
 * @return array
 */
function span(callable $predicate, $sequence)
{
    args\expects(args\traversable, $sequence);
    $isList = ds\isList($sequence);
    $result = [[], []];
    $listIndex = 0;
    foreach ($sequence as $k => $v) {
        if (!$predicate($v)) {
            $listIndex = 1;
        }
        if ($isList) {
            $result[$listIndex][] = $v;
        } else {
            $result[$listIndex][$k] = $v;
        }
    }
    return $result;
}
コード例 #4
0
ファイル: a.php プロジェクト: rakesh-mohanta/Nspl
/**
 * Moves list element to another position
 *
 * @param array $list
 * @param int $from
 * @param int $to
 * @return array
 */
function moveElement(array $list, $from, $to)
{
    args\expects(args\int, $from);
    args\expects(args\int, $to, 3);
    if (!ds\isList($list)) {
        throw new \InvalidArgumentException('First argument should be a list');
    }
    if (!isset($list[$from]) || !isset($list[$to])) {
        throw new \InvalidArgumentException('From and to should be valid list keys');
    }
    if ($from === $to) {
        return $list;
    }
    $moving = array_splice($list, $from, 1);
    array_splice($list, $to, 0, $moving);
    return $list;
}