Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.111.0

overview

Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinga local clone.

std.algorithm.comparison

This is a submodule ofstd.algorithm.It contains generic comparison algorithms.
Cheat Sheet
Function NameDescription
among Checks if a value is among a set of values, e.g.if (v.among(1, 2, 3)) //v is 1, 2 or 3
castSwitch(new A()).castSwitch((A a)=>1,(B b)=>2) returns1.
clampclamp(1, 3, 6) returns3.clamp(4, 3, 6) returns4.
cmpcmp("abc", "abcd") is-1,cmp("abc", "aba") is1, andcmp("abc", "abc") is0.
either Return first parameterp that passes anif (p) test, e.g.either(0, 42, 43) returns42.
equal Compares ranges for element-by-element equality, e.g.equal([1, 2, 3], [1.0, 2.0, 3.0]) returnstrue.
isPermutationisPermutation([1, 2], [2, 1]) returnstrue.
isSameLengthisSameLength([1, 2, 3], [4, 5, 6]) returnstrue.
levenshteinDistancelevenshteinDistance("kitten", "sitting") returns3 by using the Levenshtein distance algorithm.
levenshteinDistanceAndPathlevenshteinDistanceAndPath("kitten", "sitting") returnstuple(3, "snnnsni") by using the Levenshtein distance algorithm.
maxmax(3, 4, 2) returns4.
minmin(3, 4, 2) returns2.
mismatchmismatch("oh hi", "ohayo") returnstuple(" hi", "ayo").
predSwitch2.predSwitch(1, "one", 2, "two", 3, "three") returns"two".
License:
Boost License 1.0.
Authors:
Andrei Alexandrescu

Sourcestd/algorithm/comparison.d

uintamong(alias pred = (a, b) => a == b, Value, Values...)(Valuevalue, Valuesvalues)
if (Values.length != 0);

templateamong(values...) if (isExpressionTuple!values)
Findvalue amongvalues, returning the 1-based indexof the first matching value invalues, or0 ifvalueis not amongvalues. The predicatepred is used tocompare values, and uses equality by default.
Parameters:
predThe predicate used to compare the values.
ValuevalueThe value to search for.
ValuesvaluesThe values to compare the value to.
Returns:
0 if value was not found among the values, otherwise the index of the found value plus one is returned.
See Also:
find andcanFind for finding a value in arange.
Examples:
assert(3.among(1, 42, 24, 3, 2));if (auto pos ="bar".among("foo","bar","baz"))    writeln(pos);// 2elseassert(false);// 42 is larger than 24writeln(42.among!((lhs, rhs) => lhs > rhs)(43, 24, 100));// 2
Examples:
Alternatively,values can be passed at compile-time, allowing for a moreefficient search, but one that only supports matching on equality:
assert(3.among!(2, 3, 4));writeln("bar".among!("foo","bar","baz"));// 2
autocastSwitch(choices...)(ObjectswitchObject);
Executes and returns one of a collection of handlers based on the type of theswitch object.
The first choice thatswitchObject can be casted to the typeof argument it accepts will be called withswitchObject casted to thattype, and the value it'll return will be returned bycastSwitch.
If a choice's return type is void, the choice must throw an exception, unlessall the choices are void. In that case, castSwitch itself will return void.
Throws:
If none of the choice matches, aSwitchError will be thrown.SwitchError will also be thrown if not all the choices are void and a voidchoice was executed without throwing anything.
Parameters:
choicesThechoices needs to be composed of function or delegate handlers that accept one argument. There can also be a choice that accepts zero arguments. That choice will be invoked if the switchObject is null.
ObjectswitchObjectthe object against which the tests are being made.
Returns:
The value of the selected choice.

NotecastSwitch can only be used with object types.

Examples:
import std.algorithm.iteration : map;import std.format : format;class A{int a;this(int a) {this.a = a;}    @propertyint i() {return a; }}interface I { }class B : I { }Object[] arr = [new A(1),new B(),null];auto results = arr.map!(castSwitch!(                            (A a) =>"A with a value of %d".format(a.a),                            (I i) =>"derived from I",                            ()    =>"null reference",                        ))();// A is handled directly:writeln(results[0]);// "A with a value of 1"// B has no handler - it is handled by the handler of I:writeln(results[1]);// "derived from I"// null is handled by the null handler:writeln(results[2]);// "null reference"
Examples:
Using with void handlers:
import std.exception : assertThrown;class A { }class B { }// Void handlers are allowed if they throw:assertThrown!Exception(new B().castSwitch!(        (A a) => 1,        (B d)    {thrownew Exception("B is not allowed!"); }    )());// Void handlers are also allowed if all the handlers are void:new A().castSwitch!(    (A a) { },    (B b) {assert(false); },)();
T1clamp(T1, T2, T3)(T1val, T2lower, T3upper);
Clampsval into the given bounds. Result has the same type asval.
Parameters:
T1valThe value to clamp.
T2lowerThe lower bound of the clamp.
T3upperThe upper bound of the clamp.
Returns:
lower ifval is less thanlower,upper ifval is greater thanupper, andval in all other cases. Comparisons are made correctly (usingstd.functional.lessThan and the return value is converted to the return type using the standard integer coversion rulesstd.functional.greaterThan) even if the signedness ofT1,T2, andT3 are different.
Examples:
writeln(clamp(2, 1, 3));// 2writeln(clamp(0, 1, 3));// 1writeln(clamp(4, 1, 3));// 3writeln(clamp(1, 1, 1));// 1writeln(clamp(5, -1, 2u));// 2auto x =clamp(42,uint.max,uint.max);staticassert(is(typeof(x) ==int));writeln(x);// -1
autocmp(R1, R2)(R1r1, R2r2)
if (isInputRange!R1 && isInputRange!R2);

intcmp(alias pred, R1, R2)(R1r1, R2r2)
if (isInputRange!R1 && isInputRange!R2);
Performs a lexicographical comparison on twoinput ranges.Iteratingr1 andr2 in lockstep,cmp compares each elemente1 ofr1 with the corresponding elemente2 inr2. If oneof the ranges has been finished,cmp returns a negative valueifr1 has fewer elements thanr2, a positive value ifr1has more elements thanr2, and0 if the ranges have the samenumber of elements.
If the ranges are strings,cmp performs UTF decodingappropriately and compares the ranges one code point at a time.
A custom predicate may be specified, in which casecmp performsa three-way lexicographical comparison usingpred. Otherwisethe elements are compared usingopCmp.
Parameters:
predPredicate used for comparison. Without a predicate specified the ordering implied byopCmp is used.
R1r1The first range.
R2r2The second range.
Returns:
0 if the ranges compare equal. A negative value ifr1 is a prefix ofr2 or the first differing element ofr1 is less than the corresponding element ofr2 according topred. A positive value ifr2 is a prefix ofr1 or the first differing element ofr2 is less than the corresponding element ofr1 according topred.

NoteAn earlier version of the documentation incorrectly stated that-1 is the only negative value returned and1 is the only positive value returned. Whether that is true depends on the types being compared.

Examples:
int result;result =cmp("abc","abc");writeln(result);// 0result =cmp("","");writeln(result);// 0result =cmp("abc","abcd");assert(result < 0);result =cmp("abcd","abc");assert(result > 0);result =cmp("abc"d,"abd");assert(result < 0);result =cmp("bbc","abc"w);assert(result > 0);result =cmp("aaa","aaaa"d);assert(result < 0);result =cmp("aaaa","aaa"d);assert(result > 0);result =cmp("aaa","aaa"d);writeln(result);// 0result =cmp("aaa"d,"aaa"d);writeln(result);// 0result =cmp(cast(int[])[],cast(int[])[]);writeln(result);// 0result =cmp([1, 2, 3], [1, 2, 3]);writeln(result);// 0result =cmp([1, 3, 2], [1, 2, 3]);assert(result > 0);result =cmp([1, 2, 3], [1L, 2, 3, 4]);assert(result < 0);result =cmp([1L, 2, 3], [1, 2]);assert(result > 0);
Examples:
Example predicate that compares individual elements in reverse lexical order
int result;result =cmp!"a > b"("abc","abc");writeln(result);// 0result =cmp!"a > b"("","");writeln(result);// 0result =cmp!"a > b"("abc","abcd");assert(result < 0);result =cmp!"a > b"("abcd","abc");assert(result > 0);result =cmp!"a > b"("abc"d,"abd");assert(result > 0);result =cmp!"a > b"("bbc","abc"w);assert(result < 0);result =cmp!"a > b"("aaa","aaaa"d);assert(result < 0);result =cmp!"a > b"("aaaa","aaa"d);assert(result > 0);result =cmp!"a > b"("aaa","aaa"d);writeln(result);// 0result =cmp("aaa"d,"aaa"d);writeln(result);// 0result =cmp!"a > b"(cast(int[])[],cast(int[])[]);writeln(result);// 0result =cmp!"a > b"([1, 2, 3], [1, 2, 3]);writeln(result);// 0result =cmp!"a > b"([1, 3, 2], [1, 2, 3]);assert(result < 0);result =cmp!"a > b"([1, 2, 3], [1L, 2, 3, 4]);assert(result < 0);result =cmp!"a > b"([1L, 2, 3], [1, 2]);assert(result > 0);
templateequal(alias pred = "a == b")
Compares two or more ranges for equality, as defined by predicatepred(which is== by default).
Examples:
import std.algorithm.comparison :equal;import std.math.operations : isClose;int[4] a = [ 1, 2, 4, 3 ];assert(!equal(a[], a[1..$]));assert(equal(a[], a[]));assert(equal!((a, b) => a == b)(a[], a[]));// different typesdouble[4] b = [ 1.0, 2, 4, 3];assert(!equal(a[], b[1..$]));assert(equal(a[], b[]));// predicated: ensure that two vectors are approximately equaldouble[4] c = [ 1.0000000005, 2, 4, 3];assert(equal!isClose(b[], c[]));
Examples:
Tip:equal can itself be used as a predicate to other functions.This can be very useful when the element type of a range is itself arange. In particular,equal can be its own predicate, allowingrange of range (of range...) comparisons.
import std.algorithm.comparison :equal;import std.range : iota, chunks;assert(equal!(equal!equal)(    [[[0, 1], [2, 3]], [[4, 5], [6, 7]]],    iota(0, 8).chunks(2).chunks(2)));
boolequal(Ranges...)(Rangesrs)
if (rs.length > 1 && allSatisfy!(isInputRange, Ranges) && !allSatisfy!(isInfinite, Ranges) && is(typeof(binaryFun!pred(rs[0].front,rs[1].front))) && (rs.length == 2 || is(typeof(equal!pred(rs[1..$])) == bool)));
Compares two or more ranges for equality. The ranges may have different element types, as long as all are comparable by means of thepred. PerformsΟ(min(rs[0].length, rs[1].length, ...)) evaluations ofpred. However, ifequal is invoked with the default predicate, the implementation may take the liberty to use faster implementations that have the theoretical worst-caseΟ(max(rs[0].length, rs[1].length, ...)).
At least one of the ranges must be finite. If one range involved is infinite, the result is (statically known to be)false.
If the ranges have different kinds of UTF code unit (char,wchar, ordchar), then they are compared using UTF decoding to avoid accidentally integer-promoting units.
Parameters:
RangesrsThe ranges to be compared.
Returns:
true if and only if all ranges compare equal element for element, according to binary predicatepred.
enumEditOp: char;
Encodesedit operations necessary to transform one sequence intoanother. Given sequencess (source) andt (target), asequence ofEditOp encodes the steps that need to be taken toconverts intot. For example, ifs = "cat" and"cars", the minimal sequence that transformss intot is:skip two characters, replace 't' with 'r', and insert an 's'. Workingwith edit operations is useful in applications such as spell-checkers(to find the closest word to a given misspelled word), approximatesearches, diff-style programs that compute the difference betweenfiles, efficient encoding of patches, DNA sequence analysis, andplagiarism detection.
Examples:
with(EditOp){// [none, none, none, insert, insert, insert]    writeln(levenshteinDistanceAndPath("foo","foobar")[1]);// [substitute, none, substitute, none, none, remove]    writeln(levenshteinDistanceAndPath("banana","fazan")[1]);}
none
Current items are equal; no editing is necessary.
substitute
Substitute current item in target with current item in source.
insert
Insert current item from the source into the target.
remove
Remove current item from the target.
size_tlevenshteinDistance(alias equals = (a, b) => a == b, Range1, Range2)(Range1s, Range2t)
if (isForwardRange!Range1 && isForwardRange!Range2);

size_tlevenshteinDistance(alias equals = (a, b) => a == b, Range1, Range2)(auto ref Range1s, auto ref Range2t)
if (isConvertibleToString!Range1 || isConvertibleToString!Range2);
Returns theLevenshteindistance betweens andt. The Levenshtein distance computesthe minimal amount of edit operations necessary to transformsintot. PerformsΟ(s.length * t.length) evaluations ofequals and occupiesΟ(min(s.length, t.length)) storage.
Parameters:
equalsThe binary predicate to compare the elements of the two ranges.
Range1sThe original range.
Range2tThe transformation target
Returns:
The minimal number of edits to transform s into t.
Does not allocate GC memory.
Examples:
import std.algorithm.iteration : filter;import std.uni : toUpper;writeln(levenshteinDistance("cat","rat"));// 1writeln(levenshteinDistance("parks","spark"));// 2writeln(levenshteinDistance("abcde","abcde"));// 0writeln(levenshteinDistance("abcde","abCde"));// 1writeln(levenshteinDistance("kitten","sitting"));// 3assert(levenshteinDistance!((a, b) => toUpper(a) == toUpper(b))    ("parks","SPARK") == 2);writeln(levenshteinDistance("parks".filter!"true","spark".filter!"true"));// 2writeln(levenshteinDistance("ID","I♥D"));// 1
Tuple!(size_t, EditOp[])levenshteinDistanceAndPath(alias equals = (a, b) => a == b, Range1, Range2)(Range1s, Range2t)
if (isForwardRange!Range1 && isForwardRange!Range2);

Tuple!(size_t, EditOp[])levenshteinDistanceAndPath(alias equals = (a, b) => a == b, Range1, Range2)(auto ref Range1s, auto ref Range2t)
if (isConvertibleToString!Range1 || isConvertibleToString!Range2);
Returns the Levenshtein distance and the edit path betweens andt.
Parameters:
equalsThe binary predicate to compare the elements of the two ranges.
Range1sThe original range.
Range2tThe transformation target
Returns:
Tuple with the first element being the minimal amount of edits to transform s into t and the second element being the sequence of edits to effect this transformation.
Allocates GC memory for the returned EditOp[] array.
Examples:
string a ="Saturday", b ="Sundays";auto p =levenshteinDistanceAndPath(a, b);writeln(p[0]);// 4assert(equal(p[1],"nrrnsnnni"));
automax(T...)(Targs)
if (T.length >= 2 && !is(CommonType!T == void));

Tmax(T, U)(Ta, Ub)
if (is(T == U) && is(typeof(a <b)));
Iterates the passed arguments and returns the maximum value.
Parameters:
TargsThe values to select the maximum from. At least two arguments must be passed, and they must be comparable with<.
Returns:
The maximum of the passed-in values. The type of the returned value is the type among the passed arguments that is able to store the largest value. If at least one of the arguments is NaN, the result is an unspecified value. Seestd.algorithm.searching.maxElement for examples on how to cope with NaNs.
See Also:
Examples:
inta = 5;shortb = 6;double c = 2;auto d =max(a,b);assert(is(typeof(d) ==int));writeln(d);// 6auto e = min(a,b, c);assert(is(typeof(e) ==double));writeln(e);// 2
automin(T...)(Targs)
if (T.length >= 2 && !is(CommonType!T == void));

Tmin(T, U)(Ta, Ub)
if (is(T == U) && is(typeof(a <b)));
Iterates the passed arguments and returns the minimum value.
Parameters:
TargsThe values to select the minimum from. At least two arguments must be passed, and they must be comparable with<.
Returns:
The minimum of the passed-in values. The type of the returned value is the type among the passed arguments that is able to store the smallest value. If at least one of the arguments is NaN, the result is an unspecified value. Seestd.algorithm.searching.minElement for examples on how to cope with NaNs.
See Also:
Examples:
inta = 5;shortb = 6;double c = 2;auto d =min(a,b);staticassert(is(typeof(d) ==int));writeln(d);// 5auto e =min(a,b, c);staticassert(is(typeof(e) ==double));writeln(e);// 2ulong f = 0xffff_ffff_ffff;constuint g =min(f, 0xffff_0000);writeln(g);// 0xffff_0000dchar h = 100;uint i = 101;staticassert(is(typeof(min(h, i)) ==dchar));staticassert(is(typeof(min(i, h)) ==uint));writeln(min(h, i));// 100
Examples:
With arguments of mixed signedness, the return type is the one that canstore the lowest values.
inta = -10;uint f = 10;staticassert(is(typeof(min(a, f)) ==int));writeln(min(a, f));// -10
Examples:
User-defined types that support comparison with < are supported.
import std.datetime;writeln(min(Date(2012, 12, 21), Date(1982, 1, 4)));// Date(1982, 1, 4)writeln(min(Date(1982, 1, 4), Date(2012, 12, 21)));// Date(1982, 1, 4)writeln(min(Date(1982, 1, 4), Date.min));// Date.minwriteln(min(Date.min, Date(1982, 1, 4)));// Date.minwriteln(min(Date(1982, 1, 4), Date.max));// Date(1982, 1, 4)writeln(min(Date.max, Date(1982, 1, 4)));// Date(1982, 1, 4)writeln(min(Date.min, Date.max));// Date.minwriteln(min(Date.max, Date.min));// Date.min
Tuple!Rangesmismatch(alias pred = (a, b) => a == b, Ranges...)(Rangesrs)
if (rs.length >= 2 && allSatisfy!(isInputRange, Ranges));
Sequentially compares elements inrs in lockstep, andstops at the first mismatch (according topred, by defaultequality). Returns a tuple with the reduced ranges that start with thetwo mismatched values. PerformsΟ(min(r[0].length, r[1].length, ...))evaluations ofpred.
Examples:
int[6] x = [ 1,   5, 2, 7,   4, 3 ];double[6] y = [ 1.0, 5, 2, 7.3, 4, 8 ];auto m =mismatch(x[], y[]);writeln(m[0]);// x[3 .. $]writeln(m[1]);// y[3 .. $]auto m2 =mismatch(x[], y[], x[], y[]);writeln(m2[0]);// x[3 .. $]writeln(m2[1]);// y[3 .. $]writeln(m2[2]);// x[3 .. $]writeln(m2[3]);// y[3 .. $]
autopredSwitch(alias pred = "a == b", T, R...)(TswitchExpression, lazy Rchoices);
Returns one of a collection of expressions based on the value of the switchexpression.
choices needs to be composed of pairs of test expressions and returnexpressions. Each test-expression is compared withswitchExpression usingpred(switchExpression is the first argument) and if that yields true -the return expression is returned.
Both the test and the return expressions are lazily evaluated.
Parameters:
TswitchExpressionThe first argument for the predicate.
RchoicesPairs of test expressions and return expressions. The testexpressions will be the second argument for the predicate, and the returnexpression will be returned if the predicate yields true withswitchExpression and the test expression as arguments. May also have adefault return expression, that needs to be the last expression without a testexpression before it. A return expression may be of void type only if italways throws.
Returns:
The return expression associated with the first test expression thatmade the predicate yield true, or the default return expression if no testexpression matched.
Throws:
If there is no default return expression and the predicate does notyield true with any test expression -SwitchError is thrown.SwitchError is also thrown if a void return expression was executed withoutthrowing anything.
Examples:
string res = 2.predSwitch!"a < b"(    1,"less than 1",    5,"less than 5",    10,"less than 10","greater or equal to 10");writeln(res);// "less than 5"//The arguments are lazy, which allows us to use predSwitch to create//recursive functions:int factorial(int n){return n.predSwitch!"a <= b"(        -1, {thrownew Exception("Can not calculate n! for n < 0");}(),        0, 1,// 0! = 1        n * factorial(n - 1)// n! = n * (n - 1)! for n >= 0        );}writeln(factorial(3));// 6//Void return expressions are allowed if they always throw:import std.exception : assertThrown;assertThrown!Exception(factorial(-9));
boolisSameLength(Ranges...)(Rangesrs)
if (allSatisfy!(isInputRange, Ranges));
Checks if two or more ranges have the same number of elements. This function isoptimized to always take advantage of thelength member of either rangeif it exists.
If all ranges have alength member or at least one is infinite,_isSameLength's complexity isΟ(1). Otherwise, complexity isΟ(n), wheren is the smallest of the lengths of ranges with unknownlength.
Infinite ranges are considered of the same length. An infinite range has neverthe same length as a finite range.
Parameters:
Rangesrstwo or moreinput ranges
Returns:
true if both ranges have the same length,false otherwise.
Examples:
assert(isSameLength([1, 2, 3], [4, 5, 6]));assert(isSameLength([1, 2, 3], [4, 5, 6], [7, 8, 9]));assert(isSameLength([0.3, 90.4, 23.7, 119.2], [42.6, 23.6, 95.5, 6.3]));assert(isSameLength("abc","xyz"));assert(isSameLength("abc","xyz", [1, 2, 3]));int[] a;int[] b;assert(isSameLength(a, b));assert(isSameLength(a, b, a, a, b, b, b));assert(!isSameLength([1, 2, 3], [4, 5]));assert(!isSameLength([1, 2, 3], [4, 5, 6], [7, 8]));assert(!isSameLength([0.3, 90.4, 23.7], [42.6, 23.6, 95.5, 6.3]));assert(!isSameLength("abcd","xyz"));assert(!isSameLength("abcd","xyz","123"));assert(!isSameLength("abcd","xyz","1234"));
boolisPermutation(Flag!"allocateGC" allocateGC, Range1, Range2)(Range1r1, Range2r2)
if (allocateGC == Yes.allocateGC && isForwardRange!Range1 && isForwardRange!Range2 && !isInfinite!Range1 && !isInfinite!Range2);

boolisPermutation(alias pred = "a == b", Range1, Range2)(Range1r1, Range2r2)
if (is(typeof(binaryFun!pred)) && isForwardRange!Range1 && isForwardRange!Range2 && !isInfinite!Range1 && !isInfinite!Range2);
Checks if both ranges are permutations of each other.
This function can allocate if theYes.allocateGC flag is passed. This hasthe benefit of have better complexity than theYes.allocateGC option. However,this option is only available for ranges whose equality can be determined via eachelement'stoHash method. If customized equality is needed, then thepredtemplate parameter can be passed, and the function will automatically switch tothe non-allocating algorithm. Seestd.functional.binaryFun for more details onhow to definepred.
Non-allocating forward range option:Ο(n^2)Non-allocating forward range option with custompred:Ο(n^2)Allocating forward range option: amortizedΟ(r1.length) +Ο(r2.length)
Parameters:
predan optional parameter to change how equality is defined
allocateGCYes.allocateGC/No.allocateGC
Range1r1A finiteforward range
Range2r2A finiteforward range
Returns:
true if all of the elements inr1 appear the same number of times inr2. Otherwise, returnsfalse.
Examples:
import std.typecons : Yes;assert(isPermutation([1, 2, 3], [3, 2, 1]));assert(isPermutation([1.1, 2.3, 3.5], [2.3, 3.5, 1.1]));assert(isPermutation("abc","bca"));assert(!isPermutation([1, 2], [3, 4]));assert(!isPermutation([1, 1, 2, 3], [1, 2, 2, 3]));assert(!isPermutation([1, 1], [1, 1, 1]));// Faster, but allocates GC handled memoryassert(isPermutation!(Yes.allocateGC)([1.1, 2.3, 3.5], [2.3, 3.5, 1.1]));assert(!isPermutation!(Yes.allocateGC)([1, 2], [3, 4]));
CommonType!(T, Ts)either(alias pred = (a) => a, T, Ts...)(Tfirst, lazy Tsalternatives)
if (alternatives.length >= 1 && !is(CommonType!(T, Ts) == void) && allSatisfy!(ifTestable, T, Ts));
Get the first argumenta that passes anif (unaryFun!pred(a)) test. Ifno argument passes the test, return the last argument.
Similar to behaviour of theor operator in dynamic languages such as Lisp's(or ...) and Python'sa or b or ... except that the last argument isreturned upon no match.
Simplifies logic, for instance, in parsing rules where a set of alternativematchers are tried. The first one that matches returns it match result,typically as an abstract syntax tree (AST).
Bugs:
Lazy parameters are currently, too restrictively, inferred by DMD toalways throw even though they don't need to be. This makes it impossible tocurrently markeither asnothrow. See issue atBugzilla 12647.
Returns:
The first argument that passes the testpred.
Examples:
const a = 1;const b = 2;auto ab =either(a, b);staticassert(is(typeof(ab) ==const(int)));writeln(ab);// aauto c = 2;const d = 3;auto cd =either!(a => a == 3)(c, d);// use predicatestaticassert(is(typeof(cd) ==int));writeln(cd);// dauto e = 0;const f = 2;auto ef =either(e, f);staticassert(is(typeof(ef) ==int));writeln(ef);// f
Examples:
immutable p = 1;immutable q = 2;auto pq =either(p, q);staticassert(is(typeof(pq) ==immutable(int)));writeln(pq);// pwriteln(either(3, 4));// 3writeln(either(0, 4));// 4writeln(either(0, 0));// 0writeln(either("","a"));// ""
Examples:
string r =null;writeln(either(r,"a"));// "a"writeln(either("a",""));// "a"immutable s = [1, 2];writeln(either(s, s));// swriteln(either([0, 1], [1, 2]));// [0, 1]writeln(either([0, 1], [1]));// [0, 1]writeln(either("a","b"));// "a"staticassert(!__traits(compiles,either(1,"a")));staticassert(!__traits(compiles,either(1.0,"a")));staticassert(!__traits(compiles,either('a',"a")));
Copyright © 1999-2025 by theD Language Foundation | Page generated byDdoc on Fri Oct 10 22:10:22 2025

[8]ページ先頭

©2009-2025 Movatter.jp