Scala’s ‘::’ operator, how does it work?

From the Spec: 6.12.3 InfixOperations An infix operator can be an arbitrary identifier. Infix operators have precedence and associativity defined as follows. … The associativity of an operator is determined by the operator’s last character. Operators ending in a colon ‘:’ are right-associative. All other operators are left- associative. You can always see how these … Read more

Prolog: First duplicate value

Here is a pure version using dif/2 which implements sound inequality. dif/2 is offered by B-Prolog, YAP-Prolog, SICStus-Prolog and SWI-Prolog. firstdup(E, [E|L]) :- member(E, L). firstdup(E, [N|L]) :- non_member(N, L), firstdup(E, L). member(E, [E|_L]). member(E, [_X|L]) :- member(E, L). non_member(_E, []). non_member(E, [F|Fs]) :- dif(E, F), non_member(E, Fs). The advantages are that it can also … Read more

XPath 1.0 to find if an element’s value is in a list of values

You can check multiple conditions inside the same square brackets: /Location/Addr[State=”TX” or State=”AL” or State=”MA”] Or if you have a really long list, you can create a list of states and use the contains() function. /Location/Addr[contains(‘TX AL MA’, State)] This will work fine for two-letter state abbreviations. If you want to make it more robust … Read more

How to extend a Scala list to enable slicing not by explicit position but by given predicate/condition

List already has a slice method – it takes a subset of elements between a start and end index. What you’re looking for is repeated application of the span method: def span(p: (A) ⇒ Boolean): (List[A], List[A]) Which is documented as: Splits this list into a prefix/suffix pair according to a predicate. Note: c span … Read more