1
import { chain, map, Apply } from "fp-ts/lib/IO";
import { log } from "fp-ts/lib/Console";
import { pipe } from "fp-ts/lib/function";
import * as D from "fp-ts/lib/Date";
import { sequenceT } from "fp-ts/lib/Apply";
import { v4 as uuidv4 } from "uuid";

export const Article = (title: string) =>
  ([id, now]: [string, Date]) => ({
    id,
    title,
    createdAt: now
  });

const createArticle = (title: string) =>
  pipe(sequenceT(Apply)(uuidv4, D.create), map(Article(title)));

const program = pipe(
  createArticle("hello"),
  chain(log)
);

program();

In the example above since Article requires 2 params that are side-effects. The question is on createArticle and if that could be written as a point free function.

VLAZ
  • 26,331
  • 9
  • 49
  • 67

1 Answers1

1

You can do the piping in reverse by applying the values to the function using ap. I had to create a function that helps TypeScript figure out the type of of.

import { chain, map, Apply, of, ap } from "fp-ts/lib/IO";
import { log } from "fp-ts/lib/Console";
import { flow, pipe } from "fp-ts/lib/function";
import * as D from "fp-ts/lib/Date";
import { sequenceT } from "fp-ts/lib/Apply";
import { v4 as uuidv4 } from "uuid";

const Article =
  (title: string) =>
  ([id, now]: [string, Date]) => ({
    id,
    title,
    createdAt: now,
  });

const ofString = (str: string) => of(str);

const createArticle = flow(
  ofString,
  map(Article),
  ap(sequenceT(Apply)(uuidv4, D.create))
);

const program = pipe(createArticle("hello"), chain(log));

program();
Caleb Weeks
  • 182
  • 1
  • 1
  • 12