user4712458 -4 years ago 108

Scala Question

I have

`val a: String = "E"`

val y: ArrayBuffer[String] = new ArrayBuffer("I", "G", "S")

I am trying to make a string, such that:

`"(E <=> (I | G | S)) & (~I | ~G) & (~I | ~S) & (~G | ~S)"`

Currently, for the first part of string (first clause)

`(E <=> (I | G | S))`

`s"($a <=> (${y.mkString(" | ")}))" // & (~${y.mkString(" | ~")})"`

For the second part, where are the permutations of elements in

`y`

`(~I | ~G) & (~I | ~S) & (~G | ~S)`

I am trying to use the

`y.permutations`

Thanks.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

It seems from your example that what you need is combinations, not permutations.

So to have a term for every pair of elements from `y`

you can find all combinations of length 2 using `combinations`

method. Then you can wrap each pair in brackets in the necessary format, and finally build the whole second part with `mkString`

:

```
y.combinations(2).map { case Seq(a, b) => s"(~$a | ~$b)" }.mkString(" & ")
```

You can integrate this expression into the string interpolation:

```
s"($a <=> (${y.mkString(" | ")})) & ${
y.combinations(2).map { case Seq(a, b) => s"(~$a | ~$b)" }.mkString(" & ")}"
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**

Latest added