Exemplo n.º 1
0
Arquivo: Chain.php Projeto: iddqdby/f7
 /**
  * Get key of an array or \ArrayAccess, if both are set.
  *
  * @param int|string $key the key
  * @return Chain the value of the key wrapped into a Chain, or empty Chain
  */
 public function getKey($key) : Chain
 {
     return $this->continue(conditionally(conjunction(is_array_access, function ($value) use($key) {
         return isset($value[$key]);
     }), array_value_getter($key)));
 }
Exemplo n.º 2
0
 /**
  * Find random element of the stream (optionally matched with predicate).
  *
  * This is a closing operation (it calls <code>$this->close()</code> internally).
  *
  * @param callable $predicate the predicate (optional)
  * @return Optional an optional result
  * @see \monad\Stream::close()
  */
 public function findRandom(callable $predicate = null) : Optional
 {
     return $this->bindMonad(sequence(null === $predicate ? conditionally(negation(is_empty), array_value_getter(array_rand($this->val()))) : sequence(traversable_randomize, curry(function ($predicate, array $array) {
         return $this->find($predicate, $array);
     }, 2)($predicate)), optional));
 }