Functional Programming - 04: Function Composition demo

preview_player
Показать описание
In this video we look into Function Composition in practice.

In this Video:
- Composing Functions
- Defining a general function 'compose'
Рекомендации по теме
Комментарии
Автор

The quality of your content deserves a lot more views and likes

danielsena
Автор

Instant sub! I'm so glad there's someone doing something like this! Also it's nice to see that you're defining function types separately, wish that community accepted that convention too :D

Lambdaphile
Автор

Really enjoying the series so far, I thought about generics before — they really are a great tool.

Merry Christmas btw :)

DS-bzmz
Автор

Notes and Code examples in this lecture:

type Increment = (x: number) => number
const increment: Increment = x => x + 1

console.log(increment(2))

type ToString = (x: number) => string
const toString: ToString = x => `${x}`

console.log(toString(2))

type IncrementThenToString = (x: number) => string
const incement_then_toString: IncrementThenToString => x => toString(increment(x))

// We will improve the above implementation to create a `compose` function which will take two functions as inputs and will give us a function as output, which will combine the behaviour of both the functions it receives in input.
const compose: Compose = (f, g) => x => f(g(x))
const incement_then_toString2: IncrementThenToString = compose(toString, increment)



Defining type for compose function
// Defining type for compose (Rigid function definition which will only work with numbers)
type Compose = (
f: (x: number) => string,
g: (x: number) => number
) => (x: number) => string

// We can improve the `Compose` type definition by making the inputs flexible as below
type Compose = <A, B, C> (
f: (x: B) => C,
g: (x: A) => B
) => (x: A) => C

// A, B, C here are a feature of typescript called Generics.

dprophecyguy
Автор

Great tutorial, it answered a lot of my questions! By the way, I would also like to ask what tool used to implement the REPL in the video?

kmrkgo
Автор

2:00 could you please explain why this is a good practice? What are the benefits?

ОгурчикКосмический
Автор

i find this now littlw bit confusing. in f# composition would be increment >> toString and here I see (toString, increment) . why is the order reverse? What if we compose 10 functions do we need to reverse order them?

Feronom
Автор

Perhaps it is mostly a matter of the examples that are being used, being things that are generally included in a lot of programming languages by default, but it looks to me like a lot of the stuff that is being done seems like overly-complicated boilerplate.

Having a type definition for each seperate function already seems overkill, but creating a seperate function, as well as a seperate type definition for chaining the Increment and ToString would be completely wasteful and not something that would be preferable over just calling the method the way you do in the increment_then_tostring function itself.

Rapid_Riley
visit shbcf.ru