last day (299 days later) » 

9:09 PM
Hello.
 
hi
I think the easiest and fastest approach is to fork SPL
and propose new data structures
just like it happened with DoubleLinkedList
 
Except that particular structure and its children are some of the worst designed parts of the SPL :(
You seem very anxious to push code for this ^^
Can't we talk design first?
 
Where's the best place to look at up-to-date Doctrine Collections?
 
The key element is Collection interface
 
My initial thought: it's way too narrow; many collections are not array indexable.
But we do share some common stuff.
isEmpty, doing iterator stuff, etc.
 
Well... I agree. That's when you rely on things like SplObjectStorage
But I can tell for standard collections we had no need to modify this interface since 2009
the only method added was allocated in another interface called Selectable
allowing arbitrary comparisons
 
That collection, while being narrowly focused, also does way too much in my opinion.
Also, I'm not trying to rip apart all the work that went into it.
The fact you haven't had to change it much is a compliment to the foresight for its application domain.
 
agreed. I'd reduce the amount of API if we had segregated Map, Set, etc
when I originally thought about porting collections to PHP C, I would imagine looking into things that make sense from Java
or looking at HHVM's now
 
9:17 PM
I doubt they looked at my Collections but we are surprisingly compatible.
@GuilhermeBlanco What were the structures you wanted to start with again?
Set, Map and ..?
 
Hash, Map, Set and standard Vector
 
Hash?
 
Hash is a structure that creates a Vector at a Hash collision
 
I've never heard that usage for that term before.
 
I dunno how I can draw this easily
Do you know how a Buffer Pool works?
 
9:20 PM
I am familiar with the structure you described though.
 
it's like a Buffer Pool... but the elements on collision do not create a linked list
found one image
 
that's a hash table
 
ircmaxell correct
 
which is a map...
 
A specific implementation of a Map.
So for now let's look at a Map, a Set and a Vector (flat, c-array like structure)
 
9:23 PM
sure
 
Logically set's are not indexable.
So that throws out ArrayAccess as a common interface.
 
also... we should look into OrderedMap too
 
Sure (which is where trees come into play)
(eventually)
 
Why you need a Tree? A linked list is enough
 
Have you ever implemented a self balancing, ordered tree?
(you can think of a tree as a specific way to use a series of Linked Lists, so perhaps that's what you are referring to)
Anyway.
 
9:26 PM
have fun :-) (I'm out)
 
@ircmaxell See ya later.
 
I do... B-trees, B+ trees, Binaries, etc
cya
 
So let's focus for now on those three structures, independent of implementation.
You can iterate over them.
You can see if they are empty.
interface Collection extends Traversable {
    function isEmpty();
}
We'll start here; sound good?
 
I'm going to rule out methods like add and remove; that okay? Maps require a key => value pair.
So all collections can't just add a value independently like that.
 
9:30 PM
hm... sure
 
(you could probably force it to if you cared enough; see Java)
 
yes, since they're not valid across all structure
 
At this point I'd like to use two design principles for fleshing out the rest of the Collection API:
1. Nothing in it must require it to be mutable.
Actually, just one for now.
The second one for reference is: allowing for Infinite Collections.
But I'm not sure on that.
This is PHP after all; not some mathematical systems solving language.
 
I don't see a need to keep Immutable on Collections
 
I'm not saying that interface Collection needs to be mutable or immutable.
I'm saying we shouldn't force that onto the Collection interface.
 
9:34 PM
sure
 
So looking at your API, we have some similar methods with some detail differences.
 
so why don't we start with ImmutableCollection enforcing non-mutables first?
 
map,filter, exists.
 
then we have Collection with mutables
 
Oh, oh, I nearly forgot: Collections should generally be lazy.
Agreed?
 
9:35 PM
yes
 
Good, so far.
Here's your map:
function map(Closure $func);
I'm curious, why Closure instead of callable? Pre 5.4 support?
 
correct
late 2008-early 2009 code
and heavily reused by other libs
unable to change contract
 
So callable is fine with you?
 
interface Collection extends \Traversable{

    function isEmpty();

    function map(callable $f): Collection;

    function filter(callable $f): Collection;

}
 
9:40 PM
seems ok so far
 
So I'd say that map and filter should return lazy collections.
Or at least, a Collection which could be lazy
 
nothing against so far
 
What else would you say that Collections really need?
 
I'd argue iterate(), but keys may be objects
so I'm -1 so far
I'm thinking maybe a contains()
 
iterate meaning something like a functional style foreach?
 
9:43 PM
yep
or something like an Iterator instance that could be used
both could be valid approaches
 
Okay, we'll think about that some more and shared any insight we have at some later date.
So, contains.
 
and also a size() method returning the amount of elements in the ImmutableCollection
 
Eh... that one I'm not sure on. Practically speaking we don't have infinite collections in PHP apps.
But I'm not sure if I want to preclude that just yet.
 
but we may have > PHP_INT_MAX
 
Possibly, sure.
So maybe we'll avoid that one for now too.
Contains is somewhat problematic from my perspective.
 
9:46 PM
explain?
 
Do you use ==, ===, or pass a callable?
 
The last is strictly the most flexible.
=== doesn't work if someone uses value objects though.
 
I'd assume contains($element)
 
and == has too many problems.
 
9:47 PM
of course it does by checking references
*object references
 
Which is a problem sometimes.
 
the only situation I see is with arrays
for contains
 
Arrays and value-like objects (we use these at work all the time)
 
except that, everything should/could work as ===
I know... things like: contains(new SplInt(1))
would break
 
Yep.
So I see either 1. including contains and passing a callable or 2. not including contains.
 
9:50 PM
so... easy would be if __toString() is available use it for comparison
but that would double the needed time
so... dunno
contains() will always be a problem, no matter which structure we talk about
 
I think toArray is pretty non controversial and we both have it.
 
you can't do toArray
if I do something like:
 
Ignoring keys you can.
 
$collection->add(new SplInt(1), $obj)
well... then you can't safely rely on toArray
 
As in a Map.
Okay, so how about values instead?
 
9:52 PM
makes more sense
which would also bring its brother, keys
 
For objects that have only values it is a no-op. For things with keys then it can convert.
 
yep
ok... so until now we have map, filter, isEmpty, values
 
Let's talk about map and filter for a second.
I use a callable that looks like:
callalble $f($value, $key) returns new value
Or in filter returns bool.
 
good enough
 
The important thing is passing both keys and values.
Otherwise you get map_keys() and junk like that.
If you don't use a key then just ignore it.
 
9:56 PM
yep
 
I'm trying to get Hack's documentation.
It's even worse than most of PHP's documentation ^^
 
hm...
it actually reminded me about Tuple
key/value pair
 
It appears they don't put filter and map on Collection.
They delegate it to Set and Map, etc.
 
weird
but somehow reasonable
because it should return its specific implementations
maybe they do that for more granular hinting
 
They put it specifically on the const variants.
Mutable variants then extend it.
So all variants have it... I wonder why they don't push it out to Collection?
 
10:02 PM
I do know
they are protecting from having in an Immutable lower level to "hint" a Mutable instance
like ImmutableCollection creating a MutableCollection
 
Hmm... they are really confusing.
They have an ImmMap and a ConstMap and a MutableMap
I am trying to figure out Imm vs Const now.
 
it seems that Const is the base between Imm and Mutable
yep... that's it
Const is the base
Imm is the Immutable implementation, final
Mutable is the Const derivative that adds mutable methods, abstract
 
And the implementation is the no Imm, Const, Mutable
 
10:09 PM
now it makes sense
and it also explains why map and filter does not receive the keys
would they receive by value or by reference?
 
value
Hack avoids references like the plague (as they should, imo)
 
what would happen if I change that value?
 
Nothing, unless it is an object ^^
 
what would it happen if it is?
 
You are asking, what if you don't respect the const in a const collection?
I would imagine it works at runtime but their type checker fails (or they intend to do this eventually)
 
10:12 PM
so let's support I have an Tuple<obj, obj>
and I call this collection ->map
and I change some properties out of the key obj
 
It probably does what you ask at runtime.
Their type checker works statically and probably complains there.
 
that could affect the ordering or existence (for example) if you have a Set
hm...
 
Yeah.
This is why C has const.
 
ok
so... where are we now?
now that HHVM's Collection support somehow makes more sense
 
I think we need to decide whether or not we are going down the separation of const and nonconst or if we'll just be vague enough people can do either.
 
10:15 PM
=)
 
(aside from modifying something in an intended non-const fashion, such as a key in filter, which we can't do anything about atm)
 
I'd const
looks like magic if not
 
Bleh, by the way have you been following the PHPNG stuff?
 
I quickly looked at it, but not its branch closely
 
Several people are upset about the manner that they did it.
Joe Watkins has thousands of lines of code that need to be changed to support it.
He'd have appreciated a heads up four months ago when they started work.
 
10:19 PM
yep
 
So what are your thoughts on const and non-const?
Honestly I don't personally think we need to go down that route.
What are you thinking?
 
I think we could get rid of mutable/immutable and rely purely on standard items
and get some inspiration from Java 8
which got some improvements around Collections API
they basically added lambdas around map, filter and forEach
I gtg for now... can we continue this tomorrow?
 
Yeah.
If I'm not in here, ping me (with the @ sign)
 
As homework, try to research around existing implementation from other languages
and we discuss it tomorrow
 
Okay.
@GuilhermeBlanco If it's okay with you I'll push up a repo on github separate from my existing collections.
Grant you collab privs and whatnot.
I also think the PSR people were working on a Collections PSR or something?
 

  last day (299 days later) »