Off topic, but .. Good Functional Tutorial?

Ever since I discovered the brilliant architecture of D3, I’ve wanted to look into “functional programming” in JS.

D3 seems the sanest approach to FP and wondered if any of us have a good tutorial for FP that is similar to D3’s.

1 Like

I agree, D3 has some excellent helpful content. I am beginning to learn about Observable, and I’m hoping to get more.

1 Like

Can you elaborate on which parts of D3 you found as a good approach to FP? I agree D3 has an elegant architecture but I wouldn’t consider it as a highly FP oriented library. What @mbostock did consistently in the API is that you have objects which already has useful decorator methods with sane defaults. These decorator methods return the object itself after modifying it, so they are chainable. For instance, you first create a scale or axis object and then customize it to your needs with chained method calls. During customization, sometimes you simply set a value (i.e. setting the domain for a scale), sometimes you specify behavior via a function (i.e. how thresholds in a histogram will be computed). The latter gives the developer a lot of flexibility and power of course. Since first D of D3 is data, whenever we want to set a simple value, we can also use a function to decide that value based on the data (i.e. in force layout link.distance(30), or link.distance( link => link.weightAlreadyComputed) or more complex logic link.distance( link => link.source.isUser ? max(totalCommonFriends(link.source, link.target), 20) : 100).

Are these what you are referring as FP aproach of D3? I’d say these are not specific to JS but just mike’s brilliance using language’s dynamic capabilities and having functions as first class values for his vision of how a visualization library should work.

Neverthless, here are some pointers to FP in javascript:

  • Here is a good series of posts. I couldn’t fİnd a table of contents but each post has a next/previous link at bottom.
  • Check Array.prototype’s higher order methods.
  • Lodash library has some nice FP utility functions. You can check this post on how to use them.
  • Ramdajs is another utility library which is dedicated to FP in javascript.

Agreed … my interest in D3’s style is it’s elegance. Certainly many FP-ish features and is getting me interested in FP. I’m not sure why D3 doesn’t use the newly popular “monorepo” approach, however. 30 repos is quite a lot!

And your links are very useful, thanks!

BTW: Looking into lodash/underscore turned up this interesting link:

You don’t (may not) need Lodash/Underscore

My particular project has one area in which immutability is not practical. I have 3 (or more) large arrays of objects (100K or more) which every step of the program are modified. A new array is not a big issue, but cloning the objects themselves would be a disaster.

But I’m sure changing the approach entirely could reduce this issue. I’ll keep my eyes out for a good approach to FP solutions in this area.

Thanks again.

It sounds like a dataframe approach could be useful for this case. There are a few packages out there that will allow to work with df in javascript (apache arrow @ https://beta.observablehq.com/@theneuralbit/introduction-to-apache-arrow, dataframe-js @ https://www.npmjs.com/package/dataframe-js).
(I haven’t tested them much yet.)

https://purelyfunctional.tv/

This is a terrific site: both wide variety of videos on interesting functional programming topics + excellent education in the Clojure (a Lisp) language.

https://drboolean.gitbooks.io/mostly-adequate-guide-old/content/

I’ve read through that book a couple times, and reference it frequently.