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

Recommend this page to a friend!
  Classes of Emmanuel Antico  >  eMacros  >  src/eMacros/Runtime/Collection/ArrayMap.php  >  Download  
File: src/eMacros/Runtime/Collection/ArrayMap.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,352 bytes
 

Contents

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

use
eMacros\Applicable;
use
eMacros\Scope;
use
eMacros\GenericList;
use
eMacros\Symbol;

class
ArrayMap implements Applicable {
   
/**
     * Applies a callback to a given array
     * Usage: (Array::map "strtoupper" (array "a" "b" "c"))
     * Returns: array
     * (non-PHPdoc)
     * @see \eMacros\Applicable::apply()
     */
   
public function apply(Scope $scope, GenericList $arguments) {
       
$nargs = count($arguments);
       
        if (
$nargs == 0) {
            throw new \
BadFunctionCallException("ArrayMap: No callback specified.");
        }
        elseif (
$nargs == 1) {
            throw new \
BadFunctionCallException("ArrayMap: No target specified.");
        }
       
       
$op = $arguments[0]->evaluate($scope);
       
        if (
is_callable($op)) {
           
$args = array();
           
$it = $arguments->getIterator();
           
$it->rewind();
           
            for (
$it->next(); $it->valid(); $it->next()) {
                
$el = $it->current()->evaluate($scope);

                 if (
$el instanceof \IteratorAggregate) {
                    
$eit = $el->getIterator();
                    
$eit->rewind();
                    
                    
$el = array();
                    
                     for (;
$eit->valid(); $eit->next()) {
                        
$el[] = $eit->current();
                     }
                 }
                
                
$args[] = $el;
            }
           
           
array_unshift($args, $op);
           
            return
call_user_func_array('array_map', $args);
        }
       
        throw new \
InvalidArgumentException("ArrayMap: Expected callable as first argument.");
    }
}

?>

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