Multiply a string by an integer? - xpath

In python, for example, I can do this
"X" * 3
or, more importantly (in this case)
"XO" * 3
and get XXX and XOXOXO, respectively.
I'm trying to replicate this with xpath/xquery. Ideally, it should be something like
"XO" * count(something)
The closest I could get is by sort of faking the output by creating a string with a length close to my estimate of what count(something) is going to be and then slice it with substring(). So if my count(something) returns 2
substring("XXX",1,count(//something))
would return XX.
It gets slightly more complicated if my string is XO, in which case I have to modify it to
substring("XOXOXO",1,count(//something) * 2)
to get XOXO.
However, in addition to feeling hacky, these require an estimate of count() and the creation of a string of the correct length (+ some safety margin).
Is there a better way to do it in xpath or xquery?

You can use ((1 to 3) ! 'XO') => string-join() and of course you can put that into a function if you like with a parameter for the string and the number of "multiplications" you want:
declare function local:multiply-string($input as xs:string, $factor as xs:integer) as xs:string
{
((1 to $factor) ! $input) => string-join()
};
local:multiply-string('XO', 3)
https://xqueryfiddle.liberty-development.net/pPqteBa/1

Related

How does element membership work in Perl 6?

Consider this example
my #fib = (1,1, * + * … * > 200).rotor(2 => -1);
say #fib[0] ∈ #fib; # prints True
The first statement creates a Sequence of 2-element subsequences via the use of the rotor function. #fib will contain (1,1), (1,2) and so on. Quite obviously, the first element of a sequence is part of a sequence. Or is it?
my #fib = (1,1, * + * … * > 200).rotor(2 => -1);
say #fib[0], #fib[0].^name; # OUTPUT: «(1 1)List␤»
So the first element contains a list whose value is (1 1). OK, let's see
my $maybe-element = (1,1);
say $maybe-element, $maybe-element.^name; # OUTPUT: «(1 1)List␤»
say $maybe-element ∈ #fib; # OUTPUT: «False␤»
Wait, what? Let's see...
my $maybe-element = #fib[0];
say $maybe-element ∈ #fib; # OUTPUT: «True␤»
Hum. So it's not the container. But
say (1,1).List === (1,1).List; # OUTPUT: «False␤»
And
say (1,1).List == (1,1).List; # OUTPUT: «True␤»
So I guess ∈ is using object identity, and not equality. That being the case, how can we check, in sets or sequences of lists, if an independently generated list is included using this operator? Should we use another different strategy?
Maybe a subquestion is why the same literals generate completely different objects, but there's probably a good, and very likely security-related, answer for that.
So I guess ∈ is using object identity, and not equality.
That is correct.
That being the case, how can we check, in sets or sequences of lists, if an independently generated list is included using this operator?
You can use .grep or .first and the equality operator of your choice (presumably you want eqv here), or you can try to find a list-like value type. Off the top of my head, I don't know if one is built into Perl 6.

How to use Linq Aggregate Query to print line sequence number alongwith some data from the List<string>?

I have a simple List like
List<string> Test=new List<string>() {"A","B","C"}
I am using a simple Linq.Aggregate as under to get a string from the elements of this List as under:-
string Output= Test.Aggregate((First, Next) => First + "\r\n" + Next);
This gives me the result like(new line separated):
A
B
C
However,i want result with a sequence number on each line,ie like this:-
1)A
2)B
3)C
How do i do this using linq?
Select has an overload that will give you the index of the element to work with so you could do Select((x,i)=>String.Format("{0}){1}", i+1,x)). Or in full:
string output= Test.Select((x,i)=>String.Format("{0}){1}", i+1,x)).Aggregate((First, Next) => First + "\r\n" + Next);
One thing worth mentioning though is that string concatenation in a loop (and in the Aggregate counts as in a loop) is generally considered a bad idea for performance reasons. You should consider using alternative methods such as a StringBuilder:
string output = Test
.Aggregate (new StringBuilder(), (sb, x) => sb.AppendFormat("{0}){1}\r\n", lineCount++, x))
.ToString();
I wouldn't use Aggregate here, just a Select to get the index and join the resulting list back together to make a single string, for example:
var output = string.Join("\r\n",
Test.Select((s, index) => $"{index+1}){s}"));

How to acess argument from a function passed as argument in SML

I am new to coding SML and am still trying to understand pattern matching. I am trying to find out how to access an argument from a function passed as an argument in SML. For example, if a function takes 2 arguments, a function and an integer, how can I access the argument (for simplicity, assuming it only has one) that the argument function has. Does this question even make sense in SML? Here is some code, which doesn't work, but I think it illustrates what I am trying to do.
fun argumentExtraction (n,f) =
case f of
fn x => x
A function doesn't have arguments, it takes arguments. You give arguments to the function and you get a result back. You can't ask the function what its arguments are because it doesn't have them yet. You are supposed to give the arguments to the function.
To make this perhaps a bit clearer, let us consider an example. Let's take the following functions:
fun add x y = x + y
fun plus1 x = x + 1
fun applyTwice f x = f (f x)
Now applyTwice takes a function and an argument and applies the function to the argument twice. We can call it like this:
applyTwice plus1 40
or like this:
applyTwice (add 1) 40
and in both cases the answer is going to be 42 (because that's the value of plus1 (plus1 40) and of add 1 (add 1 40), add 1 really being the exact same function as plus1).
Now I'm not sure whether you want to be able to pass (plus1 40) as the value for f and then somehow get 40 from that or whether you want to pass (add 1) and then somehow get back the 1, but both are impossible.
In the case of (plus1 40) you can't even pass that as the value for f. f is supposed to be a function, but (plus1 40) is an integer (41), not a function.
(add 1) on the other hand is a function and you can indeed pass it as the value for f. In fact I did so in my example. But what you can't do is get the value 1 back from it. I mean, imagine there was a way that you could do that. What should then happen if you passed plus1 instead of (add 1). What should you get back from that? There is no value captured in plus1, so there's really nothing to get back, but clearly plus1 is just as valid an argument to applyTwice as (add 1).
The only way this could possibly make sense is if partially applied function had a different type from normal functions, allowing you to define a function that accepts (add 1) as one argument, but not plus1. However that's not the case ML and there's really not much benefit to that. Having different types of functions would just make the type system more complicated and make it more difficult to define higher order functions (as they'd have to be able to deal with both types of function). Nor do I see the use case where this ability would even be helpful.

Defining constants from multiple return values Racket

The matrix-qr function in Racket's math library outputs two values. I know about call-with-values to put both output values into the next function you want.
However, how can I take each individual output and put define some constant with that value? The QR function outputs a Q matrix and an R matrix. I need something like:
(define Q ...)
(define R ...)
Also, how could I just use one of the outputs from a function that outputs two values?
The usual way to create definitions for multiple values is to use define-values, which pretty much works like you’d expect.
(define-values (Q R) ; Q and R are defined
(matrix-qr (matrix [[12 -51 4]
[ 6 167 -68]
[-4 24 -41]])))
There is also a let equivalent for multiple values, called let-values (as well as let*-values and letrec-values).
Ignoring values is harder. There is no function like (first-value ...), for example, because ordinary function application does not produce a continuation that can accept multiple values. However, you can use something like match-define-values along with the _ “hole marker” to ignore values and simply not bind them.
(match-define-values (Q _) ; only Q is defined
(matrix-qr (matrix [[12 -51 4]
[ 6 167 -68]
[-4 24 -41]])))
It is theoretically possible to create a macro that could either convert multiple values to a list or simply only use a particular value, but in general this is avoided. Returning multiple values should not be done lightly, which is why, for almost all functions that return them, it wouldn’t usually make much sense to use one of the values but ignore the other.

Compound Expressions in a Function in Mathematica

I wanted to calculate the power sum S_p(x) = 1^p + 2^p + 3^p + ... + x^p using the code
powersum[x_,p_]:=sum=0;For[i=1,i<x,i++,sum=sum+i^p];sum
but it seems to output 0 every time. Why does it do that?
As written, Mathematica is parsing your expression like this:
powersum[x_,p_]:=sum=0; (*Definition ended here*)
For[i=1,i<x,i++,sum=sum+i^p];
sum
You need to use to wrap your expression in parenthesis to make them all part of the function definition.
powersum[x_,p_]:=(sum=0;For[i=1,i<x,i++,sum=sum+i^p];sum)
Often it is preferable to use Module[]:
powersum[x_,p_]:=Module[{sum},sum=0;For[i=1,i<x,i++,sum=sum+i^p];sum]
or
powersum[x_,p_]:=Module[{sum=0},For[i=1,i<x,i++,sum=sum+i^p];sum]
this is essentially the same as wrapping in () except sum is protected in a local context.
of course for this example you could as well use :
powersum[x_,p_]:=Sum[i^p,{i,1,x-1}]
or
powersum[x_, p_] := Range[x - 1]^p // Total

Resources