Last active
January 21, 2025 17:01
Code for "Transforming Composition" talk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mockData = getMockData(); // mock API response data | |
var movieIDs = [ 627, 699, 378, 92, 933, 725, 528, 3, 156, ]; | |
var movieListings = []; | |
for (let movieID of movieIDs) { | |
if (validMovie(movieID)) { | |
let movieData = lookupMovie(movieID); | |
if (recentMovie(movieData)) { | |
movieListings.push( | |
formatMovie(movieData) | |
); | |
} | |
} | |
} | |
console.log(movieListings); | |
// [ "How To Train Your Dragon (2010)", "Barbie (2023)", "Frozen (2013)" ] | |
// ******************************* | |
function validMovie(movieID) { return movieID >= 100; } | |
function recentMovie(movieData) { return movieData.released >= 2000; }; | |
function formatMovie(movieData) { | |
return `${movieData.title} (${movieData.released})`; | |
} | |
// mock of fetching movie from an external database | |
function lookupMovie(movieID) { return mockData[movieID]; } | |
function getMockData() { | |
return { | |
156: { title: "Gone With The Wind", released: 1940, }, | |
378: { title: "The Godfather", released: 1972, }, | |
528: { title: "Star Wars: A New Hope", released: 1977, }, | |
627: { title: "Titanic", released: 1997, }, | |
699: { title: "How To Train Your Dragon", released: 2010, }, | |
725: { title: "Frozen", released: 2013, }, | |
933: { title: "Barbie", released: 2023, }, | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mockData = getMockData(); // mock API response data | |
var movieIDs = [ 627, 699, 378, 92, 933, 725, 528, 3, 156, ]; | |
var movieListings = ( | |
movieIDs | |
.filter(validMovie) | |
.map(lookupMovie) | |
.filter(recentMovie) | |
.map(formatMovie) | |
); | |
console.log(movieListings); | |
// [ "How To Train Your Dragon (2010)", "Barbie (2023)", "Frozen (2013)" ] | |
// ******************************* | |
function validMovie(movieID) { return movieID >= 100; } | |
function recentMovie(movieData) { return movieData.released >= 2000; }; | |
function formatMovie(movieData) { | |
return `${movieData.title} (${movieData.released})`; | |
} | |
// mock of fetching movie from an external database | |
function lookupMovie(movieID) { return mockData[movieID]; } | |
function getMockData() { | |
return { | |
156: { title: "Gone With The Wind", released: 1940, }, | |
378: { title: "The Godfather", released: 1972, }, | |
528: { title: "Star Wars: A New Hope", released: 1977, }, | |
627: { title: "Titanic", released: 1997, }, | |
699: { title: "How To Train Your Dragon", released: 2010, }, | |
725: { title: "Frozen", released: 2013, }, | |
933: { title: "Barbie", released: 2023, }, | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mockData = getMockData(); // mock API response data | |
formatMovie(lookupMovie(699)); // "How To Train Your Dragon (2010)" | |
var dragon1 = compose(formatMovie,lookupMovie); | |
dragon1(699); // "How To Train Your Dragon (2010)" | |
var dragon2 = flow(lookupMovie,formatMovie); | |
dragon2(699); // "How To Train Your Dragon (2010)" | |
// ******************************* | |
// aka: compose-right | |
function compose(...fns) { | |
return v => fns.reduceRight((inp,fn) => fn(inp),v); | |
} | |
// aka: compose-left | |
function flow(...fns) { | |
return v => fns.reduce((inp,fn) => fn(inp),v); | |
} | |
function formatMovie(movieData) { | |
return `${movieData.title} (${movieData.released})`; | |
} | |
// mock of fetching movie from an external database | |
function lookupMovie(movieID) { return mockData[movieID]; } | |
function getMockData() { | |
return { | |
156: { title: "Gone With The Wind", released: 1940, }, | |
378: { title: "The Godfather", released: 1972, }, | |
528: { title: "Star Wars: A New Hope", released: 1977, }, | |
627: { title: "Titanic", released: 1997, }, | |
699: { title: "How To Train Your Dragon", released: 2010, }, | |
725: { title: "Frozen", released: 2013, }, | |
933: { title: "Barbie", released: 2023, }, | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mockData = getMockData(); // mock API response data | |
formatMovie(recentMovie(lookupMovie(699))); // "undefined (undefined)" | |
var dragon1 = compose(formatMovie,recentMovie,lookupMovie); | |
dragon1(699); // "undefined (undefined)" | |
var dragon2 = flow(lookupMovie,recentMovie,formatMovie); | |
dragon2(699); // "undefined (undefined)" | |
// ******************************* | |
// aka: compose-right | |
function compose(...fns) { | |
return v => fns.reduceRight((inp,fn) => fn(inp),v); | |
} | |
// aka: compose-left | |
function flow(...fns) { | |
return v => fns.reduce((inp,fn) => fn(inp),v); | |
} | |
function recentMovie(movieData) { return movieData.released >= 2000; }; | |
function formatMovie(movieData) { | |
return `${movieData.title} (${movieData.released})`; | |
} | |
// mock of fetching movie from an external database | |
function lookupMovie(movieID) { return mockData[movieID]; } | |
function getMockData() { | |
return { | |
156: { title: "Gone With The Wind", released: 1940, }, | |
378: { title: "The Godfather", released: 1972, }, | |
528: { title: "Star Wars: A New Hope", released: 1977, }, | |
627: { title: "Titanic", released: 1997, }, | |
699: { title: "How to Train Your Dragon", released: 2010, }, | |
725: { title: "Frozen", released: 2013, }, | |
933: { title: "Barbie", released: 2023, }, | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mockData = getMockData(); // mock API response data | |
var movieIDs = [ 627, 699, 378, 92, 933, 725, 528, 3, 156, ]; | |
function showMovie(movieID) { | |
if (validMovie(movieID)) { | |
var movieData = lookupMovie(movieID); | |
if (recentMovie(movieData)) { | |
return formatMovie(movieData); | |
} | |
} | |
} | |
showMovie(699); // "How to Train Your Dragon (2010)" | |
movieIDs | |
.map(showMovie) | |
.filter(Boolean); // remove pesky undefineds | |
// [ "How To Train Your Dragon (2010)", "Barbie (2023)", "Frozen (2013)" ] | |
// ******************************* | |
// aka: compose-right | |
function compose(...fns) { | |
return v => fns.reduceRight((inp,fn) => fn(inp),v); | |
} | |
// aka: compose-left | |
function flow(...fns) { | |
return v => fns.reduce((inp,fn) => fn(inp),v); | |
} | |
function validMovie(movieID) { return movieID >= 100; } | |
function recentMovie(movieData) { return movieData.released >= 2000; }; | |
function formatMovie(movieData) { return `${movieData.title} (${movieData.released})`; } | |
// mock of fetching movie from an external database | |
function lookupMovie(movieID) { return mockData[movieID]; } | |
function getMockData() { | |
return { | |
156: { title: "Gone With The Wind", released: 1940, }, | |
378: { title: "The Godfather", released: 1972, }, | |
528: { title: "Star Wars: A New Hope", released: 1977, }, | |
627: { title: "Titanic", released: 1997, }, | |
699: { title: "How To Train Your Dragon", released: 2010, }, | |
725: { title: "Frozen", released: 2013, }, | |
933: { title: "Barbie", released: 2023, }, | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mockData = getMockData(); // mock API response data | |
var showMovie = flow( | |
filter(validMovie), | |
map(lookupMovie), | |
filter(recentMovie), | |
map(formatMovie) | |
); | |
showMovie([ 699 ]).next(); | |
// { value: "How To Train Your Dragon (2010)", done: false } | |
// ******************************* | |
function filter(fn) { | |
return function *filter(iterable) { | |
for (let v of iterable) { | |
if (fn(v)) { | |
yield v; | |
} | |
} | |
}; | |
} | |
function map(fn) { | |
return function *map(iterable) { | |
for (let v of iterable) { | |
yield fn(v); | |
} | |
}; | |
} | |
// aka: compose-left | |
function flow(...fns) { | |
return v => fns.reduce((inp,fn) => fn(inp),v); | |
} | |
function validMovie(movieID) { return movieID >= 100; } | |
function recentMovie(movieData) { return movieData.released >= 2000; }; | |
function formatMovie(movieData) { return `${movieData.title} (${movieData.released})`; } | |
// mock of fetching movie from an external database | |
function lookupMovie(movieID) { return mockData[movieID]; } | |
function getMockData() { | |
return { | |
156: { title: "Gone With The Wind", released: 1940, }, | |
378: { title: "The Godfather", released: 1972, }, | |
528: { title: "Star Wars: A New Hope", released: 1977, }, | |
627: { title: "Titanic", released: 1997, }, | |
699: { title: "How To Train Your Dragon", released: 2010, }, | |
725: { title: "Frozen", released: 2013, }, | |
933: { title: "Barbie", released: 2023, }, | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mockData = getMockData(); // mock API response data | |
var showMovie = flow( | |
gather, | |
filter(validMovie), | |
map(lookupMovie), | |
filter(recentMovie), | |
map(formatMovie), | |
takeOne | |
); | |
showMovie(699); // "How To Train Your Dragon (2010)" | |
// ******************************* | |
function filter(fn) { | |
return function *filter(iterable) { | |
for (let v of iterable) { | |
if (fn(v)) { | |
yield v; | |
} | |
} | |
}; | |
} | |
function map(fn) { | |
return function *map(iterable) { | |
for (let v of iterable) { | |
yield fn(v); | |
} | |
}; | |
} | |
function gather(...vals) { return vals; } | |
function takeOne(iter) { return iter.next().value; } | |
// aka: compose-left | |
function flow(...fns) { | |
return v => fns.reduce((inp,fn) => fn(inp),v); | |
} | |
function validMovie(movieID) { return movieID >= 100; } | |
function recentMovie(movieData) { return movieData.released >= 2000; }; | |
function formatMovie(movieData) { | |
return `${movieData.title} (${movieData.released})`; | |
} | |
// mock of fetching movie from an external database | |
function lookupMovie(movieID) { return mockData[movieID]; } | |
function getMockData() { | |
return { | |
156: { title: "Gone With The Wind", released: 1940, }, | |
378: { title: "The Godfather", released: 1972, }, | |
528: { title: "Star Wars: A New Hope", released: 1977, }, | |
627: { title: "Titanic", released: 1997, }, | |
699: { title: "How To Train Your Dragon", released: 2010, }, | |
725: { title: "Frozen", released: 2013, }, | |
933: { title: "Barbie", released: 2023, }, | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mockData = getMockData(); // mock API response data | |
var movieIDs = [ 627, 699, 378, 92, 933, 725, 528, 3, 156, ]; | |
var movieListings = ( | |
flow( | |
filter(validMovie), | |
map(lookupMovie), | |
filter(recentMovie), | |
map(formatMovie), | |
takeAll | |
)(movieIDs) | |
); | |
console.log(movieListings); | |
// [ "How To Train Your Dragon (2010)", "Barbie (2023)", "Frozen (2013)" ] | |
// ******************************* | |
function filter(fn) { | |
return function *filter(iterable) { | |
for (let v of iterable) { | |
if (fn(v)) { | |
yield v; | |
} | |
} | |
}; | |
} | |
function map(fn) { | |
return function *map(iterable) { | |
for (let v of iterable) { | |
yield fn(v); | |
} | |
}; | |
} | |
function takeAll(iter) { return [ ...iter ]; } | |
// aka: compose-left | |
function flow(...fns) { | |
return v => fns.reduce((inp,fn) => fn(inp),v); | |
} | |
function validMovie(movieID) { return movieID >= 100; } | |
function recentMovie(movieData) { return movieData.released >= 2000; }; | |
function formatMovie(movieData) { | |
return `${movieData.title} (${movieData.released})`; | |
} | |
// mock of fetching movie from an external database | |
function lookupMovie(movieID) { return mockData[movieID]; } | |
function getMockData() { | |
return { | |
156: { title: "Gone With The Wind", released: 1940, }, | |
378: { title: "The Godfather", released: 1972, }, | |
528: { title: "Star Wars: A New Hope", released: 1977, }, | |
627: { title: "Titanic", released: 1997, }, | |
699: { title: "How To Train Your Dragon", released: 2010, }, | |
725: { title: "Frozen", released: 2013, }, | |
933: { title: "Barbie", released: 2023, }, | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mockData = getMockData(); // mock API response data | |
var movieIDs = [ 627, 699, 378, 92, 933, 725, 528, 3, 156, ]; | |
var movieListings = ( | |
Iterator.from(movieIDs) | |
.filter(validMovie) | |
.map(lookupMovie) | |
.filter(recentMovie) | |
.map(formatMovie) | |
.toArray() | |
); | |
console.log(movieListings); | |
// [ "How To Train Your Dragon (2010)", "Barbie (2023)", "Frozen (2013)" ] | |
// ******************************* | |
function validMovie(movieID) { return movieID >= 100; } | |
function recentMovie(movieData) { return movieData.released >= 2000; }; | |
function formatMovie(movieData) { | |
return `${movieData.title} (${movieData.released})`; | |
} | |
// mock of fetching movie from an external database | |
function lookupMovie(movieID) { return mockData[movieID]; } | |
function getMockData() { | |
return { | |
156: { title: "Gone With The Wind", released: 1940, }, | |
378: { title: "The Godfather", released: 1972, }, | |
528: { title: "Star Wars: A New Hope", released: 1977, }, | |
627: { title: "Titanic", released: 1997, }, | |
699: { title: "How To Train Your Dragon", released: 2010, }, | |
725: { title: "Frozen", released: 2013, }, | |
933: { title: "Barbie", released: 2023, }, | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mapT = | |
mapperFn => | |
combinerFn => | |
(list,v) => | |
combinerFn(list,mapperFn(v)); | |
var filterT = | |
predicateFn => | |
combinerFn => | |
(list,v) => | |
predicateFn(v) ? combinerFn(list,v) : list; | |
var transduceL = | |
fn => | |
list => | |
list.reduce(fn(concatL),[]); | |
function concatL(list,v) { return list.concat(v); } | |
var mockData = getMockData(); // mock API response data | |
var movieIDs = [ 627, 699, 378, 92, 933, 725, 528, 3, 156, ]; | |
var movieListings = ( | |
transduceL( | |
compose( | |
filterT(validMovie), | |
mapT(lookupMovie), | |
filterT(recentMovie), | |
mapT(formatMovie) | |
) | |
)(movieIDs) | |
); | |
console.log(movieListings); | |
// [ "How To Train Your Dragon (2010)", "Barbie (2023)", "Frozen (2013)" ] | |
// ******************************* | |
// aka: compose-right | |
function compose(...fns) { | |
return v => fns.reduceRight((inp,fn) => fn(inp),v); | |
} | |
function validMovie(movieID) { return movieID >= 100; } | |
function recentMovie(movieData) { return movieData.released >= 2000; }; | |
function formatMovie(movieData) { | |
return `${movieData.title} (${movieData.released})`; | |
} | |
// mock of fetching movie from an external database | |
function lookupMovie(movieID) { return mockData[movieID]; } | |
function getMockData() { | |
return { | |
156: { title: "Gone With The Wind", released: 1940, }, | |
378: { title: "The Godfather", released: 1972, }, | |
528: { title: "Star Wars: A New Hope", released: 1977, }, | |
627: { title: "Titanic", released: 1997, }, | |
699: { title: "How To Train Your Dragon", released: 2010, }, | |
725: { title: "Frozen", released: 2013, }, | |
933: { title: "Barbie", released: 2023, }, | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mockData = getMockData(); // mock API response data | |
var movieIDs = [ 627, 699, 378, 92, 933, 725, 528, 3, 156, ]; | |
import { serial as FaS, transducers as FaT } from "fasy"; | |
var movieListings = await ( | |
FaT.into( | |
FaS.compose([ | |
FaT.filter(validMovie), | |
FaT.map(lookupMovie), | |
FaT.filter(recentMovie), | |
FaT.map(formatMovie) | |
]), | |
[], | |
movieIDs | |
) | |
); | |
console.log(movieListings); | |
// [ "How To Train Your Dragon (2010)", "Barbie (2023)", "Frozen (2013)" ] | |
// ******************************* | |
function validMovie(movieID) { return movieID >= 100; } | |
function recentMovie(movieData) { return movieData.released >= 2000; }; | |
function formatMovie(movieData) { | |
return `${movieData.title} (${movieData.released})`; | |
} | |
// mock of fetching movie from an external database | |
async function lookupMovie(movieID) { return mockData[movieID]; } | |
function getMockData() { | |
return { | |
156: { title: "Gone With The Wind", released: 1940, }, | |
378: { title: "The Godfather", released: 1972, }, | |
528: { title: "Star Wars: A New Hope", released: 1977, }, | |
627: { title: "Titanic", released: 1997, }, | |
699: { title: "How To Train Your Dragon", released: 2010, }, | |
725: { title: "Frozen", released: 2013, }, | |
933: { title: "Barbie", released: 2023, }, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment