Giter Site home page Giter Site logo

Comments (2)

grabbou avatar grabbou commented on May 1, 2024

Also, this:

case data do
        :ok -> 
          Logger.info("info")
          Todo.add(data)
        :error -> 
          nil
      end

instead of becoming this:

(function(){
        if(Kernel.match__qmark__(Atom('ok'), data)){
          Logger.info('info');
          return Todo.add(data);
        }else if(Kernel.match__qmark__(Atom('error'), data)){
          return null;
        }
      }.call(this));

should become this

(function(){
       const ok = 'ok';
       const error = 'error';
       switch (data) {
          case ok: 

              break;
          case error: 

              break;
       } 
})

from elixirscript.

bryanjos avatar bryanjos commented on May 1, 2024
  • After taking a look at the current implementation, why we use Atom('ok') instead of just const

Originally atoms were implemented as ES6 Symbols (before I understood how they work), but when I started working on pattern matching, I thought at the time they wouldn't work out well.

Fast forward to this weekend when I saw how Symbol.for worked, I decided to use that instead. So now Atom("ok") returns a Symbol by doing Symbol.for("ok"). I left the function call to create it as so that if for some reason down the line Symbols don't work out, its just a matter of changing the implementation and not the interface.

  • we use List(1, 2, 3, 4, 5) instead of an array literal? [1, 2, 3, 4, 5] ?

This is pretty recent as well. It used to translate to standard arrays, but to prevent any mutation, List wraps a frozen array. Same idea as above, the implementation could change, but the interface stays the same. Also Lists have an iterator (basically just using the underlying array's iterator) so it gets the benefit of being able to be used in for..of loops as well as array destructuring.

  • Also, why we explicitly call this function with this?

That is so that when using this inside of the function closures for translating case, cond, and for expressions. It uses the this of the outer scope. Since in Elixir they are part of the current scope it makes sure they act that way.

Also in the code example in your last message, yes that could work even with Symbols, but the Kernel.match? function implements a lot of the current pattern matching algorithm and is more generic.

There are still some optimizations that can be done. I've thought about using the Google Closure Compiler, but it didn't look like they supported all of the ES6 features yet.

from elixirscript.

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.