[Challenge] implementing the ambiguous operator in D

Nick Sabalausky a at a.a
Thu Sep 2 14:51:30 PDT 2010


"Philippe Sigaud" <philippe.sigaud at gmail.com> wrote in message 
news:mailman.42.1283371696.858.digitalmars-d at puremagic.com...
> Hey, this question on SO makes for a good challenge:
>
> http://stackoverflow.com/questions/3608834/is-it-possible-to-generically-implement-the-amb-operator-in-d
>
> The amb operator does this:
>
> amb([1, 2]) * amb([3, 4, 5]) == amb([3, 4, 5, 6, 8, 10])
> amb(["hello", "world"]) ~ amb(["qwerty"]) == amb(["helloqwerty", 
> "worldqwerty"])
> amb(["hello", "world"]) ~ "qwerty" == amb(["helloqwerty", "worldqwerty"])
> amb(["hello", "very long string"]).length = amb([5, 16])
>

Interesting thing, but devil's advocate: What would be the uses/benefits of 
that versus just having "for each element" versions of the operators?

Also, "ambiguous" seems like a rather odd name for what it does. I don't see 
how ambiguity has anything to do with it. That disconnect made it difficult 
at first for me to understand what it was. It's more like an "elementwise" 
or something.

Certainly useful, I had reason to make a similar function once:

/// ctfe_subMapJoin("Hi WHO. ", "WHO", ["Joey", "Q", "Sue"])
/// --> "Hi Joey. Hi Q. Hi Sue. "
T ctfe_subMapJoin(T)(T str, T match, T[] replacements) if(isSomeString!T)
{
    T value = "";
    foreach(T replace; replacements)
        value ~= ctfe_substitute(str, match, replace);

    return value;
}

Though I'll grant that's a questionable name for it. I wasn't sure what else 
to call it though.




More information about the Digitalmars-d mailing list