News for hackers. Stuff that matters.
Re:Trine – A utility library for functional programming in J... by bshimmin on Thursday June 11, 2015 @09:39AM (#9699614)
About halfway through the README, after some examples of the new bind syntax, it says "But why stop there?" Actually, please can we stop here? Just for a bit? This continual, persistent mangling of JavaScript's syntax really is starting to hurt.

It's one thing to have to keep on top of the new frameworks springing up every five minutes - which you can, for the most part, ignore for at least six months until they start to gain some traction or have disappeared entirely - but when the core language itself is changing (and growing) at this rate, it really becomes an altogether more disconcerting and nightmarish proposition.

Re:Trine – A utility library for functional programming in J... by eueueu on Thursday June 11, 2015 @10:34AM (#9699935)
This isn't the core language changing though. This is people who are new to javascript coming to it and deciding they all must write their "hello world" framework / extension.

Most of it is just superfluous fluff that doesn't actually do anything but change the syntax to something they perceive as fundamentally better, when it's just a matter of taste and fashion.

Re:Trine – A utility library for functional programming in J... by escherize on Thursday June 11, 2015 @12:11PM (#9700530)
I'm not sure it's purely a matter of taste and fashion.

Let's say I make a new language called javascript++.

Instead of having `function` be a keyword, I chose `functioooooooooooooooooooooooooooon`. Based on their syntaxes, do you find one fundamentally better?

Re:Trine – A utility library for functional programming in J... by eueueu on Friday June 12, 2015 @07:53AM (#9705292)
Well clearly "function" is "better", because it's less prone to typos, easier to read, and makes more sense. But if you can make a good argument to go with "functioooooooooooooooooooooooooooon" then go for it.
Re:Trine – A utility library for functional programming in J... by striking on Thursday June 11, 2015 @11:16AM (#9700198)
I was almost afraid no one else thought this way. This just makes JavaScript illegible, and adds a negligible number of new features. The only JS I'm ever going to write is ES5, at this point, because the difference between

  function Person(){
    this.age = 0;
    setInterval(() => {
        this.age++;
    }, 1000);
  }
and

  function Person(){
    this.age = 0;
    var my = this;
    setInterval(function(){
        my.age++;
    }, 1000);
  }
are barely visible, but the second one is actually legible. Why are we arrowing in nothing into curly braces? Oh, it's actually a function. Great.
Re:Trine – A utility library for functional programming in J... by lobster_johnson on Thursday June 11, 2015 @11:34AM (#9700311)
A better example, in my opinion:

    let names = persons.map(p => p.name);
Compared to ES5:

    var names = persons.map(function(p) { return p.name; });
The arrow syntaxes increases readability pretty much everywhere. Promises, for example:

    save(value)
      .then(result => this.update(result))
      .catch(err => Application.showError(err, "Could not save."))
(The use of a function in the "then" here is to avoid having to bind() the function; the alternative would be: .then(this.update.bind(this)).)

Your setInterval example is a somewhat inappropriate example of the usefulness of arrows as it doesn't take any arguments and doesn't return anything (so you didn't need the braces).

But the fact that you have to alias "this" is a big argument in favour of arrow syntax. It may be trivial in a small example, but it's not trivial when extended to an entire app. You'll pretty much end up aliasing "this" in every single method. If you change any logic around, you'll end up having to either add new aliasing, or chase down unused alias variables, just to add/remove closures. (And what do you call that variable? Is it "this_" or "_this" or "self"? When working on a team you'll have to agree on a convention if the code is to remain readable.)

Re:Trine – A utility library for functional programming in J... by striking on Thursday June 11, 2015 @11:45AM (#9700370)
MDN thought it was a good example... [1]

And your second example; I was going to re-write it, but then I realized I couldn't because I didn't know what `this` was. I don't think `this` is useful, and I try to avoid it because of its malleability and the confusion it often brings because of its overly dynamic nature.

[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... , Ctrl-F for `Person` or something

Re:Trine – A utility library for functional programming in J... by lobster_johnson on Thursday June 11, 2015 @11:53AM (#9700420)
That MDN page gives a good example of how it works, but it's not intended to convince doubters with persuasive arguments.

Not sure why you don't know what "this" is in the second example. The whole point is that it's predictable. "this" is always the instance your method is defined in, unless you bind it to something else, which requires being explicit. I intentionally didn't include any context, but think about it this way:

  class Store {
    save(object) {
      this.client.put("/objects", JSON.stringify(object))
        .then(result => this.update(result))
        .catch(err => Application.showError(err, "Could not save."))  }
    }
  }
"this" is extremely useful. I'm not sure how you could argue otherwise. Here's another pattern I use all the time:

  this._socket = new WebSocket(`ws://${url}`);
  this._socket.onopen = () => {
    this._state = 'connected';
    this.emit('connected');
  };
The lambda syntax allows placing logic lexically where it belongs.
Re:Trine – A utility library for functional programming in J... by CodeMage on Thursday June 11, 2015 @11:31AM (#9700288)
I haven't touched JavaScript in years, so I'm curious about one detail: why does the second snippet need you to alias "this" to "my" and the first doesn't? If it wasn't for that, it would look like mere syntactic sugar for lambdas, but this difference makes me think there's something more to it.
Re:Trine – A utility library for functional programming in J... by lobster_johnson on Thursday June 11, 2015 @11:39AM (#9700335)
Function calls bind "this" to the caller. So if you do:

  foo.bar()
then inside bar(), "this" is foo. Whereas if you do:

  quux(function() { console.log(this); })
...the "this" will point to whatever quux's "this" is, and it could be anything, depending on what quux() is doing. You can't depend on "this" being correct here.

Hence people have for years used bind():

  quux(function() { console.log(this); }.bind(this));
But this is neither nice to read (or write), nor is it performant. Many libraries, such as Underscore, also allow you to pass in a context variable:

  quux(function() { console.log(this); }, this);
This requires that quux() passes the context as the "this" argument to the function.

The lambda (arrow) syntax fixes all of this [pun] by preserving "this" as a lexically scoped reference:

  quux(() => console.log(this));
Re:Trine – A utility library for functional programming in J... by mohaine on Thursday June 11, 2015 @11:41AM (#9700346)
It is a feature of the new syntax to bind the current context (what "this" points to) to the created function.

http://tc39wiki.calculist.org/es6/arrow-functions/

Re:Trine – A utility library for functional programming in J... by striking on Thursday June 11, 2015 @11:40AM (#9700342)
It actually is required, because `this` is mangled whenever you create a function. If I added .bind(this) to the function, it would also work, but I alias the object instead. Yes, it's confusing, but the fact that there's a difference is even more confusing.
Re:Trine – A utility library for functional programming in J... by notNow on Thursday June 11, 2015 @12:42PM (#9700750)
"this" inside the setInterval in the 2nd example points to the global object in the browser case it's the window. So, he had to cache/alias the value prior to execution to avoid the confusion.
Re:Trine – A utility library for functional programming in J... by davnicwil on Thursday June 11, 2015 @08:20PM (#9703263)
I agree that the second example, using (var my = this) and then using my inside the anonymous function is not much of a hassle.

However, in my opinion, repeating this many many times throughout a codebase is irritating to write, and adds extra weight and bug surface area to the code.

Therefore adding some syntax sugar to remove the need to do this, i.e. the first example, is a nice feature. I know opinions on syntax and formatting are pointless and endless, but could you please explain why, for you, is it less legible? Once you're used to it, don't you just scan () => as no-arg anon function signature the same way you scan function() as that now?

Re:Trine – A utility library for functional programming in J... by vkjv on Thursday June 11, 2015 @12:40PM (#9700737)
I agree, but closures and bine are expensive--present in both cases. It's better to avoid it. Granted, this is a contrived example and sometimes closures are the more elegant solution.

  function Person() {
  	this.age = 0;
  	setInterval(this.incAge, 1000, this);
  }
  Person.prototype.incAge = function personIncAge(that) {
  	that.age += 1;
  };
http://jsperf.com/bind-vs-closure-vs-param
Re:Trine – A utility library for functional programming in J... by Nemcue on Thursday June 11, 2015 @04:18PM (#9702105)
Ugh — this plague again. No other coding community has latched on to performance tests like the javascript one. For NO GOOD REASON.

They're not expensive. You're not binding thousands of times a second anyway, so stop your silly premature optimizations.

Re:Trine – A utility library for functional programming in J... by vkjv on Monday June 15, 2015 @11:08AM (#9719789)
1. I already gave an out for your point. Yes, always weigh performance vs. clarity and don't over optimize.

2. You are making an assumption that you aren't binding thousands of times a second. You need to take actual use case into consideration.

3. My example has a 1000:1 method call to bind ratio and it's still a significant difference in benchmarks. Yes, it's that expensive it's important to think about in critical sections of code.

Re:Trine – A utility library for functional programming in J... by _pmf_ on Thursday June 11, 2015 @11:33AM (#9700300)
Meh; rockstar developers with no CS background working for over valuated MVP companies implementing half understood techniques described in shady Haskell tutorials cannot possibly be wrong.
Re:Trine – A utility library for functional programming in J... by quinnirill on Thursday June 11, 2015 @04:41PM (#9702254)
Well, the no CS background and shady Haskell tutorials parts were correct; My highest formal education is high school. ;) But this was done on my free time, and I don't really identify myself as a rockstar developer. Sounds pretty arrogant to me.
Re:Trine – A utility library for functional programming in J... by notNow on Thursday June 11, 2015 @12:08PM (#9700512)
Do you know that the arrow function was the main reason that I made the switch from Chrome to FF Dev as my primary web browser and I never looked back?

You don't know how much you're missing here. I wish that ECMAScript* considers adding support for "indented syntax" like in Sass to make it even more easier to read and go through the codebase without seeing all these ugly braces around.

*: or a white knight programmer volunteering to offer this functionality for us all :)

Re:Trine – A utility library for functional programming in J... by inglor on Thursday June 11, 2015 @12:22PM (#9700596)
That's CoffeeScript right there for you.
Re:Trine – A utility library for functional programming in J... by notNow on Thursday June 11, 2015 @12:26PM (#9700629)
I'm familiar with CoffeeScript but AFAIK it's a distinct language probably a dialect of JS.

I just want the indentation without all the baggage that comes along with CoffeeScript.

Re:Trine – A utility library for functional programming in J... by delluminatus on Thursday June 11, 2015 @10:56AM (#9700068)
I disagree. I think that extending and improving a language is good, and it's certainly not a Javascript-only phenomenon.

Off the top of my head, other languages that are considering and implementing syntax extensions include Python (e.g. "yield from"), C# (e.g. "async/await"), and Java (lambda expressions).

I actually think that it's pretty low to accuse ECMAScript of "mangling" JS syntax when they are just trying to add modern extensions that make common patterns easier. Of course this function bind syntax is just a proposal and hasn't been accepted into the standard yet. But what about it bothers you so much? What about iterators or shorter function definitions, are those also nightmarish manglings of syntax? I mean, really. The usage of Javascript is evolving from simple embedded scripts to gigantic client/server applications. It's no surprise that the language itself will evolve as well.

Re:Trine – A utility library for functional programming in J... by bshimmin on Thursday June 11, 2015 @11:34AM (#9700306)
My objections are around the rate of change and the fact that JavaScript is growing into a large, complex language that people are becoming really uncertain how to use.
Re:Trine – A utility library for functional programming in J... by delluminatus on Thursday June 11, 2015 @11:59AM (#9700459)
I could see how that's a legitimate concern, it reminds me of some of the complaints I hear about C++ having too many features (I don't use C++, so this is just hearsay). But Javascript is still a much smaller and simpler language than almost anything else out there. Sure, it has some semantic quirks, but the syntax is fundamentally minimal. I think it will be a long time yet before we reach the point of being really overcomplicated.

I think the uncertainty around Javascript (and I do agree that there seems to be a real sense of confusion in the community when you look at the explosion of libraries and frameworks) is more related to the fact that we are trying to solve hard problems in a language that really gives us no help. We get no type checking, no good concurrency APIs, no immutability, no good sequence abstractions until iterators, just a fairly low-level and extremely hackable language with an unusual prototypal inheritance system.

Well, who knows. I would personally like to see some more stuff be standardized in the language or the official APIs so we don't end up with four competing libraries implementing "map" and "reduce", or promises, or whatever other thing.

Re:Trine – A utility library for functional programming in J... by damagedcake on Thursday June 11, 2015 @09:25PM (#9703485)
So interesting thought here...but C# might be a better comparison. This is a language that has seen a lot of change and a lot of added features. But at least some of the earlier features that were part of the language are no longer used much. I think js is more like C# than C++ in this way.
Re:Trine – A utility library for functional programming in J... by wwweston on Thursday June 11, 2015 @12:35PM (#9700694)
JavaScript has always been a language people were uncertain how to use.

The only difference with ES6+ is that the uncertainty will come from the addition of features people have complained were missing, instead of the traditional uncertainty that came from the intersection of its flexibility and dev's desire not to learn it.

Re:Trine – A utility library for functional programming in J... by CrossEye on Thursday June 11, 2015 @01:21PM (#9701018)
No, in fact the real problem has been that too many incompetent people were far too certain about how to use it.
Re:Trine – A utility library for functional programming in J... by Nadya on Thursday June 11, 2015 @03:14PM (#9701678)
The selling point of JavaScript to me, 12 years ago, was "You don't need to know JavaScript to use JavaScript" and most of the people I saw using it were uncertain of how to actually use it (most of those people also being 13-17 in the crowd of web-design forums I hung around with).

I feel that hasn't changed over the years outside of more and more people not knowing how to use JavaScript (and more and more people who actually know their script).

Re:Trine – A utility library for functional programming in J... by woah on Thursday June 11, 2015 @09:03PM (#9703417)
You are the only person who is really uncertain on it.
Re:Trine – A utility library for functional programming in J... by colin_jack on Thursday June 11, 2015 @11:21AM (#9700219)
You have a point but you mention C#, it's worth thinking of how you'd handle one of the examples in C#:

items .filter(isOk) .map(toOtherType) ::flatten()

In C# that sort of case is primarily handled by extension methods. They have their issues not least in terms of collisions, but to me they are a beautiful solution. The equivalent in JS would seem to be the always unpopular choice of adding these methods to the prototype, and as with extension methods you'd want the user to opt-in to their addition (possibly at object level here?).

Not sure though, maybe I've missed something.

Re:Trine – A utility library for functional programming in J... by delluminatus on Thursday June 11, 2015 @12:05PM (#9700497)
I think extension methods in C# are basically a way to work around the fact that you can't just stick a method on the prototype like you can in Javascript. I also like the C# solution, actually, I just love C#. It's a great "daily driver" language in my book.

If you took the function bind syntax to its natural extreme, you end up with a model like Python or Nim, where all instance methods accept their "this" as a first parameter, and "a.call(b)" is equivalent to "call(a, b)". Personally I always thought that was a particularly elegant way of implementing instance methods, because it doesn't require inheritance or modifying the original type.

In Nim it even lets you do some very interesting method chaining to replace nested calls, like "toInt(sqrt(toFloat(num)))" can be equivalently written "num.toFloat.sqrt.toInt"

Re:Trine – A utility library for functional programming in J... by colin_jack on Friday June 12, 2015 @02:58AM (#9704410)
Yup agreed but the prototype approach is scarier as it affects every user of the prototype, where as with namespaces you opt-in to bringing in extension methods at the file level by importing the associated namespace.

Not sure on the whole this as argument, I prefer it to the current approach of this being decided at framework/library level because I get bored of having to bind each function to get lexical scoping back. That Nim code is indeed beautiful though, maybe need to give ti a look ta.

Re:Trine – A utility library for functional programming in J... by lightblade on Thursday June 11, 2015 @06:33PM (#9702902)
Oh you missed out so much by stopping at "But why stop there?"

> ...can also represent infinite sets

This is the highlight.