Code Monkey home page Code Monkey logo

immutable.php's People

Contributors

hansott avatar jarjak avatar jkoudys avatar localheinz avatar royopa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

immutable.php's Issues

Tests are failing

Running the tests on a fresh clone currently fails:

$ phpunit --configuration tests/phpunit.xml

PHPUnit 4.8.14 by Sebastian Bergmann and contributors.

.......PHP Fatal error:  Allowed memory size of 52428800 bytes exhausted (tried to allocate 32 bytes) in /Users/am/Sites/jkoudys/immutable.php/tests/Collections/ImmArrayTest.php on line 100
PHP Stack trace:
PHP   1. {main}() /Users/am/.composer/vendor/phpunit/phpunit/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /Users/am/.composer/vendor/phpunit/phpunit/phpunit:47
PHP   3. PHPUnit_TextUI_Command->run() /Users/am/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:100
PHP   4. PHPUnit_TextUI_TestRunner->doRun() /Users/am/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:149
PHP   5. PHPUnit_Framework_TestSuite->run() /Users/am/.composer/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:440
PHP   6. PHPUnit_Framework_TestSuite->run() /Users/am/.composer/vendor/phpunit/phpunit/src/Framework/TestSuite.php:747
PHP   7. PHPUnit_Framework_TestCase->run() /Users/am/.composer/vendor/phpunit/phpunit/src/Framework/TestSuite.php:747
PHP   8. PHPUnit_Framework_TestResult->run() /Users/am/.composer/vendor/phpunit/phpunit/src/Framework/TestCase.php:724
PHP   9. PHPUnit_Framework_TestCase->runBare() /Users/am/.composer/vendor/phpunit/phpunit/src/Framework/TestResult.php:612
PHP  10. PHPUnit_Framework_TestCase->runTest() /Users/am/.composer/vendor/phpunit/phpunit/src/Framework/TestCase.php:768
PHP  11. ReflectionMethod->invokeArgs() /Users/am/.composer/vendor/phpunit/phpunit/src/Framework/TestCase.php:909
PHP  12. ImmArrayTest->testLoadBigSet() /Users/am/.composer/vendor/phpunit/phpunit/src/Framework/TestCase.php:909
PHP  13. array_map() /Users/am/Sites/jkoudys/immutable.php/tests/Collections/ImmArrayTest.php:100
PHP  14. ImmArrayTest->{closure:/Users/am/Sites/jkoudys/immutable.php/tests/Collections/ImmArrayTest.php:100-100}() /Users/am/Sites/jkoudys/immutable.php/tests/Collections/ImmArrayTest.php:100
PHP  15. md5() /Users/am/Sites/jkoudys/immutable.php/tests/Collections/ImmArrayTest.php:100

Fatal error: Allowed memory size of 52428800 bytes exhausted (tried to allocate 32 bytes) in /Users/am/Sites/jkoudys/immutable.php/tests/Collections/ImmArrayTest.php on line 100

Call Stack:
    0.0002     238216   1. {main}() /Users/am/.composer/vendor/phpunit/phpunit/phpunit:0
    0.0028     668264   2. PHPUnit_TextUI_Command::main() /Users/am/.composer/vendor/phpunit/phpunit/phpunit:47
    0.0028     668904   3. PHPUnit_TextUI_Command->run() /Users/am/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:100
    0.0471    4257656   4. PHPUnit_TextUI_TestRunner->doRun() /Users/am/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:149
    0.0516    4440752   5. PHPUnit_Framework_TestSuite->run() /Users/am/.composer/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:440
    0.0520    4456216   6. PHPUnit_Framework_TestSuite->run() /Users/am/.composer/vendor/phpunit/phpunit/src/Framework/TestSuite.php:747
    0.0596    4744928   7. PHPUnit_Framework_TestCase->run() /Users/am/.composer/vendor/phpunit/phpunit/src/Framework/TestSuite.php:747
    0.0596    4744928   8. PHPUnit_Framework_TestResult->run() /Users/am/.composer/vendor/phpunit/phpunit/src/Framework/TestCase.php:724
    0.0596    4745936   9. PHPUnit_Framework_TestCase->runBare() /Users/am/.composer/vendor/phpunit/phpunit/src/Framework/TestResult.php:612
    0.0597    4762808  10. PHPUnit_Framework_TestCase->runTest() /Users/am/.composer/vendor/phpunit/phpunit/src/Framework/TestCase.php:768
    0.0597    4763568  11. ReflectionMethod->invokeArgs() /Users/am/.composer/vendor/phpunit/phpunit/src/Framework/TestCase.php:909
    0.0597    4763688  12. ImmArrayTest->testLoadBigSet() /Users/am/.composer/vendor/phpunit/phpunit/src/Framework/TestCase.php:909
    0.0855   34062136  13. array_map() /Users/am/Sites/jkoudys/immutable.php/tests/Collections/ImmArrayTest.php:100
    0.2867   52376512  14. ImmArrayTest->{closure:/Users/am/Sites/jkoudys/immutable.php/tests/Collections/ImmArrayTest.php:100-100}() /Users/am/Sites/jkoudys/immutable.php/tests/Collections/ImmArrayTest.php:100
    0.2867   52376560  15. md5() /Users/am/Sites/jkoudys/immutable.php/tests/Collections/ImmArrayTest.php:100

Related to #4.

Immutability and loss of information

Hi @jkoudys ,

Thanks a lot for sharing your super-accurate work.
I have a question concerning Immutability. Presently, you make your collections immutable by calling return new static(SplFixedArray $fixedArray); at each end of mutator methods.

This works perfectly as long as the collection itself as no more property than the content of the $fixedArray parameter.

For example if I extend ImmArray this way :

class MyLovelyCollection extends ImmArray
{
    protected $feeling = 'loneliness';
    
    public function __construct($fixedArray, $feeling)
    {
        $this->feeling = $feeling;
        parent::__construct($fixedArray);
    }
    
    public function getFeeling()
    {
        return $this->feeling;
    }
}

And then call something like

$faces = new MyLovelyCollection([':)', ':/', ':D', 'XD'], 'happiness');

echo $faces->getFeeling() // => 'happiness'

$faces->filter(function($face) {
    return ! preg_match("#[^/]$#", $face);
});

echo $faces->getFeeling() // => 'loneliness'

Because of this, I cannot use together the quality of your iterable ImmArray (it has no point inside a composition) and some properties (mutable or not) that I would attach to. Simple things like composition becomes super hard due to it.

So, more pragmatically, my question is "Why don't do clone $collection; return $collection->doSomething(); instead of return new static(...);?".

Thanks in advance for your time,
Jean

Use item index in map() callback

I'd like to have the item index available during mapping. If you want, I can create a pull request. For backwards compatibility, index can be used as a second argument of the callback.

Build SliceIterator for more efficient slices

To be javascript-like, the original implementation made a slice as a full clone. However, this is only necessary in JS specifically because an array is mutable. On an immutable array, it makes more sense to define an ArrayAccess, JsonSerializable LimitIterator to simply access a subset of the target ImmArray. It's not going anywhere in memory, so no need to clone when we can simply iterate at an offset.

Not compatible with PHP >=7.1.0 as claimed in composer package (aka incompatible with PHP >= 8.0)

I am getting the following error when using ImmArray:

Call to undefined method SplFixedArray::rewind()

An internet search revealed this change notice of PHP 8.0:

  . SplFixedArray is now an IteratorAggregate and not an Iterator.
    SplFixedArray::rewind(), ::current(), ::key(), ::next(), and ::valid()
    have been removed. In their place, SplFixedArray::getIterator() has been
    added. Any code which uses explicit iteration over SplFixedArray must now
    obtain an Iterator through SplFixedArray::getIterator(). This means that
    SplFixedArray is now safe to use in nested loops.

(source: https://github.com/php/php-src/blob/e4e2541c1a90daf708ab1ee4d44f2ab2883ce84b/UPGRADING#L515-L520)

Therefore I deduct that immutable.php is not compatible with PHP 8.0 and above, as claimed in package metadata.
(I am using the package via composer. there ">=7.1.0" is claimed)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.