Amb: Difference between revisions

14 bytes removed ,  1 month ago
(→‎Insitux: improvement)
(10 intermediate revisions by 6 users not shown)
Line 910:
</pre>
 
Alternatively, without using===Without external libraries.===
<syntaxhighlight lang="c++">
#include <functional>
Line 920:
std::string join(const std::string& delimiter, const std::vector<std::string>& list) {
return list.empty() ? "" : std::accumulate(++list.begin(), list.end(), list[0],
[delimiter](auto& a, auto& b) { return a + delimiter + b; });
}
 
Line 1,414:
 
=={{header|Elena}}==
ELENA 56.0 :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
import extensions'routines;
 
// --- Joinable --
 
joinable(former,later) = (former[former.Length - 1] == later[0]);
Line 1,443 ⟶ 1,445:
}
};
 
// --- AmbValueCollection ---
 
class AmbValueCollection
{
object theCombinator_combinator;
constructor new(params object[] args)
{
theCombinator_combinator := SequentialEnumerator.newload(params args)
}
 
seek(cond)
{
theCombinator_combinator.reset();
 
theCombinator_combinator.seekEach::(v => dispatcher.eval(v,cond))
}
do(f)
{
var result := theCombinator.get()*_combinator;
if (nil != result)
{
Line 1,473 ⟶ 1,477:
}
}
 
// --- ambOperator ---
 
singleton ambOperator
Line 1,479 ⟶ 1,485:
= AmbValueCollection.new(params args);
}
 
// --- Program ---
 
public program()
Line 1,490 ⟶ 1,498:
new object[]{"walked", "treaded", "grows"},
new object[]{"slowly", "quickly"})
.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")
};
Line 2,440 ⟶ 2,448:
 
=={{header|langur}}==
{{works with|langur|0.11}}
This would build every valid set, but for the sample data, there's only one.
 
<syntaxhighlight lang="langur">val .wordsets = [
wfw/the that a/,
wfw/frog elephant thing/,
wfw/walked treaded grows/,
wfw/slowly quickly/,
]
 
val .alljoin = ffn(.words) { for[=true] .i of len(.words)-1 {
if last(.words[.i]) != first(.words[.i+1]): break = false
}}
 
# .amb expects 2 or more arguments
val .amb = ffn(...[2 .. -1] .words) { if(.alljoin(.words): join " ", .words) }
 
writeln join "\n", filter( mapX( .amb, .wordsets...))</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 5,280 ⟶ 5,288:
uBasic/4tH has limited support for arrays, so some workarounds are required to make this work.
<syntaxhighlight lang="text"> ' set up the arrays
Push Dup("the"), Dup("that"), Dup("a") : a = FUNC(_Ambsel (0))
Push Dup("frog"), Dup("elephant"), Dup("thing") : b = FUNC(_Ambsel (a))
Push Dup("walked"), Dup("treaded"), Dup("grows") : c = FUNC(_Ambsel (b))
Push Dup("slowly"), Dup("quickly") : f = FUNC(_Ambsel (c))
' we'll reuse variable f ;-)
Proc _Ambassert (_Connect) ' now assert the function required
Line 5,319 ⟶ 5,327:
0 OK, 0:772
</pre>
 
=={{header|VBScript}}==
=====Implementation=====
Line 5,399 ⟶ 5,408:
{{trans|Go}}
Based on the 'alternative' version.
<syntaxhighlight lang="ecmascriptwren">var finalRes = []
 
var amb // recursive closure
Line 5,424 ⟶ 5,433:
[ "walked", "treaded", "grows" ],
[ "slowly", "quickly" ]
]
}
 
if (amb.call(wordsets, [])) {
890

edits