File: src/eMacros/Runtime/Collection/Cdr.php

Recommend this page to a friend!
  Classes of Emmanuel Antico  >  eMacros  >  src/eMacros/Runtime/Collection/Cdr.php  >  Download  
File: src/eMacros/Runtime/Collection/Cdr.php
Role: Class source
Content type: text/plain
Description: Class source
Class: eMacros
PHP LISP language interpreter
Author: By
Last change:
Date: 7 years ago
Size: 1,047 bytes
 

Contents

Class file image Download
<?php
namespace eMacros\Runtime\Collection;

use
eMacros\Runtime\GenericFunction;

class
Cdr extends GenericFunction {
   
/**
     * Returns a list without its first element
     * Usage: (Array::cdr (array 1 2 3))
     * Returns: array
     * (non-PHPdoc)
     * @see \eMacros\Runtime\GenericFunction::execute()
     */
   
public function execute(array $arguments) {
        if (empty(
$arguments)) {
            throw new \
BadFunctionCallException("Cdr: No parameters found.");
        }
       
        list(
$list) = $arguments;
       
        if (
is_array($list)) {
            if (empty(
$list)) {
                return
null;
            }
           
            return
array_slice($list, 1);
        }
       
        if (
$list instanceof \Iterator || $list instanceof \IteratorAggregate) {
           
$it = $list instanceof \Iterator ? $list : $list->getIterator();
           
$it->rewind();
           
           
//empty list?
           
if (!$it->valid()) {
                return
null;
            }
           
           
$result = array();
           
            for (
$it->next(); $it->valid(); $it->next()) {
               
$result[] = $it->current();
            }
       
            return
$result;
        }
       
        throw new \
InvalidArgumentException('Cdr: Parameter is not a list.');
    }
}
?>

For more information send a message to info at phpclasses dot org.