Is it possible to use the Do/Bind pattern in fp-ts to construct an object with multiple types?

Iā€™m in search of a method similar to Record.Do with Record.bind so I can perform actions like the following:

function getB: (a:A) => B
function getC: (b: B) => C
function getD: (c: C) => D

type Z = {
  a: A,
  b: B,
  c: C,
  d: D,
}

const getZ = (a: A): Z => pipe(
  R.Do,
  R.bind('a', () => a),
  R.bind('b', () => getB(a)),
  R.bind('c', (bindings) => getC(bindings.b)),
  R.bind('d', (bindings) => getD(bindings.c)),
)

The objective is to construct an object containing diverse types while preserving all the inner objects of different types prior to applying certain transformations on them.

I am uncertain about how to achieve this. My intention is not to transport my types to other realms such as Option, Either, IO; as this would involve more code utilizing O.some(s), E.right(s), or IO.of(s) for non-error transformations.

This is the closest approach I could devise:

const getZ = (a: A): Z => pipe(
  IO.Do,
  IO.bind('a', () => () => a),
  IO.bind('b', () => () => getB(a)),
  IO.bind('c', (bindings) => () => getC(bindings.b)),
  IO.bind('d', (bindings) => () => getD(bindings.c)),
)()

Answer ā„–1

Have you considered utilizing the do notation with the Identity module? This particular type is free from any effects, making it suitable for achieving your desired outcome: https://example.com/identity/do-notation

Answer ā„–2

One efficient method to accomplish this task is as follows:

const findZ = (inputA: A): Z => {
   const tempB = retrieveB(inputA)
   const tempC = locateC(tempB)
   const tempD = fetchD(tempC)
   return ({inputA, tempB, tempC, tempD})
}

Utilizing more of the fp-ts library functionality, but still somewhat reliant on imperatives?

const findZ = (inputA: A): Z => pipe(
  inputA,
  (a) => ({ b: retrieveB(a), a}),
  (connections) => ({ c: locateC(connections.b), ...connections })
  (connections) => ({ d: fetchD(connections.c), ...connections })
)

Open to further recommendations

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The function that iterates through the 'categoria' state and returns a new array is not functioning properly

Having difficulty with the object of a function using .map(). It works when the code is used directly, but not when put inside a function. For example: if(this.state.cat){ return _.map(this.state.cat, categoria => { if(this.state.search_ ...

Change Json data into a TypeScript class [ts]

I have the following JSON data: {"mapData":{"test":"success","publicKey":""},"statusCode":200,"message":null} How can I convert this JSON to a TypeScript class? The mapData contains anonymous values: It may be {"test":"success","publicKey":""} Or it m ...

Guide on how to include jquery-ui css file in Vue3

I am new to TypeScript and VueJs. Recently, I decided to incorporate jquery-ui into my project using Vue3, TypeScript, and Electron. After some trial and error, I was able to successfully set up the environment. However, when I attempted to use the jquery ...

Difficulty with Iterating through Items in Angular HTML with *ngFor

Having an issue with *ngFor. It seems like the index in my tab object "joueurClassement" is not in the correct number sequence. The picture below illustrates this problem, where it works fine when the indexes are 0,1,2,3,4. In my HTML => <tbody ...

Utilize Type Script/Angular 8 for seamless file management by either dragging and dropping files or selecting

Can you provide guidance on using the ng-upload-file plugin with TypeScript? https://github.com/danialfarid/ng-file-upload I attempted to implement it but encountered difficulties. Do you have a working sample available or know of another open-source plu ...

Confusing directions about parentheses for "this.fn.bind(this)(super.fn(...args)"

While reviewing a project, I came across code that can be simplified to: export abstract class Logger { private static log(level: LogLevels, ...args: Array<any>) {/**/} public error(...args: Array<any>): LogData { return Logger ...

Receiving real-time updates from an Angular 2 service

Having an issue with displaying user nicknames in Angular 2 using the User ID. When attempting to call the getUserName(userId) function from dashboard.component.html, which then triggers the auth0 service to retrieve the user profile, I am encountering con ...

Retrieve the array from the response instead of the object

I need to retrieve specific items from my database and then display them in a table. Below is the SQL query I am using: public async getAliasesListByDomain(req: Request, res: Response): Promise<void> { const { domain } = req.params; const a ...

What is the reason behind TypeScript requiring me to initialize a property even though I am retrieving its value from a local reference?

I am just beginning to explore Angular. This is the template for my custom component: <div class="row"> <div class="col-xs-12"> <form action=""> <div class="ro"> <d ...

What is the best way to showcase the information retrieved from my API?

I am attempting to display the ID and Document number that are retrieved from an array. Data Returned However, I am not seeing any results in return. You can view the application results here. I have tried using string interpolation like {{document.id}} ...

Is it possible to directly declare multiple variables within an array in TypeScript?

I am looking to simplify my variable declaration process for an array in my Angular 8 project using TypeScript. Currently, my code looks like this: export class GridComponent { pizza0: Pizza; pizza1: Pizza; pizza2: Pizza; pizza3: Pizza; pizza ...

How can one trigger a service method in nestjs through a command?

I am looking to run a service method without relying on API REST - I need to be able to execute it with just one command ...

Encountering the TS1005 error in Angular 4.4.7 with Typescript 2.7.2: Missing semi-colon ';' issue

I am encountering numerous errors related to lib.es6.d.ts while trying to build my project. node_modules/typescript/lib/lib.es6.d.ts(20605,14): error TS1005: ';' expected. https://i.sstatic.net/weh53.png tsconfig.json { "compilerOption ...

What is the method to retrieve the data type of an array in TypeScript?

I am currently working on a TypeScript function (still getting acquainted with TS) that accepts a parameter which could be either a number, a string, an array of numbers, or an array of strings. It is crucial for me to distinguish between these 4 types wi ...

Is ts-node necessary for using TypeScript in Node.js?

Having trouble setting up a Node.js application in Visual Studio Code using TypeScript due to issues with importing modules. Initially, I created an index.ts file with the import statement -> import config from './app/config.json'; This resu ...

Leveraging interfaces with the logical OR operator

Imagine a scenario where we have a slider component with an Input that can accept either Products or Teasers. public productsWithTeasers: (Product | Teaser)[]; When attempting to iterate through this array, an error is thrown in VS Code. <div *ngFor= ...

Exploring the Possibilities with Material UI v4: Expanding on PaletteColor

Looking to make a simple adjustment in Material UI v4's (v4.11) palette, specifically adding a new field to PaletteColorOptions like darker?: string. The type definition can be found in the relevant Material UI module: createPalette.d.ts export type ...

Using the TypeScript NextPage function along with the getInitialProps static method and @typescript-eslint/unbound-method

After enabling typescript-eslint with its recommended settings, I encountered an issue in my code. I came across this helpful post on Stack Overflow: Using getInitialProps in Next.js with TypeScript const X: NextPage = props => {/*...*/} X.getInitialP ...

Leveraging process.env with TypeScript

Is there a way to access node environment variables in TypeScript? Whenever I try using process.env.NODE_ENV, I encounter the following error : Property 'NODE_ENV' does not exist on type 'ProcessEnv' I even tried installing @types/no ...

What causes an empty array to appear outside of a FOR loop in Typescript (Angular CLI)?

After thorough research on similar issues, I couldn't find a solution to my problem. Hence, I am posting this question again to seek clarification on why this issue is occurring. I have developed a web application where users can download data by cli ...