Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

152
Task/Amb/C-sharp/amb-1.cs Normal file
View file

@ -0,0 +1,152 @@
using System;
using System.Collections.Generic;
public class Amb : IDisposable
{
List<IValueSet> streams = new List<IValueSet>();
List<IAssertOrAction> assertsOrActions = new List<IAssertOrAction>();
volatile bool stopped = false;
public IAmbValue<T> DefineValues<T>(params T[] values)
{
return DefineValueSet(values);
}
public IAmbValue<T> DefineValueSet<T>(IEnumerable<T> values)
{
ValueSet<T> stream = new ValueSet<T>();
stream.Enumerable = values;
streams.Add(stream);
return stream;
}
public Amb Assert(Func<bool> function)
{
assertsOrActions.Add(new AmbAssert()
{
Level = streams.Count,
IsValidFunction = function
});
return this;
}
public Amb Perform(Action action)
{
assertsOrActions.Add(new AmbAction()
{
Level = streams.Count,
Action = action
});
return this;
}
public void Stop()
{
stopped = true;
}
public void Dispose()
{
RunLevel(0, 0);
if (!stopped)
{
throw new AmbException();
}
}
void RunLevel(int level, int actionIndex)
{
while (actionIndex < assertsOrActions.Count && assertsOrActions[actionIndex].Level <= level)
{
if (!assertsOrActions[actionIndex].Invoke() || stopped)
return;
actionIndex++;
}
if (level < streams.Count)
{
using (IValueSetIterator iterator = streams[level].CreateIterator())
{
while (iterator.MoveNext())
{
RunLevel(level + 1, actionIndex);
}
}
}
}
interface IValueSet
{
IValueSetIterator CreateIterator();
}
interface IValueSetIterator : IDisposable
{
bool MoveNext();
}
interface IAssertOrAction
{
int Level { get; }
bool Invoke();
}
class AmbAssert : IAssertOrAction
{
internal int Level;
internal Func<bool> IsValidFunction;
int IAssertOrAction.Level { get { return Level; } }
bool IAssertOrAction.Invoke()
{
return IsValidFunction();
}
}
class AmbAction : IAssertOrAction
{
internal int Level;
internal Action Action;
int IAssertOrAction.Level { get { return Level; } }
bool IAssertOrAction.Invoke()
{
Action(); return true;
}
}
class ValueSet<T> : IValueSet, IAmbValue<T>, IValueSetIterator
{
internal IEnumerable<T> Enumerable;
private IEnumerator<T> enumerator;
public T Value { get { return enumerator.Current; } }
public IValueSetIterator CreateIterator()
{
enumerator = Enumerable.GetEnumerator();
return this;
}
public bool MoveNext()
{
return enumerator.MoveNext();
}
public void Dispose()
{
enumerator.Dispose();
}
}
}
public interface IAmbValue<T>
{
T Value { get; }
}
public class AmbException : Exception
{
public AmbException() : base("AMB is angry") { }
}

30
Task/Amb/C-sharp/amb-2.cs Normal file
View file

@ -0,0 +1,30 @@
// original problem
using (Amb amb = new Amb())
{
var set1 = amb.DefineValues("the", "that", "a");
var set2 = amb.DefineValues("frog", "elephant", "thing");
var set3 = amb.DefineValues("walked", "treaded", "grows");
var set4 = amb.DefineValues("slowly", "quickly");
amb.Assert(() => IsJoinable(set1.Value, set2.Value));
amb.Assert(() => IsJoinable(set2.Value, set3.Value));
amb.Assert(() => IsJoinable(set3.Value, set4.Value));
amb.Perform(() =>
{
System.Console.WriteLine("{0} {1} {2} {3}", set1.Value, set2.Value, set3.Value, set4.Value);
amb.Stop();
});
}
// problem from http://www.randomhacks.net/articles/2005/10/11/amb-operator
using (Amb amb = new Amb())
{
IAmbValue<int> x = amb.DefineValues(1, 2, 3);
IAmbValue<int> y = amb.DefineValues(4, 5, 6);
amb.Assert(() => x.Value * y.Value == 8);
amb.Perform(() =>
{
System.Console.WriteLine("{0} {1}", x.Value, y.Value);
amb.Stop();
});
}

73
Task/Amb/C-sharp/amb-3.cs Normal file
View file

@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
namespace Amb {
public interface IValue<T> {
T Value { get; }
string ToString();
}
public sealed class Amb {
public IValue<T> Choose<T>(params T[] choices) {
var array = new ChoiceArray<T> { Values = choices };
_choices.Add(array);
return array;
}
public void Require(Func<bool> predicate) =>
_constraints.Add(new Constraint { Predicate = predicate, AppliesForItems = _choices.Count });
public bool Disambiguate() => Disambiguate(0, 0);
interface IChoices {
int Length { get; }
int Index { get; set; }
}
interface IConstraint {
int AppliesForItems { get; }
bool Invoke();
}
readonly List<IChoices> _choices = new();
readonly List<IConstraint> _constraints = new();
bool Disambiguate(int choicesTracked, int constraintIdx) {
while (constraintIdx < _constraints.Count && _constraints[constraintIdx].AppliesForItems <= choicesTracked) {
if (!_constraints[constraintIdx].Invoke())
return false;
constraintIdx++;
}
if (choicesTracked == _choices.Count)
return true;
for (var i = 0; i < _choices[choicesTracked].Length; i++) {
_choices[choicesTracked].Index = i;
if (Disambiguate(choicesTracked + 1, constraintIdx))
return true;
}
return false;
}
class Constraint : IConstraint {
internal Func<bool> Predicate;
public int AppliesForItems { get; set; }
public bool Invoke() => Predicate?.Invoke() ?? default;
}
class ChoiceArray<T> : IChoices, IValue<T> {
internal T[] Values;
public int Index { get; set; }
public T Value => Values[Index];
public int Length => Values.Length;
public override string ToString() => Value.ToString();
}
}
}

32
Task/Amb/C-sharp/amb-4.cs Normal file
View file

@ -0,0 +1,32 @@
using System.Linq;
using static System.Console;
namespace Amb {
class Program {
static void Main(string[] args) {
var amb = new Amb();
var set1 = amb.Choose("the", "that", "a");
var set2 = amb.Choose("frog", "elephant", "thing");
amb.Require(() => set1.Value.Last() == set2.Value[0]);
var set3 = amb.Choose("walked", "treaded", "grows");
amb.Require(() => set2.Value.Last() == set3.Value[0]);
var set4 = amb.Choose("slowly", "quickly");
amb.Require(() => set3.Value.Last() == set4.Value[0]);
WriteLine(amb.Disambiguate()? $"{set1} {set2} {set3} {set4}" : "Amb failed");
Read();
// problem from http://www.randomhacks.net/articles/2005/10/11/amb-operator
amb = new Amb();
var x = amb.Choose(1, 2, 3);
var y = amb.Choose(4, 5, 6);
amb.Require(() => x.Value * y.Value == 8);
WriteLine(amb.Disambiguate() ? $"{x} * {y} = 8" : "Amb failed");
Read();
}
}
}

22
Task/Amb/C-sharp/amb-5.cs Normal file
View file

@ -0,0 +1,22 @@
using static System.Console;
using System.Linq;
string[] w1 = { "the", "that", "a" };
string[] w2 = { "frog", "elephant", "thing" };
string[] w3 = { "walked", "treaded", "grows" };
string[] w4 = { "slowly", "quickly" };
var result = from a in w1
join b in w2 on a?.LastOrDefault() equals b?.FirstOrDefault()
join c in w3 on b?.LastOrDefault() equals c?.FirstOrDefault()
join d in w4 on c?.LastOrDefault() equals d?.FirstOrDefault()
select new [] {a, b, c, d};
WriteLine(string.Join(" ", result.SelectMany(x => x)));
double[] x = { 1, 2, 3 };
double[] y = { 7, 6, 4, 5 };
var result2 = from a in x
join b in y on a equals 8 / b
select new[] { a, b };
WriteLine(string.Join(" ", result2.SelectMany(x => x)));