Login   Register  
PHP Classes
elePHPant
Icontem

File: src/eMacros/Runtime/Arithmetic/Division.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Emmanuel Antico  >  eMacros  >  src/eMacros/Runtime/Arithmetic/Division.php  >  Download  
File: src/eMacros/Runtime/Arithmetic/Division.php
Role: Class source
Content type: text/plain
Description: Class source
Class: eMacros
PHP LISP language interpreter
Author: By
Last change:
Date: 2014-01-13 05:23
Size: 839 bytes
 

Contents

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

use 
eMacros\Runtime\GenericFunction;

class 
Division extends GenericFunction {
    
/**
     * Obtains the result of dividing the first element on a list by the rest of the elements.
     * Usage: (/ 4 2)
     * Special case: (/ 5) = 1/5
     * Returns: number
     * (non-PHPdoc)
     * @see \eMacros\Runtime\GenericFunction::execute()
     */
    
public function execute(array $arguments) {
        if (empty(
$arguments)) {
            
//no parameters
            
throw new \InvalidArgumentException("Division: At least 1 argument is required.");
        }
        
        
//emulate CLISP: (/ 4) = 1/4
        
if (!isset($arguments[1])) {
            return 
$arguments[0];
        }
        
        
//get first value
        
$result array_shift($arguments);
        
        foreach (
$arguments as $value) {
            
$result /= $value;
        }
        
        return 
$result;
    }
}