Skip to content

Instantly share code, notes, and snippets.

@kenwebb
Last active March 25, 2019 22:43
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/dc314bcfd6a9c81948543c330d79656e to your computer and use it in GitHub Desktop.
Save kenwebb/dc314bcfd6a9c81948543c330d79656e to your computer and use it in GitHub Desktop.
Functional Concepts in JavaScript with Ramda
<?xml version="1.0" encoding="UTF-8"?>
<!--Xholon Workbook http://www.primordion.com/Xholon/gwt/ MIT License, Copyright (C) Ken Webb, Mon Mar 25 2019 18:43:25 GMT-0400 (Eastern Daylight Time)-->
<XholonWorkbook>
<Notes><![CDATA[
Xholon
------
Title: Functional Concepts in JavaScript with Ramda
Description:
Url: http://www.primordion.com/Xholon/gwt/
InternalName: dc314bcfd6a9c81948543c330d79656e
Keywords:
My Notes
--------
March 21, 2019
To install ramda
----------------
cd ~/nodespace
npm install ramda
cd ~/nodespace/node_modules/ramda/dist
this contains ramda.js and ramda.min.js
To run this Xholon app
----------------------
http://127.0.0.1:8888/Xholon.html?app=Functional+Concepts+in+JavaScript+with+Ramda&src=lstr&gui=clsc&jslib=ramda.min
http://www.primordion.com/Xholon/gwt/Xholon.html?app=dc314bcfd6a9c81948543c330d79656e&src=gist&gui=clsc&jslib=ramda.min
Ramda R.clone()
---------------
see: ~/gwtspace/Xholon/Xholon/script/javascript.ramda01.js (current contents)
// Ramda JavaScript library
// March 23, 2019
// http://127.0.0.1:8888/Xholon.html?app=HelloWorld&gui=clsc&jslib=ramda.min
const rava = R.clone(xh.avatar());
rava.name();
rava.action("param transcript false;param debug true");
rava.action("who");
rava.action("where");
rava.action("look");
rava.action("first");
rava.action("go 0");
rava.action("help");
rava.action("build Zebra"); // FIXED TypeError: xhcEnt_0_g$ is null, TreeNodeFactoryNew .getXholonClassNode() line 925
rava.action("appear"); // causes rava to have a parent node, to actually be part of the Xholon tree
R.toPairs(rava);
rava.action("build Hello"); // this is OK; Hello already exists in the IH
References
----------
(1) https://www.knowthen.com/blog
1 hour video course by James Moore
I registered for the course, watched the videos, and feel that it was excellent
(2) https://github.com/knowthen/ramdatutorial
.json and .js files for the course
(3) https://github.com/knowthen/ramdatutorial/blob/master/index.js
(4) https://ramdajs.com/
Ramda
A practical functional library for JavaScript programmers.
(5)
(6) https://stackoverflow.com/questions/40361059/where-can-i-find-an-explanation-summary-of-symbols-used-to-explain-functional-pr
The API documentation for the JavaScript functional programming library Ramda.js contains symbolic abbreviations but does not provide a legend for understanding these.
Is there a place (website, article, cheatsheet, etc.) that I can go to to decipher these?
Some examples from the Ramda.js API documentation:
Number -> Number -> Number
Apply f => f (a -> b) -> f a -> f b
Number -> [a] -> [[a]]
(*... -> a) -> [*] -> a
{k: ((a, b, ..., m) -> v)} -> ((a, b, ..., m) -> {k: v})
Filterable f => (a -> Boolean) -> f a -> f a
Lens s a = Functor f => (a -> f a) -> s -> f s
(acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
(Applicative f, Traversable t) => (a -> f a) -> t (f a) -> f (t a)
It's called the Hindley-Milner type system
(7) https://en.m.wikipedia.org/wiki/Hindley%E2%80%93Milner_type_system
A Hindley–Milner (HM) type system is a classical type system for the lambda calculus with parametric polymorphism.
It is also known as Damas–Milner or Damas–Hindley–Milner.
It was first described by J. Roger Hindley and later rediscovered by Robin Milner.
Luis Damas contributed a close formal analysis and proof of the method in his PhD thesis.
(8) https://github.com/ramda/ramda/wiki
(9) https://github.com/ramda/ramda/wiki/Type-Signatures
Type Signatures
(or "What are all those funny arrows about?")
(10) https://github.com/fantasyland/fantasy-land
Specification for interoperability of common algebraic structures in JavaScript
]]></Notes>
<_-.XholonClass>
<PhysicalSystem/>
<Block/>
<Cities/>
<Reducer/>
<GroupByPropReducer/>
<CalcScorer/>
</_-.XholonClass>
<xholonClassDetails>
</xholonClassDetails>
<PhysicalSystem>
<Block/>
<!-- cities.json from ref [2] -->
<Cities><Attribute_String>
[
{
"name": "Bangkok",
"country": "TH",
"region": "SE Asia",
"cost": 1380,
"currency": "USD",
"temp": 302.03888888888895,
"humidity": 78,
"internetSpeed": 20
},
{
"name": "Chiang Mai",
"country": "TH",
"region": "SE Asia",
"cost": 654,
"currency": "USD",
"temp": 299.8166666666667,
"humidity": 83,
"internetSpeed": 20
},
{
"name": "Pattaya",
"country": "TH",
"region": "SE Asia",
"cost": 1183,
"currency": "USD",
"temp": 301.48333333333335,
"humidity": 88,
"internetSpeed": 20
},
{
"name": "Phuket",
"country": "TH",
"region": "SE Asia",
"cost": 1040,
"currency": "USD",
"temp": 302.03888888888895,
"humidity": 74,
"internetSpeed": 20
},
{
"name": "Hoi An",
"country": "VN",
"region": "SE Asia",
"cost": 1083,
"currency": "USD",
"temp": 299.2611111111112,
"humidity": 83,
"internetSpeed": 20
},
{
"name": "Prague",
"country": "CZ",
"region": "Europe",
"cost": 1191,
"currency": "USD",
"temp": 292.03888888888895,
"humidity": 77,
"internetSpeed": 40
},
{
"name": "Phnom Penh",
"country": "KH",
"region": "SE Asia",
"cost": 936,
"currency": "USD",
"temp": 301.48333333333335,
"humidity": 78,
"internetSpeed": 10
},
{
"name": "Sofia",
"country": "BG",
"region": "Europe",
"cost": 1490,
"currency": "USD",
"temp": 296.48333333333335,
"humidity": 69,
"internetSpeed": 40
},
{
"name": "Riga",
"country": "LV",
"region": "Europe",
"cost": 1417,
"currency": "USD",
"temp": 286.48333333333335,
"humidity": 82,
"internetSpeed": 40
},
{
"name": "Ho Chi Minh City",
"country": "VN",
"region": "SE Asia",
"cost": 1329,
"currency": "USD",
"temp": 296.48333333333335,
"humidity": 97,
"internetSpeed": 20
},
{
"name": "Penang",
"country": "MY",
"region": "SE Asia",
"cost": 1065,
"currency": "USD",
"temp": 301.48333333333335,
"humidity": 83,
"internetSpeed": 10
},
{
"name": "Budapest",
"country": "HU",
"region": "Europe",
"cost": 2166,
"currency": "USD",
"temp": 298.15000000000003,
"humidity": 53,
"internetSpeed": 30
},
{
"name": "Kuala Lumpur",
"country": "MY",
"region": "SE Asia",
"cost": 1206,
"currency": "USD",
"temp": 299.2611111111112,
"humidity": 83,
"internetSpeed": 10
},
{
"name": "Brasov",
"country": "RO",
"region": "Europe",
"cost": 1824,
"currency": "USD",
"temp": 294.2611111111112,
"humidity": 57,
"internetSpeed": 80
},
{
"name": "Bratislava",
"country": "SK",
"region": "Europe",
"cost": 1551,
"currency": "USD",
"temp": 294.2611111111112,
"humidity": 82,
"internetSpeed": 20
},
{
"name": "Kosice",
"country": "SK",
"region": "Europe",
"cost": 1466,
"currency": "USD",
"temp": 292.03888888888895,
"humidity": 72,
"internetSpeed": 30
},
{
"name": "Belgrade",
"country": "RS",
"region": "Europe",
"cost": 1358,
"currency": "USD",
"temp": 293.15000000000003,
"humidity": 93,
"internetSpeed": 10
},
{
"name": "Wroclaw",
"country": "PL",
"region": "Europe",
"cost": 1764,
"currency": "USD",
"temp": 294.2611111111112,
"humidity": 68,
"internetSpeed": 20
},
{
"name": "Davao",
"country": "PH",
"region": "SE Asia",
"cost": 1054,
"currency": "USD",
"temp": 294.8166666666667,
"humidity": 100,
"internetSpeed": 5
},
{
"name": "Timisoara",
"country": "RO",
"region": "Europe",
"cost": 1999,
"currency": "USD",
"temp": 294.2611111111112,
"humidity": 94,
"internetSpeed": 60
},
{
"name": "Ubud",
"country": "ID",
"region": "SE Asia",
"cost": 1185,
"currency": "USD",
"temp": 298.15000000000003,
"humidity": 83,
"internetSpeed": 5
},
{
"name": "Ljubljana",
"country": "SI",
"region": "Europe",
"cost": 2162,
"currency": "USD",
"temp": 293.15000000000003,
"humidity": 77,
"internetSpeed": 20
},
{
"name": "Warsaw",
"country": "PL",
"region": "Europe",
"cost": 1987,
"currency": "USD",
"temp": 291.48333333333335,
"humidity": 63,
"internetSpeed": 30
},
{
"name": "Zagreb",
"country": "HR",
"region": "Europe",
"cost": 1539,
"currency": "USD",
"temp": 295.37222222222226,
"humidity": 82,
"internetSpeed": 10
},
{
"name": "Shanghai",
"country": "CN",
"region": "East Asia",
"cost": 1656,
"currency": "USD",
"temp": 298.15000000000003,
"humidity": 83,
"internetSpeed": 20
},
{
"name": "San Juan",
"country": "US",
"region": "Central America",
"cost": 2164,
"currency": "USD",
"temp": 297.03888888888895,
"humidity": 94,
"internetSpeed": 10
},
{
"name": "Cochabamba",
"country": "BO",
"region": "South America",
"cost": 829,
"currency": "USD",
"temp": 297.03888888888895,
"humidity": 25,
"internetSpeed": 5
},
{
"name": "Taipei",
"country": "TW",
"region": "East Asia",
"cost": 2137,
"currency": "USD",
"temp": 304.8166666666667,
"humidity": 69,
"internetSpeed": 40
},
{
"name": "Montreal",
"country": "CA",
"region": "North America",
"cost": 2305,
"currency": "USD",
"temp": 297.5944444444445,
"humidity": 57,
"internetSpeed": 20
},
{
"name": "Las Vegas",
"country": "US",
"region": "North America",
"cost": 2148,
"currency": "USD",
"temp": 307.5944444444445,
"humidity": 20,
"internetSpeed": 40
},
{
"name": "Lima",
"country": "PE",
"region": "South America",
"cost": 1099,
"currency": "USD",
"temp": 292.03888888888895,
"humidity": 72,
"internetSpeed": 10
},
{
"name": "Curitiba",
"country": "BR",
"region": "South America",
"cost": 2145,
"currency": "USD",
"temp": 299.8166666666667,
"humidity": 60,
"internetSpeed": 30
},
{
"name": "Siem Reap",
"country": "KH",
"region": "SE Asia",
"cost": 1045,
"currency": "USD",
"temp": 299.2611111111112,
"humidity": 78,
"internetSpeed": 10
},
{
"name": "Austin",
"country": "US",
"region": "North America",
"cost": 2541,
"currency": "USD",
"temp": 299.2611111111112,
"humidity": 73,
"internetSpeed": 60
},
{
"name": "Beijing",
"country": "CN",
"region": "East Asia",
"cost": 1565,
"currency": "USD",
"temp": 300.9277777777778,
"humidity": 93,
"internetSpeed": 30
},
{
"name": "Cairo",
"country": "EG",
"region": "Africa",
"cost": 917,
"currency": "USD",
"temp": 305.37222222222226,
"humidity": 38,
"internetSpeed": 5
},
{
"name": "Dallas",
"country": "US",
"region": "North America",
"cost": 2265,
"currency": "USD",
"temp": 306.48333333333335,
"humidity": 55,
"internetSpeed": 20
},
{
"name": "Park City",
"country": "US",
"region": "North America",
"cost": 2862,
"currency": "USD",
"temp": 302.03888888888895,
"humidity": 21,
"internetSpeed": 40
},
{
"name": "Lisbon",
"country": "PT",
"region": "Europe",
"cost": 2455,
"currency": "USD",
"temp": 300.37222222222226,
"humidity": 64,
"internetSpeed": 30
},
{
"name": "Santa Monica",
"country": "US",
"region": "North America",
"cost": 2601,
"currency": "USD",
"temp": 302.03888888888895,
"humidity": 69,
"internetSpeed": 40
},
{
"name": "Singapore",
"country": "SG",
"region": "SE Asia",
"cost": 2678,
"currency": "USD",
"temp": 298.15000000000003,
"humidity": 94,
"internetSpeed": 80
},
{
"name": "Medellin",
"country": "CO",
"region": "South America",
"cost": 1296,
"currency": "USD",
"temp": 294.2611111111112,
"humidity": 52,
"internetSpeed": 10
},
{
"name": "Toronto",
"country": "CA",
"region": "North America",
"cost": 2825,
"currency": "USD",
"temp": 300.37222222222226,
"humidity": 69,
"internetSpeed": 20
},
{
"name": "Tokyo",
"country": "JP",
"region": "East Asia",
"cost": 2392,
"currency": "USD",
"temp": 298.15000000000003,
"humidity": 83,
"internetSpeed": 30
},
{
"name": "Porto",
"country": "PT",
"region": "Europe",
"cost": 2686,
"currency": "USD",
"temp": 299.2611111111112,
"humidity": 99,
"internetSpeed": 30
},
{
"name": "Tucson",
"country": "US",
"region": "North America",
"cost": 2882,
"currency": "USD",
"temp": 309.8166666666667,
"humidity": 31,
"internetSpeed": 30
},
{
"name": "Palawan",
"country": "PH",
"region": "SE Asia",
"cost": 1255,
"currency": "USD",
"temp": 295.9277777777778,
"humidity": 96,
"internetSpeed": 5
},
{
"name": "Hong Kong",
"country": "HK",
"region": "East Asia",
"cost": 2634,
"currency": "USD",
"temp": 304.8166666666667,
"humidity": 79,
"internetSpeed": 90
},
{
"name": "Playa del Carmen",
"country": "MX",
"region": "North America",
"cost": 1887,
"currency": "USD",
"temp": 303.7055555555556,
"humidity": 70,
"internetSpeed": 10
},
{
"name": "Manchester",
"country": "UK",
"region": "Europe",
"cost": 2641,
"currency": "USD",
"temp": 294.2611111111112,
"humidity": 64,
"internetSpeed": 30
},
{
"name": "Tel Aviv",
"country": "IL",
"region": "Europe",
"cost": 2519,
"currency": "USD",
"temp": 301.48333333333335,
"humidity": 61,
"internetSpeed": 20
},
{
"name": "Bucharest",
"country": "RO",
"region": "Europe",
"cost": 2064,
"currency": "USD",
"temp": 295.37222222222226,
"humidity": 77,
"internetSpeed": 80
},
{
"name": "Vancouver",
"country": "CA",
"region": "North America",
"cost": 2843,
"currency": "USD",
"temp": 292.5944444444445,
"humidity": 87,
"internetSpeed": 40
},
{
"name": "Bordeaux",
"country": "FR",
"region": "Europe",
"cost": 3077,
"currency": "USD",
"temp": 300.9277777777778,
"humidity": 36,
"internetSpeed": 30
},
{
"name": "Buenos Aires",
"country": "AR",
"region": "South America",
"cost": 1724,
"currency": "USD",
"temp": 291.48333333333335,
"humidity": 88,
"internetSpeed": 10
},
{
"name": "Detroit",
"country": "US",
"region": "North America",
"cost": 2301,
"currency": "USD",
"temp": 300.37222222222226,
"humidity": 44,
"internetSpeed": 30
},
{
"name": "Seoul",
"country": "KR",
"region": "East Asia",
"cost": 2934,
"currency": "USD",
"temp": 293.15000000000003,
"humidity": 82,
"internetSpeed": 60
},
{
"name": "Cebu",
"country": "PH",
"region": "SE Asia",
"cost": 1808,
"currency": "USD",
"temp": 299.2611111111112,
"humidity": 88,
"internetSpeed": 5
},
{
"name": "Tallinn",
"country": "EE",
"region": "Europe",
"cost": 3161,
"currency": "USD",
"temp": 289.2611111111112,
"humidity": 82,
"internetSpeed": 40
},
{
"name": "Boulder",
"country": "US",
"region": "North America",
"cost": 3070,
"currency": "USD",
"temp": 307.5944444444445,
"humidity": 20,
"internetSpeed": 20
},
{
"name": "Salzburg",
"country": "AT",
"region": "Europe",
"cost": 2748,
"currency": "USD",
"temp": 290.9277777777778,
"humidity": 82,
"internetSpeed": 20
},
{
"name": "Madrid",
"country": "ES",
"region": "Europe",
"cost": 2798,
"currency": "USD",
"temp": 308.15000000000003,
"humidity": 17,
"internetSpeed": 30
},
{
"name": "Vienna",
"country": "AT",
"region": "Europe",
"cost": 3096,
"currency": "USD",
"temp": 292.5944444444445,
"humidity": 82,
"internetSpeed": 20
},
{
"name": "Manila",
"country": "PH",
"region": "SE Asia",
"cost": 1371,
"currency": "USD",
"temp": 298.15000000000003,
"humidity": 100,
"internetSpeed": 5
},
{
"name": "Dubai",
"country": "AE",
"region": "Middle East",
"cost": 3233,
"currency": "USD",
"temp": 309.2611111111112,
"humidity": 37,
"internetSpeed": 30
},
{
"name": "Recife",
"country": "BR",
"region": "South America",
"cost": 1994,
"currency": "USD",
"temp": 299.2611111111112,
"humidity": 97,
"internetSpeed": 20
},
{
"name": "Saint Petersburg",
"country": "RU",
"region": "Europe",
"cost": 2124,
"currency": "USD",
"temp": 289.2611111111112,
"humidity": 63,
"internetSpeed": 20
},
{
"name": "Orlando",
"country": "US",
"region": "North America",
"cost": 2436,
"currency": "USD",
"temp": 306.48333333333335,
"humidity": 55,
"internetSpeed": 20
},
{
"name": "Seattle",
"country": "US",
"region": "North America",
"cost": 3254,
"currency": "USD",
"temp": 292.03888888888895,
"humidity": 72,
"internetSpeed": 30
},
{
"name": "Salt Lake City",
"country": "US",
"region": "North America",
"cost": 3729,
"currency": "USD",
"temp": 301.48333333333335,
"humidity": 20,
"internetSpeed": 30
},
{
"name": "Berlin",
"country": "DE",
"region": "Europe",
"cost": 3508,
"currency": "USD",
"temp": 293.7055555555556,
"humidity": 49,
"internetSpeed": 30
},
{
"name": "Wellington",
"country": "NZ",
"region": "Oceania",
"cost": 3539,
"currency": "USD",
"temp": 284.2611111111111,
"humidity": 87,
"internetSpeed": 30
},
{
"name": "Cambridge",
"country": "UK",
"region": "Europe",
"cost": 3270,
"currency": "USD",
"temp": 295.37222222222226,
"humidity": 49,
"internetSpeed": 30
},
{
"name": "Portland",
"country": "US",
"region": "North America",
"cost": 3266,
"currency": "USD",
"temp": 289.8166666666667,
"humidity": 62,
"internetSpeed": 20
},
{
"name": "Granada",
"country": "ES",
"region": "Europe",
"cost": 2855,
"currency": "USD",
"temp": 301.48333333333335,
"humidity": 25,
"internetSpeed": 30
},
{
"name": "Barcelona",
"country": "ES",
"region": "Europe",
"cost": 3431,
"currency": "USD",
"temp": 298.15000000000003,
"humidity": 60,
"internetSpeed": 30
},
{
"name": "Dublin",
"country": "IE",
"region": "Europe",
"cost": 3214,
"currency": "USD",
"temp": 290.37222222222226,
"humidity": 82,
"internetSpeed": 30
},
{
"name": "Auckland",
"country": "NZ",
"region": "Oceania",
"cost": 2869,
"currency": "USD",
"temp": 284.8166666666667,
"humidity": 100,
"internetSpeed": 20
},
{
"name": "Stockholm",
"country": "SE",
"region": "Europe",
"cost": 3714,
"currency": "USD",
"temp": 295.37222222222226,
"humidity": 68,
"internetSpeed": 50
},
{
"name": "Denver",
"country": "US",
"region": "North America",
"cost": 3229,
"currency": "USD",
"temp": 307.5944444444445,
"humidity": 20,
"internetSpeed": 30
},
{
"name": "Basel",
"country": "CH",
"region": "Europe",
"cost": 3795,
"currency": "USD",
"temp": 291.48333333333335,
"humidity": 82,
"internetSpeed": 40
},
{
"name": "Amsterdam",
"country": "NL",
"region": "Europe",
"cost": 3772,
"currency": "USD",
"temp": 293.7055555555556,
"humidity": 68,
"internetSpeed": 40
},
{
"name": "Johannesburg",
"country": "ZA",
"region": "Africa",
"cost": 2072,
"currency": "USD",
"temp": 293.15000000000003,
"humidity": 12,
"internetSpeed": 10
},
{
"name": "Lyon",
"country": "FR",
"region": "Europe",
"cost": 3680,
"currency": "USD",
"temp": 295.37222222222226,
"humidity": 49,
"internetSpeed": 50
},
{
"name": "Rome",
"country": "IT",
"region": "Europe",
"cost": 3250,
"currency": "USD",
"temp": 297.03888888888895,
"humidity": 88,
"internetSpeed": 10
},
{
"name": "Copenhagen",
"country": "DK",
"region": "Europe",
"cost": 3514,
"currency": "USD",
"temp": 292.03888888888895,
"humidity": 77,
"internetSpeed": 40
},
{
"name": "Los Angeles",
"country": "US",
"region": "North America",
"cost": 4178,
"currency": "USD",
"temp": 301.48333333333335,
"humidity": 73,
"internetSpeed": 30
},
{
"name": "Cape Town",
"country": "ZA",
"region": "Africa",
"cost": 2564,
"currency": "USD",
"temp": 288.7055555555556,
"humidity": 84,
"internetSpeed": 10
},
{
"name": "Rotterdam",
"country": "NL",
"region": "Europe",
"cost": 3736,
"currency": "USD",
"temp": 293.15000000000003,
"humidity": 63,
"internetSpeed": 50
},
{
"name": "Oslo",
"country": "NO",
"region": "Europe",
"cost": 4294,
"currency": "USD",
"temp": 293.15000000000003,
"humidity": 59,
"internetSpeed": 30
},
{
"name": "Columbus",
"country": "US",
"region": "North America",
"cost": 4208,
"currency": "USD",
"temp": 301.48333333333335,
"humidity": 65,
"internetSpeed": 20
},
{
"name": "Dusseldorf",
"country": "DE",
"region": "Europe",
"cost": 3910,
"currency": "USD",
"temp": 295.9277777777778,
"humidity": 68,
"internetSpeed": 30
},
{
"name": "Philadelphia",
"country": "US",
"region": "North America",
"cost": 4358,
"currency": "USD",
"temp": 303.15000000000003,
"humidity": 45,
"internetSpeed": 30
},
{
"name": "Minneapolis",
"country": "US",
"region": "North America",
"cost": 3724,
"currency": "USD",
"temp": 293.15000000000003,
"humidity": 82,
"internetSpeed": 20
},
{
"name": "New York City",
"country": "US",
"region": "North America",
"cost": 4674,
"currency": "USD",
"temp": 302.03888888888895,
"humidity": 35,
"internetSpeed": 40
},
{
"name": "Chicago",
"country": "US",
"region": "North America",
"cost": 3911,
"currency": "USD",
"temp": 300.9277777777778,
"humidity": 50,
"internetSpeed": 30
},
{
"name": "Paris",
"country": "FR",
"region": "Europe",
"cost": 4656,
"currency": "USD",
"temp": 296.48333333333335,
"humidity": 49,
"internetSpeed": 70
},
{
"name": "Miami",
"country": "US",
"region": "North America",
"cost": 4119,
"currency": "USD",
"temp": 304.2611111111112,
"humidity": 74,
"internetSpeed": 30
},
{
"name": "San Francisco",
"country": "US",
"region": "North America",
"cost": 4854,
"currency": "USD",
"temp": 295.37222222222226,
"humidity": 82,
"internetSpeed": 30
},
{
"name": "London",
"country": "UK",
"region": "Europe",
"cost": 4504,
"currency": "USD",
"temp": 295.37222222222226,
"humidity": 60,
"internetSpeed": 30
}
]
</Attribute_String></Cities>
<Reducer/>
<GroupByPropReducer/>
<CalcScorer/>
</PhysicalSystem>
<Blockbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
// video 2
// note that nothing is mutated, vs using a for loop where the array and the array index both get mutated
const numbers = [1, 2, 3];
function double(number) {
return number * 2;
}
const doubledNumbers = numbers.map(double);
this.println(doubledNumbers);
//# sourceURL=Blockbehavior.js
]]></Blockbehavior>
<Citiesbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
// video 2 and 3
const VERSION = 2;
var me, updatedCities, index, R, beh = {
postConfigure: function() {
me = this.cnode.parent();
index = 0;
R = $wnd.R; // ramda
switch (VERSION) {
case 1: this.version1(); break;
case 2: this.version2(); break;
default: break;
}
},
version1: function() {
me.println("version 1 ...");
const cities = $wnd.JSON.parse(me.first().remove().text());
const KtoC = k => k - 273.15;
const updateTemperature = city => {
const temp = Math.round(KtoC(city.temp));
//return temp;
return R.merge(city, { temp });
}
//const updatedCitiesConst = cities.map(updateTemperature);
//updatedCities = updatedCitiesConst; //cities.map(updateTemperature); // updatedCities should be immutable (const)
updatedCities = cities.map(updateTemperature);
Object.freeze(updatedCities);
me.println(updatedCities.length);
},
version2: function() {
me.println("version 2 ...");
const cities = $wnd.JSON.parse(me.first().remove().text());
const KtoC = k => k - 273.15;
const KtoF = k => k * 9 / 5 - 459.67;
const updateTemperature = R.curry((convertFn, city) => {
const temp = Math.round(convertFn(city.temp));
return R.merge(city, { temp });
});
updatedCities = R.map(updateTemperature(KtoF), cities);
Object.freeze(updatedCities);
me.println(updatedCities.length);
},
processReceivedSyncMessage: function(msg) {
return updatedCities;
},
act: function() {
me.println($wnd.JSON.stringify(updatedCities[index++]));
}
}
//# sourceURL=Citiesbehavior.js
]]></Citiesbehavior>
<Reducerbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
// video 4
var me, cities, R, beh = {
postConfigure: function() {
me = this.cnode.parent();
R = $wnd.R; // ramda
cities = me.prev().call(101, null, me).data;
me.println(cities.length);
this.reduce1();
this.reduce2();
},
reduce1: function() {
const numbers = [1, 2, 3];
function sum(x, y) {return x + y;}
const total = numbers.reduce(sum, 0);
me.println(total);
},
reduce2: function() {
const totalCostReducer = (acc, city) => {
const { cost = 0 } = city;
return acc + cost;
}
const totalCost = R.reduce(totalCostReducer, 0, cities);
const cityCount = R.length(cities);
// the following print the values obtained in refs [1, 2]
me.println(totalCost); // 250891
me.println(cityCount); // 100
me.println(totalCost / cityCount); // 2508.91
}
}
//# sourceURL=Reducerbehavior.js
]]></Reducerbehavior>
<GroupByPropReducerbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
// video 5
var me, cities, groupedByPropVar, R, beh = {
postConfigure: function() {
me = this.cnode.parent();
R = $wnd.R; // ramda
cities = me.prev().prev().call(101, null, me).data;
me.println(cities.length);
this.reduce1();
//this.reduce2();
},
reduce1: function() {
const groupByPropReducer = (acc, city) => {
const { cost = [], internetSpeed = [] } = acc;
return R.merge(acc, {
cost: R.append(city.cost, cost),
internetSpeed: R.append(city.internetSpeed, internetSpeed),
});
}
const groupedByProp = R.reduce(groupByPropReducer, {}, cities);
groupedByPropVar = groupedByProp;
console.log(groupedByProp);
},
processReceivedSyncMessage: function(msg) {
return groupedByPropVar;
}
}
//# sourceURL=GroupByPropReducerbehavior.js
]]></GroupByPropReducerbehavior>
<CalcScorerbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
// video 6 and 7
var me, cities, groupedByProp, scoredCities, R, beh = {
postConfigure: function() {
me = this.cnode.parent();
R = $wnd.R; // ramda
cities = me.prev().prev().prev().call(101, null, me).data;
groupedByProp = me.prev().call(101, null, me).data;
me.println(cities.length);
this.calcScores();
this.filterAndSort();
},
calcScores: function() {
const percentile = (array, value) => {
const length = R.length(array);
const eqVal = R.equals(value);
const alen = !R.any(eqVal, array)
? R.range(0, length + 1)
: R.range(0, length);
const sortedArray = R.sort((a, b) => a - b, array);
const idx = R.map(eqVal, sortedArray);
const alenTrue = R.filter((v, i) => {
return idx[alen.indexOf(v)] === true;
}, alen);
const mean = R.mean(alenTrue);
const percent = mean / length;
return percent;
};
const calcScore = city => {
const { cost = 0, internetSpeed = 0 } = city;
const costPercentile = percentile(groupedByProp.cost, cost);
const internetSpeedPercentile = percentile(
groupedByProp.internetSpeed,
internetSpeed,
);
const score =
100 * (1.0 - costPercentile) +
20 * internetSpeedPercentile;
return R.merge(city, { score });
}
const scoredCitiesConst = R.map(calcScore, cities);
scoredCities = scoredCitiesConst;
console.log(scoredCities);
},
filterAndSort: function() {
const filterByWeather = city => {
const { temp = 0, humidity = 0 } = city;
return temp > 68 && temp < 85 && humidity > 30 && humidity < 70;
}
const filteredCities = R.filter(filterByWeather, scoredCities);
me.println(filteredCities.length); // 26
me.println(R.length(filteredCities)); // 26
const sortedCities = R.sortWith(
[R.descend(city => city.score)],
filteredCities,
);
console.log(sortedCities);
const top10 = R.take(10, sortedCities);
console.log(top10);
me.println(R.length(top10));
}
}
//# sourceURL=CalcScorerbehavior.js
]]></CalcScorerbehavior>
<!-- TODO
const cityToArray = city => {
const { name, country, score, cost, temp, internetSpeed } = city;
return [name, country, score, cost, temp, internetSpeed];
};
const interestingProps = [
'Name',
'Country',
'Score',
'Cost',
'Temp',
'Internet',
];
const topCities = R.pipe(
R.map(updateTemperature(KtoF)),
R.filter(filterByWeather),
R.map(calcScore),
R.sortWith([R.descend(city => city.score)]),
R.take(10),
R.map(cityToArray),
R.prepend(interestingProps),
table,
)(cities);
console.log(topCities);
-->
<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>Cities</title>
<rect id="PhysicalSystem/Cities" 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