Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 941397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:06:48+00:00 2026-05-15T22:06:48+00:00

From this question here , I was writing an enum wrapper to have some

  • 0

From this question here, I was writing an enum wrapper to have some methods that can be used with lambdas to somewhat emulate ruby’s usage of blocks in enums.

class enum {
    public $arr;

    function __construct($array) {
        $this->arr = $array;
    }

    function each($lambda) {
        array_walk($this->arr, $lambda);
    }

    function find_all($lambda) {
        return array_filter($this->arr, $lambda);
    }

    function inject($lambda, $initial=null) { 
        if ($initial == null) { 
            $first = array_shift($this->arr); 
            $result = array_reduce($this->arr, $lambda, $first); 
            array_unshift($this->arr, $first); 

            return $result; 
        } else { 
            return array_reduce($this->arr, $lambda, $initial); 
        } 
    } 

}


$list = new enum(array(-1, 3, 4, 5, -7));
$list->each(function($a) { print $a . "\n";});

// in PHP you can also assign a closure to a variable 
$pos = function($a) { return ($a < 0) ? false : true;};
$positives = $list->find_all($pos);

Now, how could I implement inject() as elegantly as possible?


EDIT: method implemented as seen above. Usage examples:

// inject() examples 
$list = new enum(range(5, 10)); 

$sum = $list->inject(function($sum, $n) { return $sum+$n; }); 
$product = $list->inject(function($acc, $n) { return $acc*$n; }, 1); 

$list = new enum(array('cat', 'sheep', 'bear')); 
$longest = $list->inject(function($memo, $word) { 
        return (strlen($memo) > strlen($word)) ? $memo : $word; } 
    ); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:06:48+00:00Added an answer on May 15, 2026 at 10:06 pm

    I’m not familiar with Ruby, but from the description, it seems similar to array_reduce.

    mixed array_reduce ( array $input , callback $function [, mixed $initial = NULL ] )

    array_reduce() applies iteratively the function function to the elements of the array input, so as to reduce the array to a single value.

    In addition to "reduce", this operation is also sometimes called "fold"; in Mathematica:

    Fold[f, init, {a, b, c, d}] == f[f[f[f[init, a], b], c], d]
    

    The second form uses the first element of the collection as a the initial value (and skips that element while iterating).

    This second form can be implemented this way:

    //$arr is the initial array
    $first = array_shift($arr);
    $result = array_reduce($arr, $callback, $first);
    

    Response to Mladen

    The array functions in PHP cannot be used that way because they can only work with arrays, not arbitrary objects.

    There are a few options here:

    • You could convert the object into an array prior to passing it to array_reduce. In practice, this doesn’t have much value because the conversion consists of creating an array with the object properties as elements. This behavior can only be changed internally (writing a native extension).
    • You could have all your objects implement an interface with a method toArray that would have to be called priorly to passing it to array_reduce. Not a great idea, either.
    • You could implement a version of array_reduce that works with any Traversable object. This would be easy to do, but you couldn’t put a Traversable type hint in the function declaration since arrays are not objects. With such a hint, every array would have to be encapsulated in an ArrayIterator object prior to the function call.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first question here. Writing some code, i receive this error from
This is similar to (but different from) this question . Here is some simple
This is a follow-on question from the one I asked here . Can constraints
From my previous question here I was writing a program that executes A number
This is a continuation of this question from yesterday . Here are my three
I'm trying to implement this example http://jsfiddle.net/gzF6w/1/ (from another question here on stak) on
Hopefully the last NN question you'll get from me this weekend, but here goes
This is an extension to the question I asked here: Get text from clipboard
I'm trying to implement this algorithm description from a previous question I had here
I face a very serious situation. By writing this question I hope that really

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.