Live data from Hacker News

Trine – A utility library for functional programming in JavaScript

github.com

111–116 of 116 posts

Re: Trine – A utility library for functional programming in JavaScript

#111

Earlier quoted context omitted.

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 prot…

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 fir…

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 JavaScript

#112
post #107

WTF!! One of your iterator example for primes uses GOTO (aka labels). It may be different where you work, but anyone in my company using labels either has the mother of all excuses or is going to be a warning away from termination.

Sounds like a very healthy company culture. ;)

Re: Trine – A utility library for functional programming in JavaScript

#113
post #39

Earlier quoted context omitted.

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.

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?

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 JavaScript

#114

(Disclosure: one of the authors of Ramda here, so I may well be biased toward a very different approach.) This is an interesting approach, but I get caught right at the beginning, where you say "What you'd really want to do is this: items .filter(isOk) .map(toOtherType) .flatten() But what I really want to do is build a reusable function: var process = seq(filter(isOk), map(toOtherType), flatten); So I can at my leis…

With Trine this would be function process () { return this ::filter(isOk) ::map(toOtherType) ::flatten(); } and then later: groupsOfItems::map(process); Granted, the `flatten` function is not in the Trine yet, somehow forgot that from the initial release.

Ok, that helps make it a bit more palatable to me.

Thanks for the information.

One question, though: How do user functions interact in here? If you didn't include `flatten`, but I had my own version of it, would it be straightforward for me to build `process` using my own `flatten`? Do I have to modify some Trine objects, or can I use some simple function references?

Obviously I haven't yet actually dug into the Trine code. Perhaps this weekend.

Re: Trine – A utility library for functional programming in JavaScript

#115

Earlier quoted context omitted.

With Trine this would be function process () { return this ::filter(isOk) ::map(toOtherType) ::flatten(); } and then later: groupsOfItems::map(process); Granted, the `flatten` function is not in the Trine yet, somehow forgot that from the initial release.

Ok, that helps make it a bit more palatable to me. Thanks for the information. One question, though: How do user functions interact in here? If you didn't include `flatten`, but I had my own version of it, would it be straightforward for me to build `process` using my own `flatten`? Do I have to modify some Trine objects, or can I use some simple function references? Obviously I haven't yet actually dug into the Trin…

Happy to help!

Yes, you can use just simple function references, e.g. you could just define flatten as so

    function * flatten () {
      for ( const item of this ) {
        yield * item;
      }
    }
And it would work just like it was part of Trine - in fact that's probably how it's going to be implemented in Trine. The chaining is not a feature of Trine, but the function bind syntax proposal ( https://github.com/zenparsing/es-function-bind ), Trine has merely been designed to work well with this syntax.

Re: Trine – A utility library for functional programming in JavaScript

#116
post #95
post #83

Earlier quoted context omitted.

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

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.

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.

Post reply on HN