Functional Programming - 03: Function Composition

preview_player
Показать описание
In this video, we will look at Function Composition in Functional Programming. At the end of this video we will see a general way to compose Functions.

In this Video:
- Composing Functions
Рекомендации по теме
Комментарии
Автор

This is an amazingly well explained series. I wish I started my Functional Programming journey with this playlist.

bmehder
Автор

Notes for this lecture:
FP is mostly about composing values.
- We can compose values other than functions too.
By simplifying our paradigm we don't lose any information.
- Example : We have two functions increment and toString.
- increment input and output domain is int -> int.
- toString input and output domain is int -> string
- we can compose these two functions without losing any behaviour (increment + toString => increment_then_toString)
We can extract this logic of combining two mappable functions out into a function called compose
- const compose = (f, g) => x => f(g(x))

dprophecyguy
Автор

I have a question. What is the best way to pipe or compose functions together that takes a number as an input and pass that number to each functions to do validation steps? For example, one function tests the input to be greater than 5 and another that tests if the number is even. One function is x => x > 5, and the other is x => x / 2 === 0. You cannot compose those functions together because the output of one function is a boolean and the other function expects an integer as an argument. compose(isEven, isGreaterThanFive)(7). This will not work. I have done solutions like returning an array or object to store both the value and the results and pipe it into the other function, but I need to change the input parameter to accept an array or object, which is hideous, and I hate it. I'm guessing a Monad is appropriate for this case.

PS-dpyg
Автор

Excellent! But you should speak faster in all your videos. Of course, I know we can always increase video speed...

tbad
Автор

import * as R from "ramda";

const toString = (n: number) => n.toString();
const inc = (n: number) => n + 1;
const inc_then_toString = R.compose(toString, inc);

rgao
visit shbcf.ru