Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114570 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 1056 invoked from network); 25 May 2021 09:09:25 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 May 2021 09:09:25 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 263E21804CC for ; Tue, 25 May 2021 02:20:39 -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=0.0 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f43.google.com (mail-ej1-f43.google.com [209.85.218.43]) (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:20:38 -0700 (PDT) Received: by mail-ej1-f43.google.com with SMTP id jt22so1996790ejb.7 for ; Tue, 25 May 2021 02:20:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=negyesi-net.20150623.gappssmtp.com; s=20150623; h=mime-version:from:date:message-id:subject:to; bh=cpuR+b4o+zfSh5PlmTpLhuqvYTafvvNLILAHDxSY9Mk=; b=N4CutGLp3EDxw3sXoOEu09QGaFvNiTtr17ohWg9HaHVAFLBEp48m9xk0QDKRIUkBfr Im3uJpYw/nUulTrMVkijh2zslXcaJT08Ym2JDWKbEpZQZC0VpEHJpPSqqTOp8OUGiKXc 2GnG6mwJHt39zRbbVBSvxxe8H/bMHndbY2kIGCVQyy4Oes09kmAzTy/tVzPK/GNMUyDo bHhKtBo7zruMLYGeqM/6xk3bICDUTCXSnhl0WutQvF8+UveFTzQonR7nYqF2pxKYe+q4 bKgSY17g1/GurWwi7PIG96I3udP/X4LhRvGJz4XY4eUFN3qC8e2YpuuoTS74Z77sVkvO r3dw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=cpuR+b4o+zfSh5PlmTpLhuqvYTafvvNLILAHDxSY9Mk=; b=QgEIm5tT9yls6a1+qsV7s0Q+T4St0bM5xEIBbkKC1R/icejNWn4XHv8/0QwBygsvbs V2vpA5MO89/aLJGdgzHfkf0du8Zr0sNdmWjm3VgkKn2FW6lg33fqFGzHYoU/doFEhbBs Ikz2ZICnZGwUKw/uJhBnSvvmTzx3+6Dzldb2kGT1KwBnvvEbrO2IyK44DMWPWGNF8xJt RZNaIA+6IXJeN+VkPG0Vs+99xmWYKujxPdaJG66IN50wtvT+6SIninsxvhcRCM191j2i nWJdzpT8RpoiEDgxaddEYRUEm7fUSoBKZ+L+0/aDY76CdwCEV0sUjxPTdGWOc7nfwsI2 yOog== X-Gm-Message-State: AOAM533XJ51eqBML0vkFCymRgwWVrKF7+hloT7YjQQG1nTT9Umnb3d30 Exk6DIp8Iuhp5p3avV2U1ANJeKXhqpb2oA== X-Google-Smtp-Source: ABdhPJxG0IE60b3gHbnSCupTxVQX71/vhTbAqO7Y8S9oN0UGB79P71w5qUczeBKHxyypRVjM3GjaQw== X-Received: by 2002:a17:906:ae4a:: with SMTP id lf10mr3482402ejb.81.1621934436329; Tue, 25 May 2021 02:20:36 -0700 (PDT) Received: from mail-ej1-f43.google.com (mail-ej1-f43.google.com. [209.85.218.43]) by smtp.gmail.com with ESMTPSA id g23sm8721503ejb.15.2021.05.25.02.20.35 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 25 May 2021 02:20:35 -0700 (PDT) Received: by mail-ej1-f43.google.com with SMTP id et19so39162585ejc.4 for ; Tue, 25 May 2021 02:20:35 -0700 (PDT) X-Received: by 2002:a17:906:e91:: with SMTP id p17mr28368980ejf.9.1621934434954; Tue, 25 May 2021 02:20:34 -0700 (PDT) MIME-Version: 1.0 Date: Tue, 25 May 2021 02:20:23 -0700 X-Gmail-Original-Message-ID: Message-ID: To: "internals@lists.php.net" Content-Type: multipart/alternative; boundary="000000000000d8338d05c32409c8" Subject: A little syntactic sugar on array_* function calls? From: karoly@negyesi.net (Karoly Negyesi) --000000000000d8338d05c32409c8 Content-Type: text/plain; charset="UTF-8" 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 --000000000000d8338d05c32409c8--