RosettaCodeData/Task/Amb/Elena/amb.elena

83 lines
1.5 KiB
Text
Raw Permalink Normal View History

2019-09-12 10:33:56 -07:00
import system'routines;
import extensions;
import extensions'routines;
2015-02-20 09:02:09 -05:00
2019-09-12 10:33:56 -07:00
joinable(former,later) = (former[former.Length - 1] == later[0]);
2015-02-20 09:02:09 -05:00
2017-09-23 10:01:46 +02:00
dispatcher =
2015-02-20 09:02:09 -05:00
{
2018-08-17 15:15:24 +01:00
eval(object a, Func2 f)
2019-09-12 10:33:56 -07:00
{
^ f(a[0],a[1])
}
2015-02-20 09:02:09 -05:00
2018-08-17 15:15:24 +01:00
eval(object a, Func3 f)
2019-09-12 10:33:56 -07:00
{
^ f(a[0], a[1],a[2])
}
2015-02-20 09:02:09 -05:00
2018-08-17 15:15:24 +01:00
eval(object a, Func4 f)
2019-09-12 10:33:56 -07:00
{
^ f(a[0],a[1],a[2],a[3])
}
2015-02-20 09:02:09 -05:00
2018-08-17 15:15:24 +01:00
eval(object a, Func5 f)
2019-09-12 10:33:56 -07:00
{
^ f(a[0],a[1],a[2],a[3],a[4])
}
};
2015-02-20 09:02:09 -05:00
2017-09-23 10:01:46 +02:00
class AmbValueCollection
2015-02-20 09:02:09 -05:00
{
2019-09-12 10:33:56 -07:00
object theCombinator;
2015-02-20 09:02:09 -05:00
2019-09-12 10:33:56 -07:00
constructor new(params object[] args)
{
theCombinator := SequentialEnumerator.new(params args)
}
2015-02-20 09:02:09 -05:00
2019-09-12 10:33:56 -07:00
seek(cond)
{
theCombinator.reset();
2015-02-20 09:02:09 -05:00
2019-09-12 10:33:56 -07:00
theCombinator.seekEach:(v => dispatcher.eval(v,cond))
}
2015-02-20 09:02:09 -05:00
2019-09-12 10:33:56 -07:00
do(f)
{
var result := theCombinator.get();
if (nil != result)
{
dispatcher.eval(result,f)
}
else
{
InvalidArgumentException.raise()
}
}
2015-02-20 09:02:09 -05:00
}
2019-09-12 10:33:56 -07:00
singleton ambOperator
2015-02-20 09:02:09 -05:00
{
2019-09-12 10:33:56 -07:00
for(params object[] args)
= AmbValueCollection.new(params args);
}
2015-02-20 09:02:09 -05:00
2019-09-12 10:33:56 -07:00
public program()
{
try
2017-09-23 10:01:46 +02:00
{
2019-09-12 10:33:56 -07:00
ambOperator
2020-02-17 23:21:07 -08:00
.for(new::("the","that","a"),new::("frog", "elephant", "thing"),new::("walked", "treaded", "grows"),
new::("slowly", "quickly"))
2019-09-12 10:33:56 -07:00
.seek:(a,b,c,d => joinable(a,b) && joinable(b,c) && joinable(c,d) )
.do:(a,b,c,d) { console.printLine(a," ",b," ",c," ",d) }
}
catch(Exception e)
{
console.printLine:"AMB is angry"
};
2017-09-23 10:01:46 +02:00
2019-09-12 10:33:56 -07:00
console.readChar()
}