Skip to content

Issue #78 - Cookbook Prototype #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 0.21.0/docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
<li><a href="..">Home</a></li>
<li class="active"><a href="#">Documentation</a></li>
<li><a href="/repl?v=0.21.0">Try Ramda</a></li>
<li><a href="/examples">Cookbook</a></li>
</ul>
</div>
<div class='navbar-right'>
<ul class="nav navbar-nav">
<li><a href="https://github.com/ramda/ramda">GitHub</a></li>
</ul>
</div>
Expand Down
1 change: 1 addition & 0 deletions 0.21.0/examples/code/apply.function.n.times.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Apply a given function N times","name":"Apply function N times","description":null,"source":"let R = require('ramda');\n\n// applyN :: (a -> a) -> Number -> (a -> a)\nconst applyN = R.compose(R.reduceRight(R.compose, R.identity), R.repeat);\n\napplyN(x => x * x, 4)(2); //=> 65536 (2 -> 4 -> 16 -> 256 -> 65536)\n\nconst isOdd = n => n % 2 == 1;\nconst collatz = n => isOdd(n) ? (3 * n + 1) : (n / 2);\n\napplyN(collatz, 5)(27);\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/convert.list.to.object.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Convert a list of property-lists (with header) into a list of objects","name":"Convert List to Object","description":null,"source":"let R = require('ramda');\n\nconst tsv = [\n ['name', 'age', 'drink'], \n ['john', 23, 'wine'], \n ['maggie', 45, 'water']\n];\nR.compose(R.apply(R.lift(R.zipObj)), R.splitAt(1))(tsv);\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/convert.object.to.array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Convert object to array","name":"Convert object to array","description":null,"source":"let R = require('ramda');\n\n// convert :: {a} -> [{ word :: String, count :: a }]\nconst convert = R.compose(R.map(R.zipObj(['word', 'count'])), R.toPairs);\n\nconvert({I: 2, it: 4, that: 1});\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/create.list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Create a list","name":"Create a list","description":"This snippet will create a new list with the specified values. Check out https://clojuredocs.org/clojure.core/list.","source":"let R = require('ramda');\n\n// list :: a... -> [a...]\nvar list = R.unapply(R.identity);\nlist(1, 2, 3);\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/diff.objects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"diffing objects","name":"Diff Objects","description":"similar to <a href=\"http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Maps.html#difference%28java.util.Map,%20java.util.Map%29\">Guava's Maps Difference</a>","source":"let R = require('ramda');\n\nvar groupObjBy = R.curry(R.pipe(\n // Call groupBy with the object as pairs, passing only the value to the key function\n R.useWith(R.groupBy, [R.useWith(__, [R.last]), R.toPairs]),\n R.map(R.fromPairs)\n))\n\nvar diffObjs = R.pipe(\n R.useWith(R.mergeWith(R.merge), [R.map(R.objOf(\"leftValue\")), R.map(R.objOf(\"rightValue\"))]),\n R.groupObjBy(R.cond([\n [\n R.both(R.has(\"leftValue\"), R.has(\"rightValue\")),\n R.pipe(R.values, R.ifElse(R.apply(equals), R.always(\"common\"), R.always(\"difference\")))\n ],\n [R.has(\"leftValue\"), R.always(\"onlyOnLeft\")],\n [R.has(\"rightValue\"), R.always(\"onlyOnRight\")],\n ])),\n R.evolve({\n common: R.map(R.prop(\"leftValue\")),\n onlyOnLeft: R.map(R.prop(\"leftValue\")),\n onlyOnRight: R.map(R.prop(\"rightValue\"))\n })\n);\n\ndiffObjs({a: 1, c: 5, d: 4 }, {a: 1, b: 2, d: 7});\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/do.any.strings.appear.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Do any of these strings appear in another list?","name":"Any Duplicate Strings","description":null,"source":"let R = require('ramda');\n// overlaps :: [a] -> [a] -> Boolean\nconst overlaps = R.pipe(R.intersection, R.complement(R.isEmpty));\nprocess.argv // ['node', 'script.js', '-v']\noverlaps(['-v', '--verbose'], process.argv)\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/filter.using.keys.and.values.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Filter an object using keys as well as values","name":"Filter w/ Keys & Vals","description":null,"source":"let R = require('ramda');\n\nconst filterWithKeys = (pred, obj) => R.pipe(\n R.toPairs, \n R.filter(R.apply(pred)), \n R.fromPairs\n)(obj);\n\nfilterWithKeys(\n (key, val) => key.length === val, \n {red: 3, blue: 5, green: 5, yellow: 2}\n);\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/get.object.by.id.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Get object by id","name":"Get object by id","description":null,"source":"let R = require('ramda');\n\n// findById :: String -> Array -> Object\nconst findById = R.converge(\n R.find,\n [R.pipe(R.nthArg(0), R.propEq(\"id\")), R.nthArg(1)]\n);\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/get.objects.methods.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Get an object's method names","name":"Get method names","description":null,"source":"let R = require('ramda');\n\n// methodNames :: Object -> [String]\nvar methodNames = R.compose(R.keys, R.pickBy(R.is(Function)));\n\nvar obj = {\n foo: true,\n bar: function() {},\n baz: function() {},\n};\n\nmethodNames(obj);\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/increment.with.step.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Create an incrementing or decrementing range of numbers with a step","name":"Increment with step","description":null,"source":"let R = require('ramda');\n\nconst rangeStep = (start, step, stop) => R.map(\n n => start + step * n,\n R.range(0, (1 + (stop - start) / step) >>> 0)\n);\n\nconsole.log(rangeStep(1, 1, 4)); // [1, 2, 3, 4]\nconsole.log(rangeStep(2, 2, 8)); // [2, 4, 6, 8]\nconsole.log(rangeStep(0, 3, 10)); // [0, 3, 6, 9]\nconsole.log(rangeStep(3, -2, -3)); // [3, 1, -1, -3]\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/make.objects.out.of.keys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Make an object out of keys, with values derived from them","name":"Make object from keys","description":null,"source":"let R = require('ramda');\nlet fs = require('fs');\n\n// objFromKeys :: (String -> a) -> [String] -> {String: a}\nconst objFromKeys = R.curry((fn, keys) =>\n R.zipObj(keys, R.map(fn, keys)));\nconst files = ['diary.txt', 'shopping.txt'];\nobjFromKeys(fs.readFileSync, files);\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/map.keys.of.object.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Map keys of an object","description":null,"source":"let R = require('ramda');\n\n// mapKeys :: (String -> String) -> Object -> Object\nconst mapKeys = R.curry((fn, obj) =>\n R.fromPairs(R.map(R.adjust(fn, 0), R.toPairs(obj))));\nmapKeys(R.toUpper, { a: 1, b: 2, c: 3 });\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/map.keys.to.objects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Map Keys To Objects","name":"Map keys to objects","description":null,"source":"let R = require('ramda');\n\nconst mapKeys = R.curry((fn, obj) =>\n R.fromPairs(R.map(R.adjust(fn, 0), R.toPairs(obj))));\n mapKeys(R.toUpper, { a: 1, b: 2, c: 3 });\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/map.value.at.end.of.path.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Map over the value at the end of a path","name":"Map val at end of path","description":null,"source":"let R = require('ramda');\n\n// :: [String] -> (a -> b) -> {k: a} -> {k: b}\nconst mapPath = R.curry((path, f, obj) =>\n R.assocPath(path, f(R.path(path, obj)), obj)\n);\n\nmapPath(['a', 'b', 'c'], R.inc, {a: {b: {c: 3}}});\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/mess.with.dom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Mess with the DOM","name":"Mess with DOM","description":"This example shows how to set styles so that all paragraphs are red.","source":"let R = require('ramda');\n\n// Get all descendants that match selector\n// Note: NodeList is array-like so you can run ramda list functions on it.\n// cssQuery :: String -> Node -> NodeList\nvar cssQuery = R.invoker(1, 'querySelectorAll');\n\n// Mutate style properties on an element\n// setStyle :: String -> String -> Element -> Element\nvar setStyle = R.assoc('style');\n\n// Make all paragraphs and anchors red\nR.pipe(\n cssQuery('a, p'),\n R.map(setStyle('color', 'red'))\n)(document)\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/n.function.calls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Get n function calls as a list","name":"Get n function calls","description":null,"source":"let R = require('ramda');\n\nR.map(R.call, R.repeat(Math.random, 5));\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/object.size.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Get Object Size","name":"Object Size","description":null,"source":"let R = require('ramda');\n\n// objSize :: Object -> Number\nconst objSize = R.nAry(1, R.pipe(\n R.when(R.is(Object), R.keys),\n R.when(R.is(Boolean), R.cond([[R.equals(false), R.always(null)], [R.T, R.always(1)]])),\n R.when(R.is(Number), R.toString),\n R.ifElse(R.isNil, R.always(0), R.length)\n));\nconsole.log(objSize()); // 0\nconsole.log(objSize(undefined)); // 0\nconsole.log(objSize(null)); // 0\nconsole.log(objSize(false)); // 0\nconsole.log(objSize(true)); // 1\nconsole.log(objSize('')); // 0\nconsole.log(objSize('foo')); // 3\nconsole.log(objSize(0)); // 1\nconsole.log(objSize(13)); // 2\nconsole.log(objSize(101)); // 3\nconsole.log(objSize(0.01)); // 4\nconsole.log(objSize({})); // 0\nconsole.log(objSize({foo:undefined, bar:undefined})); // 2\nconsole.log(objSize([])); // 0\nconsole.log(objSize([undefined, undefined])); // 2\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/pick-list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Pick List Values by Index","name":"Pick List by Index","description":"This example shows you how to specify multiple indexes of an array to retrieve multiple values from the array.","source":"let R = require('ramda');\n\n// :: [Number] -> [a] -> [a]\nvar pickIndexes = R.compose(R.values, R.pickAll);\npickIndexes([0, 2], ['a', 'b', 'c']);\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/r.props.deep.fields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Derivative of R.props for deep fields","name":"R.props for deep fields","description":null,"source":"let R = require('ramda');\n\nvar dotPath = R.useWith(R.path, R.split('.'));\nvar propsDotPath = R.useWith(R.ap, R.map(dotPath), R.of);\nvar obj = {\n a: { b: { c: 1 } },\n x: 2\n};\n\npropsDotPath(['a.b.c', 'x'], obj);\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/ramda.fantasy.ajax.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Use ramda-fantasy Future to wrap AJAX","name":"ramda-fantasy wrap AJAX","description":null,"source":"var Future = require('ramda-fantasy').Future;\n// Wrap ajax in a future\n// fetch :: String -> Future String\nvar fetch = function(url) {\n return new Future(function(rej, res) {\n var oReq = new XMLHttpRequest();\n oReq.addEventListener('load', res, false);\n oReq.addEventListener('error', rej, false);\n oReq.addEventListener('abort', rej, false);\n oReq.open('get', url, true);\n oReq.send();\n });\n};\n\n// Could use Either instead of Future but they work about the same.\n// parseJSON :: String -> Future Object\nvar parseJSON = function(str) {\n return new Future(function(rej, res) {\n try {\n res(JSON.parse(str));\n } catch (err) {\n rej({ error: 'json parse error' });\n }\n });\n};\n\n// We have\n// String -> Future String\n// And\n// String -> Future Object\n// So we can .chain() them together\nvar fetchJSON = fetch.chain(parseJSON);\n\n// Get the items out of it?\n// fetchItems :: Future Object -> Future []\nvar fetchItems = fetchJSON.map(R.prop('items'));\n\n// BTW at this point in the code the request still hasn't been sent\n\n// Filter the response?\n// Have to map first to get at the contents of the future then filter\n// fetchNewItems :: Future [Object] -> Future [Object]\nvar fetchNewItems = fetchItems.map(R.filter(R.prop('new')));\n\n// Just get the titles of the items\n// getNewTitles :: Future [Object] -> Future [String]\nvar getNewTitles = fetchNewItems.map(R.map('title'));\n\n// Finally do something\ngetNewTitles('/products.json').fork(console.error, console.log);\n// Now the AJAX req is sent and will log success or failure to console.\n\n// Bonus: Make one ajax request dependent on another\nfetchDependent = fetchJSON.map(R.prop('url')).chain(fetch);\nfetchDependent('urls.json').fork(console.error, console.log);\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/rename.keys.of.object.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Rename keys of an object","name":"Rename keys","description":null,"source":"let R = require('ramda');\n\n/**\n* Creates a new object with the own properties of the provided object, but the\n* keys renamed according to the keysMap object as `{oldKey: newKey}`.\n* When some key is not found in the keysMap, then it's passed as-is.\n*\n* Keep in mind that in the case of keys conflict is behaviour undefined and\n* the result may vary between various JS engines!\n*\n* @sig {a: b} -> {a: *} -> {b: *}\n*/\nconst renameKeys = R.curry((keysMap, obj) => {\n return R.reduce((acc, key) => {\n acc[keysMap[key] || key] = obj[key];\n return acc;\n }, {}, R.keys(obj));\n});\n\nconst input = { firstName: 'Elisia', age: 22, type: 'human' }\n\nrenameKeys({ firstName: 'name', type: 'kind', foo: 'bar' })(input)\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/set.properties.if.dont.exist.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Set properties only if they don't exist","name":"Set non-existent properties","description":"Useful for passing defaults, similar to lodash's _.defaults.","source":"let R = require('ramda');\n\n// defaults :: Object -> Object -> Object\nvar defaults = R.flip(R.merge);\n\n// process.env.SECRET overwrites deadbeef if it exists\ndefaults(process.env, {\n SECRET: 'deadbeef'\n});\n"}
1 change: 1 addition & 0 deletions 0.21.0/examples/code/specific.order.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Apply a list of functions in a specific order into a list of values","name":"Functions in order","description":null,"source":"let R = require('ramda');\n\nconst {red, green, blue} = require('chalk');\nconst disco = R.pipe(R.zipWith(R.call, [ red, green, blue ]), R.join(' '));\nconsole.log(disco([ 'foo', 'bar', 'xyz' ]));\n"}
97 changes: 97 additions & 0 deletions 0.21.0/examples/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<!DOCTYPE html>
<html class="docs-page">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ramda Cookbook</title>
<link rel="stylesheet" type="text/css" href="../style.css">
<script src="https://embed.tonicdev.com"></script>
</head>

<body>
<input type="checkbox" id="open-nav" />
<header class="navbar navbar-fixed-top navbar-inverse container-fluid">
<div class="navbar-header">
<label class="open-nav" for="open-nav"></label>
<a class="navbar-brand" href="#">
<strong>Ramda</strong>
<span class="version">v0.21.0</span>
</a>
</div>
<div class="navbar-left">
<ul class="nav navbar-nav">
<li><a href="/">Home</a></li>
<li><a href="/docs">Documentation</a></li>
<li><a href="/repl?v=0.21.0">Try Ramda</a></li>
<li class='active'><a href="#">Cookbook</a></li>
</ul>
</div>
<div class='navbar-right'>
<ul class="nav navbar-nav">
<li><a href="https://github.com/ramda/ramda">GitHub</a></li>
</ul>
</div>
</header>
<aside class="sidebar no-filter container-fluid">
<ul class="nav nav-pills nav-stacked toc">
<li class='text-capitalize padded' data-file="../examples/code/apply.function.n.times.json">Apply function N times</li>
<li class='text-capitalize padded' data-file="../examples/code/convert.list.to.object.json">Convert List to Object</li>
<li class='text-capitalize padded' data-file="../examples/code/convert.object.to.array.json">Convert object to array</li>
<li class='text-capitalize padded' data-file="../examples/code/create.list.json">Create a list</li>
<li class='text-capitalize padded' data-file="../examples/code/diff.objects.json">Diff Objects</li>
<li class='text-capitalize padded' data-file="../examples/code/do.any.strings.appear.json">Any Duplicate Strings</li>
<li class='text-capitalize padded' data-file="../examples/code/filter.using.keys.and.values.json">Filter w/ Keys &amp; Vals</li>
<li class='text-capitalize padded' data-file="../examples/code/get.object.by.id.json">Get object by id</li>
<li class='text-capitalize padded' data-file="../examples/code/get.objects.methods.json">Get method names</li>
<li class='text-capitalize padded' data-file="../examples/code/increment.with.step.json">Increment with step</li>
<li class='text-capitalize padded' data-file="../examples/code/make.objects.out.of.keys.json">Make object from keys</li>
<li class='text-capitalize padded' data-file="../examples/code/map.keys.of.object.json"></li>
<li class='text-capitalize padded' data-file="../examples/code/map.keys.to.objects.json">Map keys to objects</li>
<li class='text-capitalize padded' data-file="../examples/code/map.value.at.end.of.path.json">Map val at end of path</li>
<li class='text-capitalize padded' data-file="../examples/code/mess.with.dom.json">Mess with DOM</li>
<li class='text-capitalize padded' data-file="../examples/code/n.function.calls.json">Get n function calls</li>
<li class='text-capitalize padded' data-file="../examples/code/object.size.json">Object Size</li>
<li class='text-capitalize padded' data-file="../examples/code/pick-list.json">Pick List by Index</li>
<li class='text-capitalize padded' data-file="../examples/code/r.props.deep.fields.json">R.props for deep fields</li>
<li class='text-capitalize padded' data-file="../examples/code/ramda.fantasy.ajax.json">ramda-fantasy wrap AJAX</li>
<li class='text-capitalize padded' data-file="../examples/code/rename.keys.of.object.json">Rename keys</li>
<li class='text-capitalize padded' data-file="../examples/code/set.properties.if.dont.exist.json">Set non-existent properties</li>
<li class='text-capitalize padded' data-file="../examples/code/specific.order.json">Functions in order</li>
</ul>
</aside>
<main class="container-fluid">
<h1 class='title text-capitalize'>Cookbook</h1>
<div class='snippet'></div>
<div id='tonic'></div>
</main>
</body>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
(function(){
var notebook = Tonic.createNotebook({
element: document.getElementById("tonic"),
source: '//Select an example to continue'
});

$('li').on('click', function(e){
let filename = $(e.currentTarget).data('file');
$('li').removeClass('active');
$(e.currentTarget).addClass('active');

$.ajax({
url: filename,
dataType: 'text'
}).done(function(file){
var example = JSON.parse(file);
$('h1.title').text(example.title);
if(example.description){
$('div.snippet').html(example.description);
}

notebook.setSource(example.source);
});
});
})();
</script>

</html>
5 changes: 5 additions & 0 deletions 0.21.0/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
<li class="active"><a href="#">Home</a></li>
<li><a href="docs">Documentation</a></li>
<li><a href="/repl?v=0.21.0">Try Ramda</a></li>
<li><a href="examples">Cookbook</a></li>
</ul>
</div>
<div class='navbar-right'>
<ul class="nav navbar-nav">
<li><a href="https://github.com/ramda/ramda">GitHub</a></li>
</ul>
</div>
Expand Down
10 changes: 10 additions & 0 deletions 0.21.0/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -6741,6 +6741,9 @@ header {
display: none;
}
}
.padded {
padding: 5px;
}
/**
* Home
*/
Expand Down Expand Up @@ -6876,6 +6879,9 @@ html.docs-page #open-nav:checked ~ main {
border-right: 1px solid #ccc;
font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
}
.sidebar.no-filter {
padding-top: 20px;
}
.sidebar .filter {
width: 100%;
margin: 15px 0;
Expand All @@ -6895,6 +6901,10 @@ html.docs-page #open-nav:checked ~ main {
.sidebar .toc li {
margin: 0;
}
.sidebar .toc li.active {
background-color: #849;
color: white;
}
.sidebar .toc a {
padding: 5px 15px;
border-radius: 0;
Expand Down
Loading