@HenarMendiola
@HenarMendiola
Composición
Recursividad
Mónadas
Funtores
Efectos Secundarios
Inmutabilidad
@HenarMendiola
@HenarMendiola
@HenarMendiola
¿Cómo?
¿Qué?
@HenarMendiola
¿Cómo?
@HenarMendiola
¿Qué?
@HenarMendiola
@HenarMendiola
let pi=3,1416
let getLength=radius=>{
return 2*pi*radius;
}
let newPi=()=>{
pi=acos(-1,0);
}
let getLength=radius=>{
return 2*acos(-1,0)*radius;
}
Pura
Impura
@HenarMendiola
let getLength=radius=>{
return 2*acos(-1,0)*radius;
}
const lengthOne = getLength(5) //lengthOne = 31,4159265;
const lengthTwo = getLength(5) //lengthTwo = 31,4159265;
const lengthThree = getLength(5) //lengthThree = 31,4159265;
const lengthFour = getLength(5) //lengthFour = 31,4159265;
@HenarMendiola
const array = [3, 6, 4, 5, 8]
array.push(9)
console.log(array) //[3, 6, 4, 5, 8, 9]
@HenarMendiola
const array = [3, 6, 4, 5, 8];
const newArray = array.concat(9);
console.log(array); //[3, 6, 4, 5, 8]
console.log(newArray); //[3, 6, 4, 5, 8, 9]
@HenarMendiola
@HenarMendiola
@HenarMendiola
@HenarMendiola
@HenarMendiola
data
[{title, img, description}]
planet
server
title
description
img
DOM
@HenarMendiola
import {
getDOMElement,
getData,
processRes,
} from './src/intentions.js';
//impure functions
const addListenerToElement = (element, f) => {
element.addEventListener("click", () => f(document));
}
const buttonListenerFunction = (dom) => {
const res = R.compose(getData, getDOMElement)(dom, '#planet', 'value');
res().then(res => res.json().then(data => processRes(data)));
}
@HenarMendiola
data
[{title, img, description}]
planet
server
title
description
img
DOM
const getDOMelement = (selector, prop) => prop ?
document.querySelector(selector)[prop] : document.querySelector(selector);
const getData = planet => fetch(url+planet);
const getCollection = R.prop('collection');
const getItems = R.prop('items');
@HenarMendiola
const processItem = item => ({
title: item.data[0].title,
img: getImageFromItem(item.links).getValue(),
description: item.data[0].description
});
const generateCardTemplate = (styles) => (info) => {
return html`<div style=${styleMap(styles.card)}>
<h2>${info.title}</h2>
<div style=${styleMap(styles.info)}>
<img style=${styleMap(styles.img)} src=${info.img}>
<p>${info.description}</p>
</div>
</div>`
}
@HenarMendiola
@HenarMendiola
data
[{title, img, description}]
planet
server
title
description
img
DOM
data
collection
items
@HenarMendiola
getCollection
getItems
data
items
@HenarMendiola
data
items
@HenarMendiola
compose(getItems,getCollection)
const getHrefFromElement0 = R.compose(R.prop('href'), R.prop('0'));
@HenarMendiola
item = [
{
href: "imagesource",
...
},
...
]
"imagesource",
{
href: "imagesource",
...
}
R.prop('0')
R.prop('href')
const getHrefFromElement0 = R.compose(R.prop('href'), R.prop('0'));
@HenarMendiola
item = [
{
href: "imagesource",
...
},
...
]
"imagesource",
const getHrefFromElement0 = R.compose(R.prop('href'), R.prop('0'));
@HenarMendiola
item = [
{
href: "imagesource",
...
},
...
]
"imagesource",
getHrefFromElement0
const processInfo = R.compose(R.map(processItem), getItems, getCollection );
@HenarMendiola
const res = {
collection: {
items: [
{...},
{...},
{...}
]
}
}
{
items: [
{...},
{...},
{...}
]
}
[
{...},
{...},
{...}
];
[
{title, img, description},
{title, img, description},
{title, img, description}
];
getCollection
getItems
R.map(processItem)
const processInfo = R.compose(R.map(processItem), getItems, getCollection );
@HenarMendiola
const res = {
collection: {
items: [
{...},
{...},
{...}
]
}
}
[
{title, img, description},
{title, img, description},
{title, img, description}
];
processInfo
LA FUNCIÓN
@HenarMendiola
const processRes = R.compose(
renderToSearchContainer,
R.map(generateCardTemplate(styles)),
processInfo
);
@HenarMendiola
respuesta
información procesada
card-templates
card-templates
DOM
processInfo
generateCardTemplate
renderToSearchContainer
const processRes = R.compose(
renderToSearchContainer,
R.map(generateCardTemplate(styles)),
processInfo
);
@HenarMendiola
respuesta
card-templates
DOM
processRes
@HenarMendiola
@HenarMendiola
class Maybe {
constructor(value){
this._value = value;
}
...
};
@HenarMendiola
class Maybe {
...
static Just(value){
return new Maybe(value);
}
static Nothing(){
return new Maybe();
}
};
@HenarMendiola
class Maybe {
...
map(f){
return this.isNothing() ? Maybe.Nothing() : Maybe.Just(f(this._value));
}
getValue(){
return this.isNothing() ? './icons/txomski.jpg' : this._value;
}
};
@HenarMendiola
@HenarMendiola
@HenarMendiola
const a = 7;
function changeA(){
a = 8;
}
@HenarMendiola
@HenarMendiola
const getDOMElement =
(dom, selector, prop) =>
prop ? dom.querySelector(selector)[prop] : dom.querySelector(selector);
@HenarMendiola
const getDOMElement =
(selector, prop) =>
prop ? document.querySelector(selector)[prop] : document.querySelector(selector);
Antes
Después
const fakeDOM = (nodes) => ({
nodes,
querySelector: (node) => nodes[node],
})
it('getDOMElement should return the element from the DOM if it exits', () => {
const DOM = fakeDOM({ input: { value: 'pluto' }});
const inputValue = getDOMElement(DOM, 'input', 'value');
assert.equal(inputValue, 'pluto')
});
@HenarMendiola
const getData = planet => () => fetch(url+planet);
@HenarMendiola
Antes
Después
const getData = planet => fetch(url+planet);
it('getData returns a function with no name and no arguments', () =>{
const planet = 'earth';
const fetchFunc = getData(planet);
assert.equal(typeof fetchFunc, 'function');
assert.equal(fetchFunc.name, '');
assert.equal(fetchFunc.length, 0);
});
@HenarMendiola
@HenarMendiola
@HenarMendiola
const addThreeNumbers = (a, b, c) => a + b + c;
const curryfiedAddThree = (a) => (b, c) => a + b + c;
const curryfiedAddThree = (a, b) => (c) => a + b + c;
const curryfiedAddThree = (a, b, c) => a + b + c;
@HenarMendiola
export const generateCardTemplate = (styles, info) => {
return html`<div style=${styleMap(styles.card)}>
<h2>${info.title}</h2>
<div style=${styleMap(styles.info)}>
<img style=${styleMap(styles.img)} src=${info.img}>
<p>${info.description}</p>
</div>
</div>`
}
@HenarMendiola
export const generateCardTemplate = (styles) => (info) => {
return html`<div style=${styleMap(styles.card)}>
<h2>${info.title}</h2>
<div style=${styleMap(styles.info)}>
<img style=${styleMap(styles.img)} src=${info.img}>
<p>${info.description}</p>
</div>
</div>`
}
@HenarMendiola
const add = (a, b) => a + b;
const addOne = (a) => add(a, 1);
const addTwo = (a) => add(a, 2);
console.log(addOne(3)) // 4;
console.log(addOne(5)) // 7;
@HenarMendiola
const getCollection = R.prop('collection');
const getItems = R.prop('items');
Antes
Después
const collection = R.prop('collection', data);
const items = R.prop('items', collection);
@HenarMendiola
@HenarMendiola
@HenarMendiola
@HenarMendiola
@HenarMendiola