-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrying.js
More file actions
68 lines (55 loc) · 1.7 KB
/
currying.js
File metadata and controls
68 lines (55 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//TODO Ejemplo1 Composicion
/*
const suma = a=> b => a+b;
const suma1 = suma(2);
suma1(5)
console.log(suma1(9));
*/
//TODO Ejemplo 2 Composicion de como hacerlo y le haremos refactor
/*
const users = [
{ edad: 17, nombre: 'Nicolas', apellido: 'Soto'},
{ edad: 18, nombre: 'Chachito', apellido: 'Feliz'},
{ edad: 12, nombre: 'Loreto', apellido: 'Fernandez'},
{ edad: 1, nombre: 'Lucia', apellido: 'Barrientos'}
]
const traePrimerInfante = data => {
const infante = data.filter(x=>x.edad < 2)
const traePrimerInfante = infante[0]
const infante = {
nombreCompleto: `${primerInfante.nombre} ${primerInfante.apellido}`,
edad: primerInfante.edad
}
return `${infante.nombreCompleto} tiene ${infate.edad} año(s)`
}
*/
// TODO Realizando el refactor dividi y venceras
const users = [
{ edad: 17, nombre: 'Nicolas', apellido: 'Soto'},
{ edad: 18, nombre: 'Chachito', apellido: 'Feliz'},
{ edad: 12, nombre: 'Loreto', apellido: 'Fernandez'},
{ edad: 1, nombre: 'Lucia', apellido: 'Barrientos'}
]
const compose =(...fns) => x =>fns.reduceRight((y,f)=> f(y),x)
const pipe =(...fns) => x =>fns.reduceRight((y,f)=> f(y),x)
const trace = x => y => console.log(x,y);
const head = xs =xs[0]
const formateo = x => ({
nombreCompleto: `${x.nombre} ${x.apellido}`,
edad: x.edad,
})
const formato = x => {`${x.nombreCompleto} tiene ${x.edad} año(s)`}
const filter = f=> xs => xs.filter(f)
const traePrimerInfante = data => compose(
formato,
formateo,
head,
filter(data.filter(x=>x.edad <2)),
)
const traePrimerInfante = data => pipe(
filter(data.filter(x=>x.edad <2)),
trace('Despues del filter'),
head,
formateo,
formato,
)