Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #32239

Re: Two quick question regarding FP (Functional Programming)

From Scott Sauyet <scott@sauyet.com>
Newsgroups comp.lang.javascript
Subject Re: Two quick question regarding FP (Functional Programming)
Date 2017-01-21 19:16 +0000
Organization A noiseless patient Spider
Message-ID <o60c2h$sfi$1@dont-email.me> (permalink)
References <1ad37323-fc7b-4602-9a59-d56eed42bb27@googlegroups.com>

Show all headers | View raw


justaguy wrote:

> Based on Scott Sauyet's recommendation of two books, I've started to
> read "Mostly Adequate Guide", now, I have two questions.

> (a) Is there something sort of online library that you can simply point
> to tools for FP such as lodash?  If so, for one thing, one does not need
> to install them.

Almost any such library with a certain level of maturity will document 
how you can install them or use them through CDNs.  Lodash's download 
section [1] points to one CDN, but it is available on others.  Ramda's 
installation section [2] points to two, but it's also available on 
others.  This is partly because some CDNs (for instance wzrd [3]) auto-
publish Node modules on CDNs.  If you're using them on the server side in 
Node.js, then these will all be available with an `npm install` and a 
`require('library-name')`.


> (b) In his chapter 5 about Composition, he claimed FP has the advantage
> of "Pointfree" vs. "Not Pointfree" but the example he quoted seems to
> suggest, the latter actually is easier to read, I really don't see its
> benefit and maybe because I don't know FP. Besides, he also added,
> "Pointfree" is a double-edged sword.  Your thoughts?

Let's assume you understand the meanings of `map`, a function, which for 
our purposes transforms one list to another by applying a function to 
each member, and `square`, a function which transforms one number to 
another by multiplying it by itself.  Now you need a function which 
transforms a list of of numbers to another list of numbers by squaring 
each member.  You could write it like this:

    function squareAll(nbrs) {
      return map(square, nbrs)
    }

Or with the simplified arrow function syntax of ES2015:

    const squareAll = nbrs => map(square, nbrs)

A points-free version would look like this:

    const squareAll = map(square)

In the language Haskell, where function application is delineated not 
with parentheses but with space separation, and where declaration 
keywords like `const` are unnecessary, this would be written as

    squareAll = map square

That's extremely elegant; every token used adds meaning to the definition.

But the points-free Javascript is not too far off, adding only `const`, `
(`, and `)`.  If we look at the arrow-function version, we add a number 
of additional tokens on top of those: `nbrs`, `=>`, `,`, and `nbrs`, and 
the other version, while removing `const`, adds `function`, `(`, `nbrs`, 
`)`, `{`, `return`, `,`, `nbrs`, and `}`.  (Note that the "points" in 
"points-free" does not refer to all these tokens, but only to the uses of 
`nbr` in this case.)  

The main idea is that this points-free definition can often make it very 
clear what is going on without the distraction of names.  Was `nbrs` the 
right choice?  Should it be `list`?  `array`?  `nbrList`?  It is about 
how functions act on your data, not on the data itself, and especially 
not on what you choose to name that data in a particular instance.

This is not universally helpful, though.  As functions get more complex, 
using arrow functions for parts of your definitions may make them easier 
to understand.  There is no universal prescription.  Points-free coding 
is a useful tool to have in your toolbox.  The only thing it will grant 
you is additional readability.  If it's not doing that, then don't use it.


> In addition, I'm dying to take a look at some FP JavaScript code that
> effectively solves some REAL world problem. To me, this type of learning
> is most impactful.

I would suggest looking through some real-world projects that use such 
libraries then.  The NPM page for Ramda [4] has a list of other projects 
that are dependent upon it [5].  With 1800+ projects to look at, 
something there might interest you.  (Lodash has 20 times as many 
dependents, BTW, but lodash is mostly used in more imperative settings, 
so finding the places where its used in its functional guise might be 
more tricky.)  You might also look to the dependents for Sanctuary [6], 
which is related to Ramda, but takes a more principled stand.  However 
there are only a handful of such dependencies there.



  [1]: <https://lodash.com/#download-links>
  [2]: <http://ramdajs.com/#installation>
  [3]: <https://wzrd.in/>
  [4]: <https://www.npmjs.com/package/ramda>
  [5]: <https://www.npmjs.com/browse/depended/ramda>
  [6]: <https://www.npmjs.com/browse/depended/sanctuary>

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Two quick question regarding FP (Functional Programming) justaguy <lichunshen84@gmail.com> - 2017-01-20 19:00 -0800
  Re: Two quick question regarding FP (Functional Programming) Scott Sauyet <scott@sauyet.com> - 2017-01-21 19:16 +0000
    Re: Two quick question regarding FP (Functional Programming) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-01-21 14:43 -0800
      Re: Two quick question regarding FP (Functional Programming) justaguy <lichunshen84@gmail.com> - 2017-01-21 18:04 -0800
        Re: Two quick question regarding FP (Functional Programming) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-01-21 22:06 -0800
          Re: Two quick question regarding FP (Functional Programming) justaguy <lichunshen84@gmail.com> - 2017-01-22 06:32 -0800
            Re: Two quick question regarding FP (Functional Programming) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-01-22 11:58 -0800
          Re: Two quick question regarding FP (Functional Programming) "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-01-22 22:31 +0100
            Re: Two quick question regarding FP (Functional Programming) Scott Sauyet <scott.sauyet@gmail.com> - 2017-01-25 18:22 -0800
              Re: Two quick question regarding FP (Functional Programming) "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-01-26 12:19 +0100
                Re: Two quick question regarding FP (Functional Programming) Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-01-26 12:51 +0100
    Re: Two quick question regarding FP (Functional Programming) justaguy <lichunshen84@gmail.com> - 2017-01-21 18:03 -0800
      Re: Two quick question regarding FP (Functional Programming) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-01-22 11:06 -0800

csiph-web