14

I'd like to use a "javascript derived language" in order to learn and use some of the usual web technologies today like node.js, jquery, etc.

After toying for a while with functional concepts and languages, mostly F#, I'm looking if there's any similar thing in the Javascript world. So far what I've found and considered is:

  • Javascript the functional way: use javascript's functional parts as extensively as I can, making use of Underscore or other functional libraries. The pros to this approach would be to learn the "mother language". The cons for me are that I really don't like its syntax and doesn't feel very natural to program "functional first" in Javascript.
  • CoffeeScript + Underscore: I've seen some examples using CoffeScript + Underscore which seem appealing. For example: http://autotelicum.github.com/Smooth-CoffeeScript/SmoothCoffeeScript.html#toc-Chapter-7
    The pros to this would be that CoffeScript is spreading wildly and seems the defacto standard of languages designed for its use with a javascript VM. It syntax feels more natural to me and I could program some things the functional way in an easier manner.
    The cons would be getting far away from javascript itself (which really isn't too much of a problem to me) and to be in middle ground of non truly functional.
  • Livescript: Derived from Coco (which itself was derived from CoffeScript), it has the more appealing syntax to me (it even has my beloved pipeline '|>' operator from F#). The pros are that it is the most functional like approach. The cons would be that it isn't very spread (I made a search in SO and there's really NOTHING regarding it) and all that implies (not much community, support, tutorials, etc).

After thinking for a while on all this, I have no real conclusión so it'd be great if people used to working with javascript, CoffeeScript, etc could guide me on the real importance of this concerns.

Just in case... I haven't really considered using Pit (and so continue using F#) because although it compiles to javascript, I think it doesn't integrate with any javascript library like the above and doesn't follow the same workflow or integrate with the javascript ecosystem as well as those options listed.

Thanks a lot!

Ibn Saeed
  • 3,171
  • 11
  • 50
  • 59
Jacobo Polavieja
  • 786
  • 1
  • 6
  • 16
  • 5
    Interesting, but not a question well-suited to SO. –  May 24 '12 at 22:20
  • 1
    Like pst said: Probably not question well-suited to SO. But anyway: I'd say that if you want to learn JavaScript, then learn _JavaScript_ including all the prototypal/OOP/non-functional stuff - don't try to make JS into something you already know or ignore the non-functional before you even start. I'd encourage you to start there at least - otherwise you'll be coding against layers of abstraction and translation. You can get into that later. Besides, starting from scratch is where you really learn. – Flambino May 24 '12 at 22:32
  • @pst, I didn't posted earlier due to that... maybe better suited to programmers.stackexchange? If so, I hope someones closes this and I'll try there. – Jacobo Polavieja May 25 '12 at 00:25
  • @Flambino, the thing is I really don't have the time (nor the interest) to learn JS + another one on top of that. As you say, it's probably not a good idea to try and make JS what it isn't... therefore it may be better to forget about it and use another option. The real motivation is to be able to interact with the javascript world and ecosystem... with a functional syntax as possible. – Jacobo Polavieja May 25 '12 at 00:29
  • 1
    @JacoboPolavieja Fair enough. I haven't had the time/opportunity to _really_ learn a purely functional language, so I know what you mean. I will say, though, that personally I find JS pretty great. It's got some legitimately weird parts, but it's also fantastically flexible (and thanks to CoffeeScript it's become _fun_ to write too, IMHO). Let alone the fact that it mixes different paradigms is pretty interesting - which is also why I don't think you should limit yourself to just one. So if you do find the time, give it a shot. – Flambino May 25 '12 at 02:27
  • @JacoboPolavieja What's a functional syntax? – Marcin May 25 '12 at 15:03

3 Answers3

10

I'm probably a bit biased (I am the creator of LiveScript), but you should try LiveScript! I'm currently working on a standard library for it that will integrate better than underscore. As you say, there aren't that many people using it yet, but a community has to start somewhere! As you've pointed out, it seems like the best option all other things being equal - so take a dive and try it!

gkz
  • 248
  • 1
  • 7
  • Hi @gkz! Great to hear directly from you. My main concern is that you get tired of developing it, don't have the time to continue, etc... I really don't have the knowledge right now about general functional programming and javascript to be able to contribute any great thing. On the support side, do you have any plans? Maybe at least have a 'livescript' tag here in SO? I tried to create it but don't have the necessary number of posts. If you ask, being the creator of LiveScript I guess there wouldn't be any problem. Right now I'm leaning towards CS + Functional/Underscore or LiveScript. Hmmm... – Jacobo Polavieja May 25 '12 at 09:58
  • The thing with LS is that, in worst case scenario that it gets unmaintained, I could always use the translated javascript code and continue the projects directly in JS or even use any of the JavaScript to CoffeeScript translators if I want to continue with CoffeeScript. I would lose time but not code, which is always a good thing. Have you thought about incorporating any of the concurrency techniques of F#? I really like what they're doing in that department, although the limitations of javascript VMs are there in this case... And last but not least, are values immutable by default in LS?Thx! – Jacobo Polavieja May 25 '12 at 10:03
  • 1
    I will check [tag:livescript] almost every day and answer any questions. There are other contributors to LiveScript, including the person whose project LiveScript is forked from. If for some reason I am not able to continue I will hand over admin rights to them. – gkz May 26 '12 at 21:59
  • 1
    I'm not really sure if I can add the concurrency stuff from F# - as LiveScript compiles to JavaScript and it is thus limited to what JavaScript can do. Values are not immutable by default - again this is not currently a feature in JavaScript I can compile to. It would be possible to do some sort of check at compile time but this would be very complicated - and would break down when interacting with outside JavaScript. Also - I just created [prelude.ls](https://github.com/gkz/prelude-ls) which gives you the ability to do some cool things. Eg: [1 2 3] |> map times 2 |> filter gt 5 – gkz May 26 '12 at 22:02
  • Also I see that you need 1500 reputation to crate a new tag - something I (or you) do not have unfortunately. Thus we can't actually create the tag 'livescript' for now. – gkz May 27 '12 at 00:09
  • 1
    @JacoboPolavieja I've just added partially applied operators to LiveScript - you can now do: [1 2 3] |> map (* 2) |> filter (< 5) |> fold1 (+) – gkz May 29 '12 at 01:12
2

You basically have two options:

  • Use a JS-derived language like CoffeeScript
  • Use a functional language to JS compiler

For option 1, well, it's probably your best option for interoperability with other JS code. It's also your best option if you actually want to work with other JS developers who may not be familiar with <insert functional language>.

If you aren't actually very familiar with JS, I would suggest option 1. It may be tricky to understand how things work especially if you run into bugs in the language/tool of your choice if you don't understand the JS underpinnings.

Option 2 can be more interesting to use as you would probably get the benefits of your favorite language into JS. In addition to the Pit project you mention, there are some attempts at making Haskell compile into JS which could also be an option.

Option 2's downsides are probably the fact the projects aren't (afaik) very advanced to this day yet and that it may compile into not very efficient JS code.

Lastly, there was some Haskell-style language which was based on JS, similar to CoffeeScript, but I seem to have forgotten its name. It might be worth a shot, if someone can figure out what it was called :D

Jani Hartikainen
  • 42,745
  • 10
  • 68
  • 86
  • No idea if it's what you're referring to, but [Clojure](http://clojure.org/) is mostly a functional language and compiles to javascript. – obmarg May 24 '12 at 22:27
  • @obmarg: mostly, Clojure compiles to Java Virtual Machine bytecode. Java (and the JVM) have nothing to do with JavaScript besides a name similarity chosen for marketing synergy. (Though folks have written Javascript interpreters in Java, notably Rhino). **EDIT**: (thanks, am not i am) However, there is a port of Clojure called ClojureScript that compiles to JavaScript instead; not sure how mature it is. – Mark Reed May 24 '12 at 22:31
  • @MarkReed: I was about to leave the same comment, but then I read this on Clojure's homepage *"Clojure is a dynamic programming language that targets the Java Virtual Machine (and the CLR, and JavaScript)."* Looks like there's a ClojureScript project. –  May 24 '12 at 22:32
  • Thanks all. As for ClojureScript, as far as I know it's the same case as with F#. They compile to Javascript, but you can't directly interact with the any library of the JS ecosystem from the code. For example, Pit has now released and extension to be able to interact with JQuery... I want to be able to interact directly, with anything JS. If not, F# + Pit would be a no brainer for me. The way I see it, it's different to be able to compile to JS than to be able to form part of the Javascript ecosystem, but I may be missing something. Thanks again. – Jacobo Polavieja May 25 '12 at 00:32
  • 2
    @Jani About the Haskell-like language on top of JS so far I've found: lambdascript (https://github.com/valderman/lambdascript), haste (https://github.com/valderman/haste-compiler), Roy (http://roy.brianmckenna.org/), jsHaskell (http://code.google.com/p/jshaskell/). Any of those rings a bell? – Jacobo Polavieja May 25 '12 at 01:05
  • @JacoboPolavieja Roy was the language I was referring to. Haven't used it so can't really say if it's any good or such though – Jani Hartikainen May 25 '12 at 13:03
2

And what about just a library of ""FUNCTIONAL"" functions , + coffeescript + underscore ;) !

http://osteele.com/sources/javascript/functional/

mpm
  • 20,148
  • 7
  • 50
  • 55
  • This was my first thought after a preliminar search on the topic, and that library seemed good enough. This combination seems to get bits of both "worlds": more functional (although not as much as I'd like to) and huge user base/community. I had almost decided on going down that path till I saw the Livescript project. If it had more people using it so it became more popular (ie, "guaranteeing" the future of the project and more resources in general), I'd choose Livescript. As I've never actually touched javascript, I can't imagine all the possible implications of my decision. – Jacobo Polavieja May 25 '12 at 00:39