Coder Social home page Coder Social logo

Comments (5)

schurhammer avatar schurhammer commented on September 26, 2024 1

Hi lpil, I've started working on this.

The only down side I can see so far is the readability of the output JS for long lists. Do we particularly care about that?

e.g.
this is pretty ugly (although from my testing, the fastest)
new $NonEmpty(1, new $NonEmpty(2, new $NonEmpty(3, new $Empty())))

We could put a cons function in the prelude which would help clean it up a bit, and empty can be a constant. The cons function adds very minor overhead, imo negligible. These could also have even shorter aliases in the import (?).
$cons(1, $cons(2, $cons(3, $EMPTY)))

Another idea is switching back to toList when there are many elements added at once, say >2 or so. Could be used in combination with aliases of course.

from gleam.

lpil avatar lpil commented on September 26, 2024

I'm surprised there's quite such a different as toList(first, rest) is just first.reduceRight((xs, x) => new NonEmpty(x, xs), rest);, but any faster approach would be gladly accepted.

from gleam.

lpil avatar lpil commented on September 26, 2024

Wonderful! Thank you.

An alias could be good too, rather than a function wrapper. There would be no overhead in that case.

Would just a single character such as $ be too opaque? Assuming that one is free.

from gleam.

schurhammer avatar schurhammer commented on September 26, 2024

The problem with an alias without the wrapper function is that you still need to put the new keyword every time, so the minimum you can get is 5 characters.

Single character is fine with me 🤷
$ is being used for asserts though (probably could free it up)

What should we do about lists without a rest param e.g. just [1,2,3,4,5]?
I imagine they have the same performance gain to be had from switching, but also they are more likely to be large and thus ugly.

from gleam.

schurhammer avatar schurhammer commented on September 26, 2024

My thoughts after some testing:

  1. Wrapper function good. Seems to make things more predictable for some reason.
  2. Single character was hard to read, I think just go with $prepend. The vast majority of prepends are just one item anyway.
  3. Leave lists that are not prepends as-is. I didn't measure a performance improvement from changing these.

from gleam.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.