Skip to content

Instantly share code, notes, and snippets.

@kenwebb
Last active April 18, 2020 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenwebb/8d072bf7ed13465d3330bd21e0750037 to your computer and use it in GitHub Desktop.
Save kenwebb/8d072bf7ed13465d3330bd21e0750037 to your computer and use it in GitHub Desktop.
Learning Ramdajs 1
<?xml version="1.0" encoding="UTF-8"?>
<!--Xholon Workbook http://www.primordion.com/Xholon/gwt/ MIT License, Copyright (C) Ken Webb, Fri Apr 10 2020 17:40:40 GMT-0400 (Eastern Daylight Time)-->
<XholonWorkbook>
<Notes><![CDATA[
Xholon
------
Title: Learning Ramdajs 1
Description:
Url: http://www.primordion.com/Xholon/gwt/
InternalName: 8d072bf7ed13465d3330bd21e0750037
Keywords:
My Notes
--------
April 6, 2020
More practice with Ramda.
http://www.primordion.com/Xholon/gwtCrow/Xholon.html?app=Learning+Ramdajs+1&src=lstr&gui=clsc&jslib=ramda.min
https://www.primordion.com/Xholon/gwtCrow/Xholon.html?app=Learning+Ramdajs+1&src=lstr&gui=clsc&jslib=ramda.min
References
----------
(1) https://ramdajs.com/
(2) https://fr.umio.us/why-ramda/
TODO lists
Let's start by imagining we want to be able to filter a TODO list to remove all the completed items.
(3) https://randycoulman.com/blog/categories/thinking-in-ramda/
]]></Notes>
<_-.XholonClass>
<PhysicalSystem/>
<Block/>
<Block2/>
</_-.XholonClass>
<xholonClassDetails>
</xholonClassDetails>
<PhysicalSystem>
<Block/>
<Block2/>
</PhysicalSystem>
<Blockbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var me, R, tasks, incomplete, beh = {
postConfigure: function() {
me = this.cnode;
R = null;
if ($wnd.R) {
R = $wnd.R;
}
else {
me.println("Ramda R is not available.");
}
this.makeTasks();
this.makeFunctions();
me.println(JSON.stringify(tasks));
//me.println(incomplete);
var incompleteTasks = incomplete(tasks);
me.println(JSON.stringify(incompleteTasks));
},
act: function() {
me.println("Testing ramda: " + R.add(3,8));
},
makeTasks: function() {
tasks = [
{
username: 'Scott',
title: 'Add mapObj',
dueDate: '2014-06-09',
complete: false,
effort: 'low',
priority: 'medium'
}, {
username: 'Michael',
title: 'Finish algebraic types',
dueDate: '2014-06-15',
complete: false,
effort: 'high',
priority: 'high'
}, {
username: 'Andrea',
title: 'drink tea',
dueDate: '2015-06-15',
complete: true,
effort: 'low',
priority: 'high'
}, {
username: 'Andrea',
title: 'drink tea',
dueDate: '2016-06-15',
complete: true,
effort: 'low',
priority: 'high'
}, {
username: 'Andrea',
title: 'drink tea',
dueDate: '2020-06-15',
complete: false,
effort: 'low',
priority: 'high'
}
]
},
makeFunctions: function() {
incomplete = $wnd.R.filter($wnd.R.whereEq({complete: false}));
}
}
//# sourceURL=Blockbehavior.js
]]></Blockbehavior>
<Block2behavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var me, R, beh = {
postConfigure: function() {
me = this.cnode;
R = null;
if ($wnd.R) {
R = $wnd.R;
}
else {
me.println("Ramda R is not available.");
}
this.doit();
this.misc();
},
act: function() {
me.println("Testing ramda: " + R.add(3,8));
},
doit: function() {
// data
var book1 = {year: 2015, title: "Aardvarks are varky!", author: "Able"}
var book2 = {year: 2015, title: "Badgers have fun", author: "Baker"}
var book3 = {year: 2018, title: "Cougars growl", author: "Charisma"}
var books = [book1, book2, book3];
// functions
const publishedInYear = R.curry((year, book) => book.year === year)
/*const titlesForYear = R.curry((year, books) =>
R.pipe(
R.filter(publishedInYear(year)),
R.map(book => book.title)
)(books)
)*/
// pointfree
const titlesForYear = year =>
R.pipe(
R.filter(publishedInYear(year)),
R.map(book => book.title)
)
// testing
//me.println("\n" + JSON.stringify(titlesForYear(2015, books))) // ["Aardvarks are varky!", "Badgers have fun"]
me.println("\n" + JSON.stringify(titlesForYear(2015)(books))) // ["Aardvarks are varky!", "Badgers have fun"]
},
// both, either, allPass, placeholder __
misc: function() {
const gt10 = R.gt(R.__, 10)
const lt20 = R.lt(R.__, 20)
//const f = R.both(gt10, lt20);
me.println("\nMISC");
me.println(R.both(gt10, lt20)(15));
me.println(R.both(gt10, lt20)(25));
me.println(R.either(gt10, lt20)(25));
me.println(R.allPass([])(25));
me.println(R.allPass([gt10, lt20, gt10, lt20])(25));
me.println(R.always(true)() === R.T());
me.println(R.inc(41));
me.println(R.dec(43));
me.println(R.negate(-42));
// forever21
// const forever21 = age => age >= 21 ? 21 : age + 1
const forever21 = age => R.ifElse(R.gte(R.__, 21), R.always(21), R.inc)(age)
// Pointfree forever21
const forever21pf = R.ifElse(R.gte(R.__, 21), R.always(21), R.inc)
me.println(forever21pf(25))
}
}
//# sourceURL=Block2behavior.js
]]></Block2behavior>
<SvgClient><Attribute_String roleName="svgUri"><![CDATA[data:image/svg+xml,
<svg width="100" height="50" xmlns="http://www.w3.org/2000/svg">
<g>
<title>Block</title>
<rect id="PhysicalSystem/Block" fill="#98FB98" height="50" width="50" x="25" y="0"/>
<g>
<title>Blockbehavior</title>
<rect id="PhysicalSystem/Block/Blockbehavior" fill="#6AB06A" height="50" width="10" x="80" y="0"/>
</g>
</g>
</svg>
]]></Attribute_String><Attribute_String roleName="setup">${MODELNAME_DEFAULT},${SVGURI_DEFAULT}</Attribute_String></SvgClient>
</XholonWorkbook>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment