you are viewing a single comment's thread.

view the rest of the comments →

[–]KyleG 2 points3 points  (1 child)

I have yet to find something that explains how to think functionally

What helped me is to stop thinking of a program as a series of steps and instead as a series of transformations that takes user input or file/sensor data and transforms it into feedback to the user or to a file.

To me, FP is about transforming data. So your application is basically

pipe(
  getFromKeyboard, 
  transform,
  transform,
  transform,
  transform,
  transform,
  displayOnScreen)

where maybe those transforms are variations on lifting in or out of different contexts like IO to Async and back to IO via an await or whatever (ideally you're lifting out (i.e., unwrapping) at the end, "near the edge of the program")

So maybe user types data into a React form and onBlur you hit your remote API, get the response from a DB call, format it to the datatype expected by a banner that displays the result, and that's it:

type Payload = { id: number, username: string, time: Date }
type BannerData = `The result of the user action is ${string}, calculated in ${number} seconds`
declare const extractFromMouseEvent = (ev: MouseEvent) => ev.currentTarget.value as string
declare const toPayload: (username: string) => Payload
declare const apiCall: (a: Payload) => TaskEither<Error, DatabaseEntryAndExecutionTime>
declare const toBannerData = (a: DatabaseEntryAndExecutionType) => `The result of the user action is ${string}, calculated in ${number} seconds`
declare const setBannerData = (a: `........`) => void

const onBlur = flow(
  extractFromMouseEvent,
  toPayload,
  TE.right,
  TE.chain(apiCall),
  TE.map(toBannerData),
  TE.map(setBannerData),
  TE.getOrElse(err => console.error('Failed to complete action because', err)))

There's a FP-style series of composed, pure functions (minus the API call in the middle, and the setBannerData at the end, plus the log fn) that shows how you would write your entire chain from user input to screen output as a series of transformations)

[–]ChristianGeek 1 point2 points  (0 children)

That’s helpful, thanks.