DEV Community

Iven Marquardt
Iven Marquardt

Posted on

4 2

Sorry, but Functors are not a means to map over an Array

...because they are much more general: Functors lift any ordinary function into numerous structures and effectful contexts.

See how we lift the pure function inc = x => x + 1 in a context that either produces an Array asynchronously or yields nothing at all. All it takes is the composition of these three functions:

const tMap = f => tx =>
  Task((res, rej) => tx.task(x => res(f(x)), rej));

const optMap = f => tx =>
  match(tx, {
    None: _ => None,
    Some: ({some: x}) => Some(f(x))
  });

const arrMap = f => xs =>
  xs.map((x, i) => f(x, i));
Enter fullscreen mode Exit fullscreen mode

Consider what it means in terms of code reuse in general if you can reuse all your simple functions in dozens of scenarios.

Read the full story about functors including the complete, runnable code of the given example.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay