DEV Community

Discussion on: Practical Functional Programming in JavaScript - Control Flow

Collapse
 
wulymammoth profile image
David

This is very interesting, Richard -- definitely fun exploration.

Your example begins looking very declarative (one of the awesome things about FP), and almost like SQL if one squints.

I've never gone beyond using the native functional facilities in the standard library and I used them quite a bit when I worked with JS, but curious as to whether you're familiar with Lodash's FP module: github.com/lodash/lodash/wiki/FP-G.... It definitely doesn't provide an interface that Rubico provides for handling conditions. I've never dove deep enough into Haskell to know whether what you're doing is an adaptation of an idea there

Collapse
 
richytong profile image
Richard Tong • Edited

rubico is very much a JavaScript library, and was born from my own needs as a JavaScript developer. I have tried my best to preserve the good work and idioms of more traditional fp'ers, but rubico did not come from another language or another set of practices. I would say what I'm doing here with control expressions in rubico more borrows from earlier work trying to abstract the query DSL of elasticsearch. That produced something like this

 * {
 *   $and: {
 *     fieldA: vA, // term
 *     fieldH: { $like: vH }, // match (fuzzy)
 *     fieldB: { $gte: lvB, $lte: uvB }, // range
 *     fieldI: $exists,
 *   },
 *   $or: {
 *     fieldC: [a, b], // terms
 *     path.fieldE: [a, b], // nested terms query
 *     $geo: { lat: 123, lon: 21, distance: '50mi' }, // geo_distance
 *   },
Enter fullscreen mode Exit fullscreen mode

Lodash's FP module

I wanted rubico to be less of a grab bag and focus more on absolutely necessary syntax. You could in some ways consider hasFlag as a new member of the syntax available to solve the problem of a command line interface. Also, I've left an option for a "grab bag" of sorts called rubico/x. Basically if you have a function you like (like lodash defaultsDeep), you could add it into rubico as rubico/x/defaultsDeep. Then you could import it like

const defaultsDeep = require('rubico/x/defaultsDeep')
Enter fullscreen mode Exit fullscreen mode