Lädt...


🔧 Introduction to Functional Programming in JavaScript: Monoids, Applicatives, and Lenses #8


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Functional programming offers a rich set of tools and patterns that can help you write more expressive, modular, and maintainable code. Among these tools are monoids, applicatives, and lenses. These advanced concepts can initially seem daunting, but they provide powerful abstractions for dealing with data and computations.

Monoids

A monoid is a type with a binary associative operation and an identity element. This might sound abstract, but many common data types and operations form monoids.

Monoid Properties
  1. Associativity: ( (a \cdot b) \cdot c = a \cdot (b \cdot c) )
  2. Identity Element: There exists an element ( e ) such that ( a \cdot e = e \cdot a = a )
Example: String Concatenation
const concat = (a, b) => a + b;
const identity = '';

console.log(concat('Hello, ', 'World!')); // 'Hello, World!'
console.log(concat(identity, 'Hello')); // 'Hello'
console.log(concat('World', identity)); // 'World'

String concatenation with an empty string as the identity element is a monoid.

Example: Array Concatenation
const concat = (a, b) => a.concat(b);
const identity = [];

console.log(concat([1, 2], [3, 4])); // [1, 2, 3, 4]
console.log(concat(identity, [1, 2])); // [1, 2]
console.log(concat([1, 2], identity)); // [1, 2]

Array concatenation with an empty array as the identity element is also a monoid.

Applicatives

Applicatives are a type of functor that allow for function application lifted over a computational context. They provide a way to apply functions to values that are wrapped in a context, such as Maybe, Promise, or arrays.

Applicative Properties
  1. Identity: ( A.of(x).map(f) \equiv A.of(f).ap(A.of(x)) )
  2. Homomorphism: ( A.of(f).ap(A.of(x)) \equiv A.of(f(x)) )
  3. Interchange: ( A.of(f).ap(u) \equiv u.ap(A.of(f => f(x))) )
Example: Applying Functions with Applicatives
class Maybe {
  constructor(value) {
    this.value = value;
  }

  static of(value) {
    return new Maybe(value);
  }

  map(fn) {
    return this.value === null || this.value === undefined
      ? Maybe.of(null)
      : Maybe.of(fn(this.value));
  }

  ap(maybe) {
    return maybe.map(this.value);
  }
}

const add = a => b => a + b;

const maybeAdd = Maybe.of(add);
const maybeTwo = Maybe.of(2);
const maybeThree = Maybe.of(3);

const result = maybeAdd.ap(maybeTwo).ap(maybeThree);
console.log(result); // Maybe { value: 5 }

In this example, the ap method is used to apply the function inside the Maybe context to the values inside other Maybe instances.

Lenses

Lenses are a functional programming technique for focusing on and manipulating parts of data structures. They provide a way to get and set values in immutable data structures.

Basic Lens Implementation

A lens is typically defined by two functions: a getter and a setter.

const lens = (getter, setter) => ({
  get: obj => getter(obj),
  set: (val, obj) => setter(val, obj)
});

const prop = key => lens(
  obj => obj[key],
  (val, obj) => ({ ...obj, [key]: val })
);

const user = { name: 'Alice', age: 30 };

const nameLens = prop('name');

const userName = nameLens.get(user);
console.log(userName); // 'Alice'

const updatedUser = nameLens.set('Bob', user);
console.log(updatedUser); // { name: 'Bob', age: 30 }

In this example, prop creates a lens that focuses on a property of an object. The lens allows you to get and set the value of that property in an immutable way.

Combining Lenses

Lenses can be composed to focus on nested data structures.

const addressLens = prop('address');
const cityLens = lens(
  obj => obj.city,
  (val, obj) => ({ ...obj, city: val })
);

const userAddressCityLens = {
  get: obj => cityLens.get(addressLens.get(obj)),
  set: (val, obj) => addressLens.set(cityLens.set(val, addressLens.get(obj)), obj)
};

const user = {
  name: 'Alice',
  address: {
    city: 'Wonderland',
    zip: '12345'
  }
};

const userCity = userAddressCityLens.get(user);
console.log(userCity); // 'Wonderland'

const updatedUser = userAddressCityLens.set('Oz', user);
console.log(updatedUser); // { name: 'Alice', address: { city: 'Oz', zip: '12345' } }

By composing lenses, you can focus on and manipulate nested properties in complex data structures.

Monoids, applicatives, and lenses are advanced functional programming patterns that enable you to write more expressive and maintainable JavaScript code. Monoids provide a way to combine values in a structured manner, applicatives allow for function application within a context, and lenses offer a powerful way to access and update immutable data structures.

By incorporating these patterns into your programming toolkit, you can handle complex data transformations, manage side effects, and maintain immutability in your applications.

...

🔧 Introduction to Functional Programming in JavaScript: Monoids, Applicatives, and Lenses #8


📈 135.17 Punkte
🔧 Programmierung

🔧 Introduction to Functional Programming in JavaScript: Applicatives #10


📈 72.74 Punkte
🔧 Programmierung

🔧 Introduction to Functional Programming in JavaScript: Lenses #9


📈 60.38 Punkte
🔧 Programmierung

🔧 Introduction to Functional Programming in JavaScript: Monad and functors #7


📈 40.66 Punkte
🔧 Programmierung

🔧 Introduction to Functional Programming in JavaScript #1


📈 39.04 Punkte
🔧 Programmierung

🔧 Introduction to Functional Programming in JavaScript: Closure #2


📈 39.04 Punkte
🔧 Programmierung

🔧 Introduction to Functional Programming in JavaScript: High order functions #3


📈 39.04 Punkte
🔧 Programmierung

🔧 Introduction to Functional Programming in JavaScript: Function compositions #4


📈 39.04 Punkte
🔧 Programmierung

🔧 Introduction to Functional Programming in JavaScript: Partial functions #5


📈 39.04 Punkte
🔧 Programmierung

🔧 Introduction to Functional Programming in JavaScript: Immutability #6


📈 39.04 Punkte
🔧 Programmierung

🔧 Introduction to Functional Programming in JavaScript: Different monads #11


📈 39.04 Punkte
🔧 Programmierung

🔧 Introduction to Functional Programming in JavaScript: Do monads #12


📈 39.04 Punkte
🔧 Programmierung

📰 Programming Pioneer Grady Booch on Functional Programming, Web3, and Conscious Machines


📈 33.88 Punkte
📰 IT Security Nachrichten

🔧 Applicatives in typescript


📈 33.7 Punkte
🔧 Programmierung

🔧 An Introduction to Functional Programming


📈 32.61 Punkte
🔧 Programmierung

🔧 A Small Introduction to Functional Programming


📈 32.61 Punkte
🔧 Programmierung

🔧 Introduction to Functional Programming in Scala


📈 32.61 Punkte
🔧 Programmierung

🔧 Programming Paradigms in JavaScript: OOP, Functional and Event-Driven


📈 31.38 Punkte
🔧 Programmierung

🔧 Functors, Monads and Promises in Functional Programming with JavaScript


📈 31.38 Punkte
🔧 Programmierung

🔧 Functional Programming in JavaScript: Concepts and Examples


📈 31.38 Punkte
🔧 Programmierung

🔧 Difference between Functional Testing and Non Functional Testing with Examples


📈 30.43 Punkte
🔧 Programmierung

🔧 Difference between Functional and Non Functional Testing


📈 30.43 Punkte
🔧 Programmierung

🔧 FUNCTIONAL AND NON-FUNCTIONAL TESTING


📈 30.43 Punkte
🔧 Programmierung

🔧 Difference-Functional and Non-Functional Testing


📈 30.43 Punkte
🔧 Programmierung

🔧 Functional and Non-Functional Testing


📈 30.43 Punkte
🔧 Programmierung

🔧 Functional and Non-functional testing


📈 30.43 Punkte
🔧 Programmierung

🔧 Functional and Non-Functional Testing


📈 30.43 Punkte
🔧 Programmierung

🔧 Exploring the Depths of Functional and Non-Functional Testing


📈 30.43 Punkte
🔧 Programmierung

🔧 Exploring the Depths of Functional and Non-Functional Testing


📈 30.43 Punkte
🔧 Programmierung

🔧 Difference between Functional Testing and Non-Functional Testing with examples


📈 30.43 Punkte
🔧 Programmierung

🔧 Functional and Non functional


📈 30.43 Punkte
🔧 Programmierung

🔧 What is difference between functional and non functional testing ?


📈 30.43 Punkte
🔧 Programmierung

🔧 Functional and Non functional testing


📈 30.43 Punkte
🔧 Programmierung

🔧 DIFFERENCE BETWEEN FUNCTIONAL AND NON-FUNCTIONAL TESTING


📈 30.43 Punkte
🔧 Programmierung

matomo