1.2.36 • Published 5 months ago

@sullux/fp-light v1.2.36

Weekly downloads
810
License
MIT
Repository
github
Last release
5 months ago

Home | Full API | Tutorial | Project Setup Guide (CJS / MJS)

fp-light

This is a functional programming library with a focus on point-free coding style and abstracting the complexity of asynchronous programming. This library has no dependencies, so adding it to your project does not introduce any bloat.

In This README:

Other Documentation:

Installation

npm i -P @sullux/fp-light
# or
yarn add @sullux/fp-light

Basic Usage

There are three schools of thought for this library. Some will see it as a collection of helper functions and will prefer to require/import them one by one as needed, along the lines of:

const { pipe, when } = require('@sullux/fp-light')
// or
import { pipe, when } from '@sullux/fp-light'

const download = pipe(
  when(foo, bar),
  // etc.
)

The upside to this is that the helper functions are directly usable in code with no prefix and each code file is directly portable to other projects. The downside is that in practice this adds a heavy development and maintenance burden.

The second school of thought still holds that these are helper functions, but prefers to eliminate the maintenance burden in favor of a readability cost. This is our least favorite option, but it may indeed work best for some people:

const fp = require('@sullux/fp-light')
// or
import * as fp from '@sullux/fp-light'

const download = fp.pipe(
  fp.when(foo, bar),
  // etc.
)

The upsides to this are that each file is still directly portable to other projects and there is less development and maintenance burden. The downside is readability, as prefixes are a longstanding cause of readability problems in code. If the with statement was not deprecated, combining this pattern with with(fp) { ... } might make this the best of all three patterns; unfortunately, with is deprecated and we're stuck with prefixes.

The third school of thought is our preferred approach: we hold that the functions in this library are more like language elements than helper functions, and as with Javascript language elements, they should be global:

// in index.js:
const fp = require('@sullux/fp-light')
// or
import * as fp from '@sullux/fp-light'
// make them global:
Object.assign(globalThis, fp)

// anywhere in this file or others:
const download = pipe(
  when(foo, bar),
  // etc.
)

This is another way to accomplish Option 1, but shifting the maintenance burden to the project level rather than the file level. To be clear, there is still some setup to be done, such as:

  • adding the FP language elements to the linter as globals
  • creating a workaround for the test runner since it does not usually enter the app in index.js
  • adding documentation so that subsequent development and maintenance personnel are not confused

We have created this guide to help with this setup process:

Guide To Globalizing The FP Language Elements

Regarding complete documentation, the full API includes all exports from this library. There are a lot of them. Organizing them into something other than an alphabetical encyclopedia of exports is on our to-do list, but for now, that's what we have.

Important Concepts

The single most important concept for understanding this library is to understand that most of these functions do not do what they say; instead, they compile a new function that will do what they say when called. Here is a simple example you can run in the REPL (assuming you have the FP library installed locally):

Object.assign(globalThis, require('@sullux/fp-light'))
const assert = require('assert')
const answer = add(1, 2)
assert(answer !== 3)
assert((typeof answer) === 'function')
const actualAnswer = answer()
assert(actualAnswer === 3)

Note that add(1, 2) does NOT return a number; it returns a function. When a function behaves like this here in FP Light Land we refer to it as a compilable function. Maybe that's not the best name, but that's what we've got: the add function is a compilable function.

The big question on your mind at this point might be...why???

To answer that, let's look at the pipe function. Pipe is one of the most common utility functions and can be found in almost all language productivity libraries (functional or otherwise) including lodash, Ramda, etc. Here is an example of a simple pipe as it might work in any of these libraries (we've skipped some imports here for brevity):

const s3ToLocal = pipe(
  (key) => ({ Bucket: MY_BUCKET, Key: key }),
  s3.getObject,
  (result) => result.then(({ Body, ETag }) => saveFile(ETag, Body)),
)

Notice that the result of the pipe function is another function. In a sense, all pipe does is use each step we've gien it to compile the actual s3ToLocal function. The pipe function is compilable! It compiles the actual s3ToLocal function!

Now lets look at that same function, but using the FP Light library the way it is intended to be used:

const s3ToLocal = pipe(
  { Bucket: MY_BUCKET, Key: _ },
  s3.getObject, // not compilable: we're handing the function to the pipe
  writeFile(_.ETag, _.Body), // compilable: we're handing back a new function!
)

We've already seen why pipe is compilable: it compiles a number of steps into a single function call. In the above example, consider the writeFile at the end. Saving the file is supposed to be the last step in the pipe, so we can't actually call it there on line 4. Instead, that call compiles a final function, and then that final function is being passed to the pipe. Also note that s3.getObject is not being called here because it is not compilable! It is just a plain old function and must be passed to the pipe verbatim.

Here's the fully-working example with all the appropriate imports, and we've even gone one better and made s3.getObject compilable too:

const { writeFile: writeFileLegacy } = require('node:fs/promises')
const { s3 } = require('@sullux/aws-sdk')()
const MY_BUCKET = 'foo'

const writeFile = compilable(writeFileLegacy, { count: 2 })
const getObject = compilable(s3.getObject, { count: 1 })

const s3ToLocal = pipe(
  getObject({ Bucket: MY_BUCKET, Key: _ }),
  writeFile(_.ETag, _.Body),
)

// usage: s3ToLocal('myCoolKey')

module.exports = { s3ToLocal }

This example is fully-working. Using your own bucket and key on your own AWS account you should be able to run this code yourself. The use of the underscore to represent the incoming argument was borrowed from Scala, and is discussed in more detail under its proper name identity.

If you are interested, continue the journey in the full tutorial.

Development

Please use your best judgement. We all know how this works. If you submit a PR, please make sure the build succeeds and the tests pass. Please ensure that your changes are properly unit tested where relevant and please include updated documentation as necessary.

1.2.36

5 months ago

1.2.34

5 months ago

1.2.35

5 months ago

1.2.32

2 years ago

1.2.33

2 years ago

1.2.29

2 years ago

1.2.30

2 years ago

1.2.31

2 years ago

1.2.27

2 years ago

1.2.28

2 years ago

1.2.25

2 years ago

1.2.26

2 years ago

1.2.24

3 years ago

1.2.23

3 years ago

1.2.22

3 years ago

1.2.20

3 years ago

1.2.21

3 years ago

1.2.18

3 years ago

1.2.19

3 years ago

1.2.16

3 years ago

1.2.17

3 years ago

1.2.15

3 years ago

1.2.14

3 years ago

1.2.8

3 years ago

1.2.12

3 years ago

1.2.13

3 years ago

1.2.10

3 years ago

1.2.11

3 years ago

1.2.9

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.43

4 years ago

1.0.42

4 years ago

1.0.40

4 years ago

1.0.41

4 years ago

1.0.39

4 years ago

1.0.38

4 years ago

1.0.37

4 years ago

1.0.36

4 years ago

1.0.33

4 years ago

1.0.35

4 years ago

1.0.34

4 years ago

1.0.32

4 years ago

1.0.31

4 years ago

1.0.30

4 years ago

1.0.0-beta.29

4 years ago

1.0.0-beta.28

4 years ago

1.0.0-beta.27

4 years ago

1.0.0-beta.26

4 years ago

1.0.0-beta.24

4 years ago

1.0.0-beta.25

4 years ago

1.0.0-beta.23

4 years ago

1.0.0-beta.22

4 years ago

1.0.0-beta.21

4 years ago

1.0.0-beta.20

4 years ago

1.0.0-beta.19

4 years ago

1.0.0-beta.18

4 years ago

1.0.0-beta.17

4 years ago

1.0.0-beta.16

4 years ago

1.0.0-beta.15

4 years ago

1.0.0-beta.11

4 years ago

1.0.0-beta.12

4 years ago

1.0.0-beta.13

4 years ago

1.0.0-beta.14

4 years ago

1.0.0-beta.10

4 years ago

1.0.0-beta.9

4 years ago

1.0.0-beta.6

4 years ago

1.0.0-beta.7

4 years ago

1.0.0-beta.8

4 years ago

1.0.0-beta.3

4 years ago

1.0.0-beta.4

4 years ago

1.0.0-beta.5

4 years ago

1.0.0-beta.2

4 years ago

1.0.0-beta.1

4 years ago

1.0.0-beta.0

4 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago