News for hackers. Stuff that matters.
Re:Deno Joins TC39 by j-krieger on Monday December 13, 2021 @07:24AM (#29538944)
Congrats on your new role! Currently, I feel like addition to JavaScript is a lot slower than new CSS features, for example. What do you think are the chances that JS will one day get a larger batch of STL functions, instead of about a dozen each year?
Re:Deno Joins TC39 by lucacasonato on Monday December 13, 2021 @07:39AM (#29539011)
I hope the pace will accelerate. The real questions is what functions we need though. Some candidates that I would love to see are better helper functions on iterators, and Uint8Array<->base64/hex. Most of the "standard library" in most languages is related to IO, and for JS is dependant on the host (the web, Deno, Node) so not something TC39 will touch directly. Do you have ideas for standard library functions that you think are missing?
Re:Deno Joins TC39 by FragenAntworten on Monday December 13, 2021 @08:57AM (#29539590)
'enumerate', as in Python.

'clamp' and 'sortBy', as in Lodash.

Set methods for union, intersection, difference, etc.

Re:Deno Joins TC39 by tempodox on Monday December 13, 2021 @11:12AM (#29540895)
Re set operators: For completeness' sake there would have to be a way to specify how set elements are to be tested for equality.
Re:Deno Joins TC39 by amitport on Monday December 13, 2021 @07:42AM (#29539030)
'range' a-la python
Re:Deno Joins TC39 by wruza on Monday December 13, 2021 @05:27PM (#29545492)
Range a-la python is a generator and is pretty straightforward in js as is. There is no need for a complicated solution and/or as part of stdlib, imo:

  function* range(x, y) {
    while (x <= y) {yield x++}
    // or ‘<‘ if you want half-open one
  }

  for (let n of range(3, 5)){
    console.log(n)
  }

  array = [...range(3, 5)]
  console.log(array)
Re:Deno Joins TC39 by amitport on Tuesday December 14, 2021 @04:00AM (#29549684)
It's also straight forward to implement in python. It is common enough to be standard so people won't reinvent the wheel. And it should be available to people learning the language before they learn generators.

Array.prototype.indexOf is also pretty simple as are many core functions.

Re:Deno Joins TC39 by wiredearp on Monday December 13, 2021 @09:27AM (#29539837)
Immutable data structures are on the agenda over in https://github.com/tc39/proposal-record-tuple and I would vote for that in Deno while I have the chance.
Re:Deno Joins TC39 by lucacasonato on Monday December 13, 2021 @12:26PM (#29541817)
Yup, we'll ship them once they go Stage 3/4 (when Chrome ships them in stable).
Re:Deno Joins TC39 by Zababa on Monday December 13, 2021 @08:55AM (#29539570)
I often miss map, reduce and filter on iterators, though this is already a stage 2 proposal: https://github.com/tc39/proposal-iterator-helpers.
Re:Deno Joins TC39 by lucacasonato on Monday December 13, 2021 @12:10PM (#29541605)
This is definitely something I am interested in pushing too. Iterators have not gotten the love they deserve.
Re:Deno Joins TC39 by Zababa on Monday December 13, 2021 @12:12PM (#29541637)
That's great to hear! Congratulations and thank you for your work.
Re:Deno Joins TC39 by tarjei_huse on Monday December 13, 2021 @09:25AM (#29539818)
I miss being able to create a hash from an array in a quick way. You always end up with

let bar = [{id: zz, }, {id: y},...]

let foo = {}

bar.forEach(v => foo[v.id] = v)

I'd love to have something like:

let foo = bar.toMap(v => [v.id, v])

Re:Deno Joins TC39 by keville on Monday December 13, 2021 @10:05AM (#29540193)
const foo = bar.reduce((hash, item) => { hash[item.id] = item; return hash; }, {});
Re:Deno Joins TC39 by wiredearp on Monday December 13, 2021 @09:30AM (#29539879)
new Map([[k1,v1], [k2, v2]]) can do that. Or Object.fromEntries with the same argument.
Re:Deno Joins TC39 by tarjei_huse on Monday December 13, 2021 @09:42AM (#29539980)
Thanks!

You still need to create the middle datastructure ( [[k1,v1], [k2, v2]] ) but it is an improvement :)

Re:Deno Joins TC39 by wiredearp on Monday December 13, 2021 @10:03AM (#29540179)
Ah yes, true, you always need to map the things ID to the thing itself, so that middle data structure is actually quite annoying :)
Re:Deno Joins TC39 by runarberg on Monday December 13, 2021 @11:23AM (#29541005)

    let foo = Object.fromEntries(bar.map((v) => [v.id, v]));
Re:Deno Joins TC39 by keville on Monday December 13, 2021 @05:15PM (#29545387)
It is not necessary to create any extraneous data structures to do OP's one-liner. This creates n+1 new arrays that must be garbage-collected.
Re:Deno Joins TC39 by runarberg on Monday December 13, 2021 @06:09PM (#29545934)
Object.fromEntries takes iterable, so if you have a map from a handy iterator library you could get rid of the parent array returned from `bar.map`.

    import { map } from "my-iter-lib";
    
    Object.fromEntries(map((v) => [v.id, v], bar));
Or with the proposed iterator-helpers:

    Object.fromEntries(
      Iterator.from(bar).map((v) => [v.id, v]),
    )
However the inner arrays are harder to get rid of... In fact even OP’s oneliner defines the inner arrays. I would hope there were some engine optimizations though which could minimize their footprint.
Re:Deno Joins TC39 by keville on Monday December 13, 2021 @09:06PM (#29547318)
Please see my other comment in this thread for the `reduce`-based solution that requires no extra data structures. They're not that hard to get rid of!
Re:Deno Joins TC39 by amitport on Monday December 13, 2021 @07:47AM (#29539058)
'zip' a-la python
Re:Deno Joins TC39 by lucacasonato on Monday December 13, 2021 @07:52AM (#29539095)
I have often needed this too. Noted.
Re:Deno Joins TC39 by amitport on Monday December 13, 2021 @07:47AM (#29539055)
'reversed' a-la python
Re:Deno Joins TC39 by lucacasonato on Monday December 13, 2021 @07:51AM (#29539088)
Currently in stage 2 as `Array.prototype.toReversed`: https://github.com/tc39/proposal-change-array-by-copy
Re:Deno Joins TC39 by amitport on Monday December 13, 2021 @07:56AM (#29539119)
Nice, I didn't know about that, but in any case note that IMO it should be part of the Iterable protocol. Also note that the proposal above creates a copy on reverse AFAIKT.
Re:Deno Joins TC39 by aadams on Monday December 13, 2021 @09:56AM (#29540108)
Please have someone on your team take a good hard look at the current proposal-pipeline-operator: https://github.com/tc39/proposal-pipeline-operator

It started as a Function Composition proposal (using the pipe operator |>) but after a change of leadership it has turned into something much different. We might need another perspective on the current trajectory of this proposal, as in its current form it seems to many in the community it might take JS in the wrong direction.

Thanks!

Re:Deno Joins TC39 by wruza on Monday December 13, 2021 @06:01PM (#29545855)
If you want a feedback, I do not find |> versions much (if at all) easier to read. The noise is as high as in a regular function-based chaining:

  await chain(
    Object.keys(envars)
      .map(envar => `${envar}=${envars[envar]}`)
      .join(' '),
    text => `$ ${text}`,
    text => chalk.dim(text, 'node', args.join(' ')),
    colored => console.log(colored),
  )
I don’t want to sound too negative, but this seems like a pointless extension only to bring the burden of support for some syntactic flavor.

React example is special though. It feels like very helpful in expr-only context, but in-array ifs and fors would do better there:

  <ul>
    {[
      for (v of vs) {
        const text = foo(v)
        yield <li>{text}</li>
      },
      if (vs.length == 0) {
        yield <li>No items</li>
      },
    ]}