Point-free : list operations
by Cristi Salcescu
JavaScript
let authors = {
"1" : { name : "Author C"},
"2" : { name : "Author B"},
"3" : { name : "Author A"}
}
let books = [
{
id : 1,
type : "T",
title : "Book1",
authorID : 1
},
{
id : 2,
type : "T",
title : "Book2",
authorID : 2
},
{
id : 3,
type : "R",
title : "Book3",
authorID : 3
}
];
//Callbacks with points
function getBooksUsingPoints(){
return books.filter(book => isTechnology(book))
.map(book => toBookView(book))
.sort((book1,book2) => ascByAuthor(book1, book2));
}
console.log(getBooksUsingPoints());
//Point-free callbacks
function getBooks(){
return books.filter(isTechnology)
.map(toBookView)
.sort(ascByAuthor);
}
console.log(getBooks());
//Pure functions with points
function isTechnology(book){
return book.type === "T";
}
function toBookView(book){
return Object.freeze({
title : book.title,
author : authors[book.authorID].name
});
}
function ascByAuthor(book1, book2){
if(book1.author < book2.author) return -1;
if(book1.author > book2.author) return 1;
return 0;
}