gsuberland,
@gsuberland@chaos.social avatar

PHP has changed for the better in so many ways in the time between PHP5 and PHP8, making it far easier to write good code, but oh wow do I wish their basic string and array manipulation functions had some consistency. I'm extremely tempted to port LINQ to PHP just so I don't have to deal with completely random* naming & parameter ordering.

(*not actually random; it stems from an old design quirk to do with how standard functions were hashed and indexed internally. it's just annoying)

gsuberland,
@gsuberland@chaos.social avatar

my only real wishlist items for implementing something like LINQ would be:

  1. typed arrays (so T[] rather than just array)

  2. generic inferencing, so if you say function foo(&lt;T&gt; $param) : &lt;T&gt; the type inferencer can identify that whatever type is passed into $param will also be the return type. also with support for derivatives, e.g. <T>[] and ?<T>.

gsuberland,
@gsuberland@chaos.social avatar

typed locals would also be great but I suspect that's a much bigger packet of work.

gsuberland,
@gsuberland@chaos.social avatar

this is the kind of thing I'm talking about, inspired by how ToDictionary works in .NET

I have no idea if I'll turn this into a whole library (right now it's only got two functions!) but it's super useful compared to doing this kind of thing manually every time, and having to think about how array_reduce / array_map work and how to pass external variables into the callback.

gsuberland,
@gsuberland@chaos.social avatar

oh neat, phpdoc has template type support, so I can get type inference working in IDEs at least

gsuberland,
@gsuberland@chaos.social avatar

ok I may be porting linq to php

gsuberland,
@gsuberland@chaos.social avatar

now that's what I like to see

Rairii,
@Rairii@haqueers.com avatar

@gsuberland ...does this thing work with iterator objects

gsuberland,
@gsuberland@chaos.social avatar

@Rairii yes

gsuberland,
@gsuberland@chaos.social avatar

@Rairii more completely, the input type is iterable, so it'll work with pretty much anything you throw at it. the output type is always array (more specifically T[] in the phpdoc), unless you use the *Lazy variants in which case it's Generator<mixed,T>

gsuberland,
@gsuberland@chaos.social avatar

I also made ContainsAtLeast and ContainsNoMoreThan, which are early-out optimised queries for thresholding against predicates, since it's something .NET doesn't have but I wish it did.

gsuberland,
@gsuberland@chaos.social avatar

one cool thing about this being a port in spirit rather than a 1:1 mapping is that I can extend functionality in a way that makes certain tasks easier.

for example, in C# Linq all predicates and selectors pretty much just get the element value and that's it, whereas I'm providing the numeric index in the sequence, the key, and the value, so you can work with all that info together.

in the case of GroupBy, I'm also giving the element value selector (projection) access to the group key.

gsuberland,
@gsuberland@chaos.social avatar

this means you end up being able to integrate the group key into the value projection if you want, which is very useful when the group key selector function is dynamically selected at runtime.

gsuberland,
@gsuberland@chaos.social avatar

ok I might have ported quite a lot of LINQ to PHP now.

gsuberland,
@gsuberland@chaos.social avatar

... PINQ?

jhwgh1968,
@jhwgh1968@chaos.social avatar

@gsuberland that is the perfect name tbh -- quirky, pun, pronounceable, clever, descriptive

Palpatine meme Dewit!

gsuberland,
@gsuberland@chaos.social avatar

well, here you go. here's PINQ, a fairly low effort pseudo-port of LINQ's array manipulation functions to PHP.

https://github.com/gsuberland/PINQ/blob/main/PINQ.php

oohshiiiny,

@gsuberland I like the green oks, is that just a bash script or something? Or a service startup with debug enabled?

gsuberland,
@gsuberland@chaos.social avatar

@oohshiiiny this is PHP, so it's a webpage.

vk2tty,
@vk2tty@mastodon.radio avatar

@gsuberland oh the fun I had with IEnumerable<T> in the day. Extension methods, I still miss 'em.

Though I did discover a generation of devs who couldn’t read basic map-filter pipelines, refused to learn, and thus wrote hundred-line functions hand-building intermediate collections five lines at a time.

gsuberland,
@gsuberland@chaos.social avatar

@vk2tty yeah C# is my language of choice for most things, I often miss language features from it when I work on other stuff.

SteveSyfuhs,
@SteveSyfuhs@hachyderm.io avatar
gsuberland,
@gsuberland@chaos.social avatar

@SteveSyfuhs yeah someone pointed me towards that. it's a different thing than what I want here, but still very cool.

Rairii,
@Rairii@haqueers.com avatar

@gsuberland "port LINQ to PHP"

...didn't peachpie already do that? :P

gsuberland,
@gsuberland@chaos.social avatar

@Rairii ha, weird, I didn't know that was a thing.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • ngwrru68w68
  • rosin
  • GTA5RPClips
  • osvaldo12
  • love
  • Youngstown
  • slotface
  • khanakhh
  • everett
  • kavyap
  • mdbf
  • DreamBathrooms
  • thenastyranch
  • magazineikmin
  • anitta
  • InstantRegret
  • normalnudes
  • tacticalgear
  • cubers
  • ethstaker
  • modclub
  • cisconetworking
  • Durango
  • provamag3
  • tester
  • Leos
  • megavids
  • JUstTest
  • All magazines