Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114571 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 3873 invoked from network); 25 May 2021 09:20:22 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 May 2021 09:20:22 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9F7D31804C9 for ; Tue, 25 May 2021 02:31:36 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 25 May 2021 02:31:33 -0700 (PDT) Received: by mail-pj1-f45.google.com with SMTP id g6-20020a17090adac6b029015d1a9a6f1aso1488879pjx.1 for ; Tue, 25 May 2021 02:31:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=IQhjTH1tOSMY52X9fzUhDRssIYouzLnyz9fffS2jgog=; b=aECXuFrmJwygfpziiadF9feGIkRu4VIsLp615IEjx2FJlLBcJX63nKkAMmWhkFVKqT AySw9l/IKcWvbwX5fSQzafU4HHNo1/RfIRUB0TT7WXgGuwY3UOT0sRspZ2ZWvacCAiw8 pMuEzHR28a/9nzgeGvbHhHztrI7+xhN0zlckOQm4TDKOqsYweNn7nlHSofKFAsX2w0Ov w/BNilAgl88HIy/mKBiJ6Ne6bbVbdKbmpbVS3PWRZI2rojrxH+W3+ApJT8UMcPUIJGws ikat+IZUAEveevSjVikXCWc06Flrz+/JkcjMjjZ5MRxM5EtIOmZdjN3BmSvbX8rcTSAL EWzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=IQhjTH1tOSMY52X9fzUhDRssIYouzLnyz9fffS2jgog=; b=GgTdSDmOeJPchP+DO1lXW+1jp/sotsgqcHbf/D/pjKyGmvNKkzKoUkkpSFFGQndblo MTEMd6DZWrZWqWaBiSFw3wQbAy4HQLbb3tmyPJmzg4AuNlm5VLZcs1gGCuY9+5IDqh0Y 1mjHv1DGLgCfLNF6g7soMe2k5OuOd/e9dJHCVRpUdQsJ1mzMGYBnmNe9D5pr0+SnY2Cd YSZo1toZpnM12ESZaMHzz3mOcSSwqrFNEjAt669BNKZ9VbfpJ+qHdEaaK6eU1bt8H6MZ bVyK6oo9kciZiUyimrpeOSihvmx1uDkDT0eC3+ThXinsynaz/G2IqCyuFpNKYq6Qg+yF iKng== X-Gm-Message-State: AOAM531+ODcqZFouklbOLqe+p5b9a7TjjNNqxfKKYNElyoD9EIaSHkcn jg7X9/wfBMILn5UOmq9q2KQKw0cp+j0jIDMvtcBdCmOwxc4= X-Google-Smtp-Source: ABdhPJz1H/SfWXuuj9us8BwrhCHcuQ9lX3Ht2Yx33+gH9UoZGqwDHPpAI8nqvF44r4zIBRInzceI417uOQBNGnTcsfU= X-Received: by 2002:a17:902:c951:b029:ef:9dca:9943 with SMTP id i17-20020a170902c951b02900ef9dca9943mr29947115pla.62.1621935092027; Tue, 25 May 2021 02:31:32 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 25 May 2021 11:30:56 +0200 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="00000000000002539a05c324318c" Subject: Re: [PHP-DEV] A little syntactic sugar on array_* function calls? From: divinity76@gmail.com (Hans Henrik Bergan) --00000000000002539a05c324318c Content-Type: text/plain; charset="UTF-8" fwiw this can be implemented in userland, and i bet someone already made a composer package for it ^^ On Tue, 25 May 2021 at 11:20, Karoly Negyesi wrote: > Hi, > > I was wondering whether $array->map($somefunction) would be possible. I am > not a C programmer by any stretch but reading ZEND_VM_HOT_OBJ_HANDLER(112 > it seems to me it should be quite easy (famous last words) to find out if > object is an array and if so then > > 1. prepend the string array_ before the method name > 2. based on a small lookup table move the "object" to the right place -- > either first argument or second. > 3. do a function call instead of a method call. > > Regarding #2 by default it's the first: > > $array->flip() becomes array_flip($array) > $array->column($column_key, $index_key) becomes array_column($array, > $column_key, $index_key) > $array->merge($array2, $array3) becomes array_merge($array, $array2, > $array3) > > There'd be a small list of methods/functions where it's the second, for > example: > > $array->map($fn) becomes array_map($fn, $array) > $array->search($needle, $strict) becomes array_search($needle, $array, > $strict) > $array->key_exists($key) becomes array_key_exists($key, $array) > > (Is there even any other?) > > For phase 1 we could skip the functions which gets the first argument by > reference (walk, sort) and figure it out later. Hand waving yes but never > let perfect stand in the way of good enough :) > > Look how nicely this reads: > > $array->map($fn)->filter($fn2) > > Compared to array_filter(array_map($fn, $array), $fn2) > > I see no BC concerns here because in any previous PHP versions this is a > fatal error. I do not see any syntax ambiguity either but here I am > probably just naive. It could also be usable with user defined functions. > > What do you think? > > Karoly Negyesi > --00000000000002539a05c324318c--