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); } }
/** * @deprecated * @see \nspl\a\span * * 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 = a\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; }
protected function stringifyArray(array $array) { if (a\isList($array)) { return 'array(' . implode(', ', array_map(function ($v) { return is_scalar($v) ? var_export($v, true) : \nspl\getType($v); }, $array)) . ')'; } return f\I(var_export($this->array, true), f\partial('str_replace', "\n", ''), f\partial('str_replace', 'array ( ', 'array('), f\partial('str_replace', ' ', ' '), f\partial('str_replace', ',)', ')')); }
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])); $this->assertTrue(call_user_func(isList, [10, 11, 13])); $this->assertEquals('\\nspl\\a\\isList', isList); }