• Statement-Continuation Rule

    From Lawrence D'Oliveiro@21:1/5 to All on Thu Feb 8 01:42:15 2024
    Is this valid code?

    function escape_html(s)
    {
    return s
    .replaceAll("&", "&") /* always do first, rest can be in any order */
    .replaceAll("\"", """)
    .replaceAll("<", "&lt;")
    .replaceAll(">", "&gt;")
    .replaceAll("\t", "&#9;")
    .replaceAll("\n", "&#10;")
    } /*escape_html*/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jon Ribbens@21:1/5 to Lawrence D'Oliveiro on Thu Feb 8 02:26:24 2024
    On 2024-02-08, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
    Is this valid code?

    function escape_html(s)
    {
    return s
    .replaceAll("&", "&amp;") /* always do first, rest can be in any order */
    .replaceAll("\"", "&quot;")
    .replaceAll("<", "&lt;")
    .replaceAll(">", "&gt;")
    .replaceAll("\t", "&#9;")
    .replaceAll("\n", "&#10;")
    } /*escape_html*/

    Why wouldn't it be?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to All on Sat Feb 10 21:20:22 2024
    How about this:

    const SECONDS_PER_MINUTE = 60
    const SECONDS_PER_HOUR = 3600

    document.getElementById("content").innerHTML =
    Array.from
    (
    function*() /* generate entries */
    {
    for (let i = 0; i <= 32; ++i)
    {
    yield i * SECONDS_PER_HOUR / 4
    } /*for*/
    }(),
    function(t) /* format entries as hh:mm */
    {
    const hours = Math.floor(t / SECONDS_PER_HOUR)
    const minutes = Math.floor((t - hours * SECONDS_PER_HOUR) / SECONDS_PER_MINUTE)
    return "" + hours + ":" + (minutes < 10 ? "0" : "") + minutes
    }
    ).reduce
    (
    (sofar, elt) => /* join entries into a single string */
    sofar + (sofar != "" ? ", " : "") + elt,
    ""
    )

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to All on Tue Feb 20 06:03:30 2024
    Another example. This doesn’t work:

    return
    {
    source : "/autocomplete/search-widget.php",
    select :
    function (event, ui)
    {
    $("#select_widget_name").val(ui.item.label)
    $("#select_widget_id").val(ui.item.value)
    } /*function*/,
    }

    but this does:

    res =
    {
    source : "/autocomplete/search-widget.php",
    select :
    function (event, ui)
    {
    $("#select_widget_name").val(ui.item.label)
    $("#select_widget_id").val(ui.item.value)
    } /*function*/,
    }
    return res

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From John Harris@21:1/5 to Lawrence D'Oliveiro on Tue Feb 20 11:04:57 2024
    On 20/02/2024 06:03, Lawrence D'Oliveiro wrote:
    Another example. This doesn’t work:

    return
    {
    source : "/autocomplete/search-widget.php",
    select :
    function (event, ui)
    {
    $("#select_widget_name").val(ui.item.label)
    $("#select_widget_id").val(ui.item.value)
    } /*function*/,
    }

    <snip>

    Why do you call it "Statement-Continuation Rule"? Isn't it expression evaluation?

    John

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to John Harris on Tue Feb 20 19:40:24 2024
    On Tue, 20 Feb 2024 11:04:57 +0000, John Harris wrote:

    Why do you call it "Statement-Continuation Rule"? Isn't it expression evaluation?

    No, it’s about when a statement is considered to continue onto another
    line, and when it isn’t.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Mild Shock@21:1/5 to Lawrence D'Oliveiro on Thu Feb 22 11:37:52 2024
    Looks like an instance of method chaining style https://en.wikipedia.org/wiki/Method_chaining

    It is heralded as something powerful:

    Method chaining is a powerful programming pattern that allows you to
    call multiple methods on an object in a single line of code. It enhances
    code readability and conciseness by eliminating the need for
    intermediate variables or repeated method calls. In this blog post,
    we'll explore method chaining in Python, explain it using a simple
    example, discuss its use cases, and conclude with its benefits. https://nikhilakki.in/understanding-method-chaining-in-python

    But I have my doubts. Its also related to so called
    Fluent Interfaces. When you design APIs so that they
    support method chaining:
    https://en.wikipedia.org/wiki/Fluent_interface

    So called builders often exhibit a fluent interfrace. I
    recently had a revelation, that many builders
    are rather cheaters, for example I thought the appropriate
    thing to do would be:

    builder = builder.header(key, value);

    So the fluent interface would give me a new version of the
    build, with each method chaining call. Just like the replaceAll
    gives a new string. But this is often not the case,

    it would require that all headers are copied somehow. So we
    find in the implementation of header() that it just returns
    this, and the method chaining works with a side effect:

    public HttpRequestBuilderImpl header(String name, String value) {
    checkNameAndValue(name, value);
    headersBuilder.addHeader(name, value);
    return this;
    }

    So method chaining might not always satisfied the same expectations
    about being a more "functional" approach.

    Lawrence D'Oliveiro schrieb:
    Is this valid code?

    function escape_html(s)
    {
    return s
    .replaceAll("&", "&amp;") /* always do first, rest can be in any order */
    .replaceAll("\"", "&quot;")
    .replaceAll("<", "&lt;")
    .replaceAll(">", "&gt;")
    .replaceAll("\t", "&#9;")
    .replaceAll("\n", "&#10;")
    } /*escape_html*/


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From John Harris@21:1/5 to Lawrence D'Oliveiro on Thu Feb 22 10:16:00 2024
    On 20/02/2024 19:40, Lawrence D'Oliveiro wrote:
    On Tue, 20 Feb 2024 11:04:57 +0000, John Harris wrote:

    Why do you call it "Statement-Continuation Rule"? Isn't it expression
    evaluation?

    No, it’s about when a statement is considered to continue onto another line, and when it isn’t.

    Is that what ECMA 262 calls "automatic semicolon insertion" ?

    John

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Mild Shock on Thu Feb 22 19:36:47 2024
    On Thu, 22 Feb 2024 11:37:52 +0100, Mild Shock wrote:

    But I have my doubts.

    Show us your non-method-chained-style version, then.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Mild Shock@21:1/5 to Lawrence D'Oliveiro on Fri Feb 23 00:49:08 2024
    No syntactical doubts, only semantical doubts:

    So method chaining might not always satisfied the
    same expectations about being a more "functional" approach.

    Guido von Rossum suggest to not always use method chaining:

    https://mail.python.org/pipermail/python-dev/2003-October/038855.html

    Lawrence D'Oliveiro schrieb:
    On Thu, 22 Feb 2024 11:37:52 +0100, Mild Shock wrote:

    But I have my doubts.

    Show us your non-method-chained-style version, then.


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Mild Shock on Fri Feb 23 01:40:54 2024
    On Fri, 23 Feb 2024 00:49:08 +0100, Mild Shock wrote:

    Lawrence D'Oliveiro schrieb:

    On Thu, 22 Feb 2024 11:37:52 +0100, Mild Shock wrote:

    But I have my doubts.

    Show us your non-method-chained-style version, then.

    Guido von Rossum suggest to not always use method chaining:

    Which is not really answering my question, is it?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From John Harris@21:1/5 to Lawrence D'Oliveiro on Fri Feb 23 11:55:13 2024
    On 23/02/2024 01:40, Lawrence D'Oliveiro wrote:
    On Fri, 23 Feb 2024 00:49:08 +0100, Mild Shock wrote:

    Lawrence D'Oliveiro schrieb:

    On Thu, 22 Feb 2024 11:37:52 +0100, Mild Shock wrote:
    <snip>
    Guido von Rossum suggest to not always use method chaining:

    Which is not really answering my question, is it?

    In some scenarios it makes sense to allow chaining.
    For instance when building the answer to a query where the parts to be
    included depend on circumstances. As in x.a().b().e().g();

    In other scenarios it just makes things confusing for anyone reading the
    code, including the writer.

    In other words it depends on design judgement, something that disturbs
    people who prefer a 300 page book of rules.

    John

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)