DISQUS

Languages of the real and artificial: Functional JavaScript

  • sbecker · 2 years ago
    I think it would be more readable if the map/filter/reduce functions worked on the enumerable objects themselves instead of having to be passed in.

    Instead of this:
    map('x*x', [1,2,3,4])

    Why not this:
    [1,2,3,4].map('x*x')
  • JaskellScript - the contented · 2 years ago
    [...] Did you ever use Haskell?Haskell is a functional programming language (you’ll soon see what it means), and now, there’s a library for Functional JavaScript.And that’s what you can do with it: instead of…var numbers = [2, 93, 8894, 491, 7]; var sum = 0; for( var i = 0; i < numbers.length; i++ ) { sum += numbers[i] * 20; alert(sum); You would just say:alert( reduce( 'x+y', 0, map('*20', [2,93,8894,491,7]) ) );Okay, while this is just some loosy nonsense example, the possibilities opened up by the library are huge. Still, the greatest benefits from Functional JavaScript are higher order functions: now, even Currying or partial function application are possible!Look here:// Create functions on the fly! var divide = 'x/y'.lambda(); // Apply only one parameter and create a new function. var halve = divide.partial(_,2); alert( halve(6) ); // "3"! So… This is really, really hot so far. What’s missing are good functions for working with lists (or even List comprehension). Also, I’d doubt that this has good performance. However, especially in web desing, while working with DOM or those things, I’m sure Functional JavaScript will find it’s place.Give it a try, it’s hot! (And use the "live demo feature" on the site linked above! ) [...]
  • Dean Edwards · 2 years ago
    I'm having great fun pouring over your library. It is very well written and I'm learning a lot about functional programming from reading the source.

    Regarding speed, you can improve performance by giving up your favourite idiom:

    var args = [].slice.call(arguments, 0);

    This is expensive because it creates a new Array object every time you slice an arguments object. Which is very often.

    Mozilla provides a static slice method on the Array object. Or you can fake it yourself:

    if (!Array.slice) { // Mozilla already supports this
    Array.slice = function(array) {
    // Slice an array-like object.
    var slice = Array.prototype.slice;
    return slice.apply(array, slice.call(arguments, 1));
    };
    }

    And use it like this:

    var args0 = Array.slice(arguments); // cast to Array
    var args1 = Array.slice(arguments, 1); // a normal slice

    I use it so often I make it global.

    var slice = Array.slice;
    var args = slice(arguments);

    Hope that helps. Back to reading your source code for me. :)
  • Christopher Schmitt · 2 years ago
    [...] Functional JavaScript at Oliver Steele Functional is a JavaScript library for functional programming, a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. (tags: article filter javascript library programming tutorial tips functional development webdev webdesign) [...]
  • links for 2007-07-24 « Bloggit · 2 years ago
    [...] Functional JavaScript (tags: programming javascript) [...]
  • links for 2007-07-24 « Simply… · 2 years ago
    [...] Functional JavaScript at Oliver Steele (tags: ** article development filter functions functional programming javascript library webdev haskell guide) [...]
  • links for 2007-07-24 « Talkabo · 2 years ago
    [...] Functional JavaScript at Oliver Steele “In most languages, including JavaScript, invoking a function is one of the slowest things you can do. The implementations of languages designed for functional programming use a variety of techniques to optimize function calls. JavaScript is not one of th (tags: functional fp declarative map reduce filter mapreduce javascript viapopular) [...]
  • links for 2007-07-24 · 2 years ago
    [...] Functional JavaScript at Oliver Steele In most languages, including JavaScript, invoking a function is one of the slowest things you can do. The implementations of languages designed for functional programming use a variety of techniques to optimize function calls. JavaScript is not one of tho (tags: JavaScript webdesign) digg_url='http://xiled.rss-central.net/blog/2007/07/23/links-for-2007-07-24/'; digg_skin = 'compact'; digg_bgcolor = '#FFFFFF'; digg_title = 'links for 2007-07-24'; digg_bodytext = ''; digg_topic = ''; Powered by Gregarious (41) Share This [...]
  • Ajaxian » Functional JavaScrip · 2 years ago
    [...] Oliver Steele has a new library called Functional JavaScript that defines the standard higher-order functions (map, reduce, filter) as well as functions for partial function application and function-level programming: curry, partial, compose, guard, and until. Finally, it introduces “string lambdas”, which let you write 'x -> x+1', 'x+1', or even '+1' as synonyms for function(x) {return x+1}. [...]
  • jhorman.org - links for 2007-0 · 2 years ago
    [...] Functional JavaScript at Oliver Steele (tags: javascript functional) [...]
  • Functional Javascript « Bloomi · 2 years ago
    [...] Functional Javascript 26 07 2007 Currently i am working on Javascript and i am learning a lot.While searching for some code samples in Javascript I came across a new term Functional Javascript.We all know what is “Functions” in javascript.But what is functional javascript???Let’s see. [...]
  • Alex Ott · 2 years ago
    We had wrote such library for our product, as almost all developers was experienced Scheme developers ;-)
    JavaScript code in functional looks very elegant and readable
  • Functional JavaScript « Kiterm · 2 years ago
    [...] Jul 27th, 2007 by kiterminal วันนี้บังเอิญไปเจอ Blog ของ Oliver Steele ซึ่งเค้าไปทำ Functional JavaScript ไว้ ลองมาดูตัวอย่างที่เค้าทำให้ดูละกัน map(function(x){return x+1}, [1,2,3]) -> [2,3,4] filter(function(x){return x>2}, [1,2,3,4]] -> [3,4] some(function(w){return w.length < 3}, ‘are there any short words?’.split(’ ‘)) -> false // double the items in a list: map(’*2′, [1,2,3]) -> [2, 4, 6] // find just the odd numbers: filter(’%2′, [1,2,3,4]) -> [1, 3] // or just the evens: filter(not(’%2′), [1,2,3,4]) -> [2, 4] ทั้งนี้เค้ายังได้ทำ API documentation ไว้ให้อ่านและศึกษากันด้วย [...]
  • Thomas Herchenroeder · 2 years ago
    Just a quick fix: The example
    "map(guard('10+', not('%2')), [1,2,3,4])" in your Function-level programming section should probably produce
    "-> [1,12,3,14]", or shouldn't it?!
  • Oliver · 2 years ago
    Thomas, you're right; thanks for the find and the fix. I've fixed it above now.

    I wrote the code that splices the output values into the "examples page":http://osteele.com/sources/javascript/functional exactly because I knew I wouldn't successfully keep them up to date with the expressions, but I couldn't (or didn't take the time to) do the same thing on this blog page :-(
  • Javascript como SmallTalk e Ja · 2 years ago
    [...] E outro carinha criou uma biblioteca para escrever javascript funcional (map, reduce, filter), chamada “Functional Javascript” [...]
  • David Mercer · 2 years ago
    This library rocks!