Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
53
Task/Amb/JavaScript/amb-1.js
Normal file
53
Task/Amb/JavaScript/amb-1.js
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
function ambRun(func) {
|
||||
var choices = [];
|
||||
var index;
|
||||
|
||||
function amb(values) {
|
||||
if (values.length == 0) {
|
||||
fail();
|
||||
}
|
||||
if (index == choices.length) {
|
||||
choices.push({i: 0,
|
||||
count: values.length});
|
||||
}
|
||||
var choice = choices[index++];
|
||||
return values[choice.i];
|
||||
}
|
||||
|
||||
function fail() { throw fail; }
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
index = 0;
|
||||
return func(amb, fail);
|
||||
} catch (e) {
|
||||
if (e != fail) {
|
||||
throw e;
|
||||
}
|
||||
var choice;
|
||||
while ((choice = choices.pop()) && ++choice.i == choice.count) {}
|
||||
if (choice == undefined) {
|
||||
return undefined;
|
||||
}
|
||||
choices.push(choice);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ambRun(function(amb, fail) {
|
||||
function linked(s1, s2) {
|
||||
return s1.slice(-1) == s2.slice(0, 1);
|
||||
}
|
||||
|
||||
var w1 = amb(["the", "that", "a"]);
|
||||
var w2 = amb(["frog", "elephant", "thing"]);
|
||||
if (!linked(w1, w2)) fail();
|
||||
|
||||
var w3 = amb(["walked", "treaded", "grows"]);
|
||||
if (!linked(w2, w3)) fail();
|
||||
|
||||
var w4 = amb(["slowly", "quickly"]);
|
||||
if (!linked(w3, w4)) fail();
|
||||
|
||||
return [w1, w2, w3, w4].join(' ');
|
||||
}); // "that thing grows slowly"
|
||||
55
Task/Amb/JavaScript/amb-2.js
Normal file
55
Task/Amb/JavaScript/amb-2.js
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// amb :: [a] -> (a -> [b]) -> [b]
|
||||
const amb = xs => f =>
|
||||
xs.reduce((a, x) => a.concat(f(x)), []);
|
||||
|
||||
// when :: Bool -> [a] -> [a]
|
||||
const when = p =>
|
||||
xs => p ? (
|
||||
xs
|
||||
) : [];
|
||||
|
||||
|
||||
// TEST -----------------------------------------------
|
||||
const main = () => {
|
||||
|
||||
// joins :: String -> String -> Bool
|
||||
const joins = (a, b) =>
|
||||
b[0] === last(a);
|
||||
|
||||
console.log(
|
||||
amb(['the', 'that', 'a'])
|
||||
(w1 => when(true)(
|
||||
|
||||
amb(['frog', 'elephant', 'thing'])
|
||||
(w2 => when(joins(w1, w2))(
|
||||
|
||||
amb(['walked', 'treaded', 'grows'])
|
||||
(w3 => when(joins(w2, w3))(
|
||||
|
||||
amb(['slowly', 'quickly'])
|
||||
(w4 => when(joins(w3, w4))(
|
||||
|
||||
unwords([w1, w2, w3, w4])
|
||||
|
||||
))
|
||||
))
|
||||
))
|
||||
))
|
||||
);
|
||||
};
|
||||
|
||||
// GENERIC FUNCTIONS ----------------------------------
|
||||
|
||||
// last :: [a] -> a
|
||||
const last = xs =>
|
||||
0 < xs.length ? xs.slice(-1)[0] : undefined;
|
||||
|
||||
// unwords :: [String] -> String
|
||||
const unwords = xs => xs.join(' ');
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue