5 ms·
I like the shortened function form. I played around with CS for a while before going back to JS, but I do miss being able to do something like: var names =
by untog 14y ago
I like the shortened function form. I played around with CS for a while before going back to JS, but I do miss being able to do something like:
var names = people.map((p) -> return p.name);
(or something along those lines). Funnily enough, it reminds me of C#'s LINQ:
var names = people.Select(p => p.name);
- tlack 14y agoApparently ECMAScript 6 is considering some other forms of function literals as well: http://www.2ality.com/2012/04/arrow-functions.html http://www.2ality.com/2012/04/arrow-functions.html
- untog 14y agoInteresting. As the TC article stated, it seems like a lot of this is based on ECMA proposals.
- wingspan 14y agoI've written a LINQ style library for TypeScript that allows you to write something like this: from([1,2,3]).select(n => n * n).where(n >=2).toArray() which outputs [4, 9] Hoping to open source it soon. Edit: typo, n * 2 should have been n * n, sorry for the confusion
- Kafka 14y agoCould you please explain why it would output that?
- wingspan 14y agoTypo, updated. Should have been n * n
- spicyj 14y agoHave you used the popular library Underscore.js http://underscorejs.org/ http://underscorejs.org/? The equivalent to your code in Underscore would be: _.chain([1,2,3]).map(n => n * n).filter(n => n >= 2).value()
- wingspan 14y agoI did look at underscore when figuring out how to design the library, along with many JS LINQ variants (http://ianobermiller.com/blog/2012/09/19/linq-for-javascript/ http://ianobermiller.com/blog/2012/09/19/linq-for-javascript...). It should be pretty straightforward to provide Underscore bindings for TypeScript. One big difference in the library I was working on is that it is lazy, and modeled after .NETs IEnumerable. Whether this is a good thing or not, I'm not yet sure.
- sciolistse 14y agoI've made a lazy linq-like library for javascript in the past.. The problem i had was that the native array methods are rather fast while function calls (for moveNext) are quite slow, so i couldn't get a whole lot of speed out of it. Newer javascript engines might be sufficient to offset that though.
- wingspan 14y agoI tried to get around this by creating overloads of almost all methods when you are operating on an array. `each` for instance, has a standard implementation using moveNext, and then an array implementation using a fast for loop. In some cases you could even drop down to native function calls.
- sciolistse 14y agoSounds reasonable. Could be better timing now as well, what with server-side javascript having become popular. Good luck with the library!
- colin_jack 14y ago
- murki 14y agoWouldn't it output [4, 6]?
- wingspan 14y agoAh yeah, typo, updated.
- baltcode 14y agoIt's actually even shorter: names = people.map((p) -> p.name)
- tlack 14y agoI wish Typescript would add "implied return of last statement" to the short function syntax! I love that feature in Erlang.
- zem 14y agothat's the feature i'm most surprised they didn't add. coffeescript already does it, for instance.
- ConstantineXVI 14y agoI'm getting the impression that TS is avoiding making too many dramatic changes to JS. Functions that return undefined in vanilla JS would suddenly return whatever happened to be the last statement; likely leading to unexpected behavior.
- zem 14y agoi wonder how hard it would be for their compiler to catch the opposite case, then - a program using the return value of a function without a return statement. i think that was my most common bug when i was doing javascript programming, because all the other languages i was using at the time had implicit returns.
- ConstantineXVI 14y agoClosure Tools will let you annotate your functions with a return type; their compiler will catch a function so annotated that lacks a return[0]. [0] http://sixlette.rs/snap/Closure_Compiler_Service-20121003-155648.jpg http://sixlette.rs/snap/Closure_Compiler_Service-20121003-15...