313 lines
6.3 KiB
JavaScript
313 lines
6.3 KiB
JavaScript
// Generated by demo/js-usecases.rkt
|
|
// Each use case is wrapped in a function so snippets with return are valid.
|
|
|
|
// random-number
|
|
function run_random_number() {
|
|
function randomBetween1And5() {
|
|
return (Math.floor((Math.random() * 5)) + 1);
|
|
}
|
|
}
|
|
|
|
// unique-values
|
|
function run_unique_values() {
|
|
function uniqueValues(xs) {
|
|
return Array.from(new Set(xs));
|
|
}
|
|
}
|
|
|
|
// falsey-values
|
|
function run_falsey_values() {
|
|
function falseyValues() {
|
|
return [false, 0, "", null, undefined, NaN];
|
|
}
|
|
}
|
|
|
|
// currying
|
|
function run_currying() {
|
|
function add(x) {
|
|
return function(y) {
|
|
return (x + y);
|
|
};
|
|
}
|
|
}
|
|
|
|
// object-destructuring
|
|
function run_object_destructuring() {
|
|
function describePerson(person) {
|
|
return (() => {
|
|
const { name: name, age: age = 0 } = person;
|
|
return (name + ":" + String(age));
|
|
})();
|
|
}
|
|
}
|
|
|
|
// timer-interval
|
|
function run_timer_interval() {
|
|
function startTimer() {
|
|
{
|
|
let ticks = 0;
|
|
let intervalId = false;
|
|
intervalId = setInterval(function() {
|
|
ticks = (ticks + 1);
|
|
if ((ticks === 3)) {
|
|
clearInterval(intervalId);
|
|
}
|
|
return undefined;
|
|
}, 10);
|
|
return {id: intervalId, getTicks: function() {
|
|
return ticks;
|
|
}};
|
|
}
|
|
}
|
|
}
|
|
|
|
// object-props
|
|
function run_object_props() {
|
|
function objectProps() {
|
|
{
|
|
let obj = {a: 1};
|
|
let a1 = obj.a;
|
|
let a2 = obj["a"];
|
|
return (() => {
|
|
const { a: a3 } = obj;
|
|
obj.b = 2;
|
|
obj["c"] = 3;
|
|
delete obj["a"];
|
|
return [a1, a2, a3, obj.b, obj["c"], Object.hasOwn(obj, "a")];
|
|
})();
|
|
}
|
|
}
|
|
}
|
|
|
|
// string-concat-order
|
|
function run_string_concat_order() {
|
|
function concatOrder() {
|
|
return [(1 + 2 + "3"), ("1" + 2 + 3)];
|
|
}
|
|
}
|
|
|
|
// freeze-vs-seal
|
|
function run_freeze_vs_seal() {
|
|
function freezeVsSeal() {
|
|
{
|
|
let frozen = Object.freeze({a: 1});
|
|
let sealed = Object.seal({a: 1});
|
|
frozen.a = 9;
|
|
sealed.a = 9;
|
|
delete sealed["a"];
|
|
return [frozen.a, sealed.a, Object.isFrozen(frozen), Object.isSealed(sealed), Object.hasOwn(sealed, "a")];
|
|
}
|
|
}
|
|
}
|
|
|
|
// switch
|
|
function run_switch() {
|
|
function switchExample(n) {
|
|
return (() => {
|
|
{
|
|
const __case_value = n;
|
|
switch (__case_value) {
|
|
case 1:
|
|
return "one";
|
|
break;
|
|
case 2:
|
|
case 3:
|
|
return "two-or-three";
|
|
break;
|
|
default:
|
|
return "other";
|
|
}
|
|
}
|
|
})();
|
|
}
|
|
}
|
|
|
|
// class-constructor
|
|
function run_class_constructor() {
|
|
class Greeter {
|
|
constructor(name = "world") {
|
|
this.name = name;
|
|
}
|
|
greet() {
|
|
return ("Hello " + this.name);
|
|
}
|
|
}
|
|
function classExample() {
|
|
{
|
|
let a = new Greeter();
|
|
let b = new Greeter("Ada");
|
|
return [a.greet(), b.greet()];
|
|
}
|
|
}
|
|
}
|
|
|
|
// sort-objects-by-property
|
|
function run_sort_objects_by_property() {
|
|
function sortByProperty(xs, prop) {
|
|
return xs.slice().sort(function(a, b) {
|
|
return (a[prop] - b[prop]);
|
|
});
|
|
}
|
|
}
|
|
|
|
// delete-array-elements
|
|
function run_delete_array_elements() {
|
|
function deleteArrayWays(xs) {
|
|
{
|
|
let a1 = xs.slice();
|
|
let a2 = xs.slice();
|
|
let a3 = xs.slice();
|
|
let a4 = xs.slice();
|
|
a1.splice(1, 1);
|
|
a2 = a2.filter(function(x, i) {
|
|
return ((i === 1) === false);
|
|
});
|
|
a3 = a3.slice(0, 1).concat(a3.slice(2));
|
|
delete a4[1];
|
|
return [a1, a2, a3, [Object.hasOwn(a4, "1"), (a4).length]];
|
|
}
|
|
}
|
|
}
|
|
|
|
// bubble-sort
|
|
function run_bubble_sort() {
|
|
function bubbleSort(xs) {
|
|
{
|
|
let a = xs.slice();
|
|
let n = (a).length;
|
|
while (n > 1) {
|
|
{
|
|
let i = 1;
|
|
while (i < n) {
|
|
if ((() => {
|
|
const __cmp_1 = (a)[(i - 1)];
|
|
const __cmp_2 = (a)[i];
|
|
return (__cmp_1 > __cmp_2);
|
|
})()) {
|
|
{
|
|
let tmp = (a)[(i - 1)];
|
|
a[(i - 1)] = (a)[i];
|
|
a[i] = tmp;
|
|
}
|
|
}
|
|
i = (i + 1);
|
|
}
|
|
}
|
|
n = (n - 1);
|
|
}
|
|
return a;
|
|
}
|
|
}
|
|
}
|
|
|
|
// binary-search
|
|
function run_binary_search() {
|
|
function binarySearch(xs, target, low, high) {
|
|
if (low > high) {
|
|
return -1;
|
|
} else {
|
|
{
|
|
let mid = Math.floor((() => {
|
|
const __div_3 = (low + high);
|
|
const __div_4 = 2;
|
|
if (__div_4 === 0) throw new Error("division by zero");
|
|
return (__div_3 / __div_4);
|
|
})());
|
|
let value = (xs)[mid];
|
|
const __cond_value_5 = (value === target);
|
|
if (__cond_value_5 !== false) {
|
|
return mid;
|
|
} else {
|
|
const __cond_value_6 = (value < target);
|
|
if (__cond_value_6 !== false) {
|
|
return binarySearch(xs, target, (mid + 1), high);
|
|
} else {
|
|
return binarySearch(xs, target, low, (mid - 1));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// map-count-occurrences
|
|
function run_map_count_occurrences() {
|
|
function countOccurrences(xs) {
|
|
{
|
|
let counts = new Map();
|
|
for (const x of xs) {
|
|
if ((counts.has(x) !== false)) {
|
|
counts.set(x, (counts.get(x) + 1));
|
|
} else {
|
|
counts.set(x, 1);
|
|
}
|
|
}
|
|
return Array.from(counts.entries());
|
|
}
|
|
}
|
|
}
|
|
|
|
// get-html-three-ways
|
|
function run_get_html_three_ways() {
|
|
function getHtmlThreeWays() {
|
|
return [document.body.innerHTML, document.querySelector("body").innerHTML, document.getElementById("root")["innerHTML"]];
|
|
}
|
|
}
|
|
|
|
// anagram
|
|
function run_anagram() {
|
|
function sortChars(s) {
|
|
return s.split("").sort().join("");
|
|
}
|
|
function canArrange(stringA, stringB) {
|
|
return (() => {
|
|
const __cmp_8 = sortChars(stringA);
|
|
const __cmp_9 = sortChars(stringB);
|
|
return (__cmp_8 === __cmp_9);
|
|
})();
|
|
}
|
|
}
|
|
|
|
// pairs-equal-target
|
|
function run_pairs_equal_target() {
|
|
function pairsEqualTarget(xs, target) {
|
|
{
|
|
let seen = new Set();
|
|
let used = new Set();
|
|
let out = [];
|
|
for (const x of xs) {
|
|
{
|
|
let y = (target - x);
|
|
if (((() => {
|
|
let __and_value_11 = seen.has(y);
|
|
if (__and_value_11 === false) return false;
|
|
__and_value_11 = (used.has(x) === false);
|
|
if (__and_value_11 === false) return false;
|
|
return (used.has(y) === false);
|
|
})() !== false)) {
|
|
out.push([y, x]);
|
|
used.add(x);
|
|
used.add(y);
|
|
} else {
|
|
seen.add(x);
|
|
}
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
}
|
|
}
|
|
|
|
// fetch-api
|
|
function run_fetch_api() {
|
|
function loadTitle(url) {
|
|
return fetch(url).then(function(response) {
|
|
return response.json();
|
|
}).then(function(data) {
|
|
return {ok: true, title: data.title};
|
|
}).catch(function(err) {
|
|
return {ok: false, message: err.message};
|
|
});
|
|
}
|
|
}
|