Emacs rectangle space removal - emacs

I have something like the following data set
A, B ,C,D , E
A1 , B121 ,C1,D1 , E1
A2,Ber2 ,C2,D2 , E2
A3, Bat3 ,C3,D3 , E3
And I want the commas to align so that they are right after the text ends and there is a space after which the next column starts.
Like this
A, B, C, D, E
A1, B121, C1, D1, E1
A2, Ber2, C2, D2, E2
A3, Bat3, C3, D3, E3
I tried using delete-whitepsace-rectangle but that only works as long as the width of the strings ina column match for some reason .
Is there a way to make this happen in emacs?

You want to replace spaces, a comma, and spaces, with a comma and a single space.
You can do this with replace-regexp, replacing "\ *,\ *" with ",".

Related

How to unpack a vector and send the values as a parameter list?

Consider the following future sequence:
val s1 = Future.sequence((1 to 5).map(someFuncReturningFuture))
And in the end I want to return a Future[MyCaseClass] where MyCaseClass is a case class that takes 5 parameters, which I want to be the 5 Ints inside s1. I thought s1.map((a1, a2, a3, a4, a5) => MyCaseClass(a1, a2, a3, a4, a5)) should work. How to achieve what I'm trying to do?
Solution I found was to do:
for (Vector(a1,a2,a3,a4,a5) <- Future.sequence((1 to 5).map(someFuncReturningFuture)))
yield MyCaseClass(a1,a2,a3,a4,a5)

Creating an RDD of edges from an RDD of graphs

Consider a collection of graphs. In my current case, it is an RDD[Graph[VD, Double]], but it could with certain effort be reworked into Seq[Graph[VD, Double]], if it would make an answer easier, but I prefer the former.
My question is, how to efficiently create an RDD[Edge[Double]] containing the edges of each of the graphs in the collection?
As an example, let the graph collection contain three graphs G1, G2 and G3. Let G1 have edge set { e1, e2, e3 }, G2 have edge set { e4, e5 } and G3 have edge set { e6, e7, e8, e9 }. For an input RDD of graphs containing G1, G2 and G3, the output should be an RDD[Edge[Double]] containing { e1, e2, e3, e4, e5, e6, e7, e8, e9 }.
First, I've tried with flatMap (graphs.flatMap(graph => graph.edges)), but I get a type mismatch error, stating that a TraversableOnce[?] type is required, but EdgeRDD[Double] found.
Further, I've tried first creating a collection of EdgeRDD[Double] with graphs.map(graph => graph.edges) with the intent of further modifying it, but it expectedly failed with 'Spark does not support nested RDDs'
Look at .toLocalIterator. This method allows you to turn EdgesRDD into Iterable and the flatMap will do the job.
Remember that this operation might be expensive. If your initial RDD of type RDD[Graph[VD, Double]] is not cached you should consider caching it. toLocalIterator would sequentially fetch every partition of EdgesRDD.
Your final call could look like this
graphs.flatMap(_.edges.toLocalIterator)

Notations in Coq

I want to use the notations to represent the predicate test as follows:
Variable A B : Type.
Inductive test : A -> B -> A -> B -> Prop :=
| test1 : forall a1 a2 b1 b2,
a1 \ b1 || a2 \ b2
where "c1 '\' st '||' c2 '\' st'" := (test c1 st c2 st')
.
However, the Coq has an error:
Why this notation cannot be accepted in Coq?
The notation is accepted, it's actually that Coq is incorrectly parsing your use of the notation within the definition of test1. To correctly parse this notation you need to adjust the parsing levels of its terms. You can do that with a reserved notation, since these where clauses for notation within an inductive don't support the syntax for configuring the notation:
Variable A B : Type.
Reserved Notation "c1 '\' st '||' c2 '\' st'" (at level 40, st at next level, c2 at next level, no associativity).
Inductive test : A -> B -> A -> B -> Prop :=
| test1 : forall a1 a2 b1 b2,
a1 \ b1 || a2 \ b2
where "c1 '\' st '||' c2 '\' st'" := (test c1 st c2 st')
.
I don't have a good intuition for what parsing levels work well (40 is somewhat arbitrary above), so the best advice I can give is to experiment and if it's parsed incorrectly somewhere then try adjusting the level.

matching elements in a table by perl

I have some data that looks like this:
G1 G2 G3 G4
Pf1 NO B1 NO D1
Pf2 NO NO C1 D1
Pf3 A1 B1 NO D1
Pf4 A1 NO C1 D2
Pf5 A3 B2 C2 D3
Pf6 NO B3 NO D3
My purpose is to check in each column if an element (different from the "NO" cases) is showed twice (like A1 in column 2, for example) and only twice (if it is showed three times or more I don't want it in the output) and, if so, write it as correspondenting to the element of the first column. Of course, I will have more elements of the columns corresponding to an element of the first column. So, the desired output looks like this:
Pf1 B1
Pf2 C1
Pf3 A1 B1
Pf4 A1 C1
Pf5 D3
Pf6 D3
I have a code, that work in the opposite direction. It lists the elements of the first column that correspond to the elements that are showed twice and only twice in the other columns. This code looks like this:
use Data::Dumper;
my %hash;
while (<DATA>) {
next if $.==1;
chomp;
my ($first,#others) = (split /\s+/);
for (#others){
$hash{$_}.=' '.$first;
}
}
print Dumper \%hash;
I need to be pushed in order to adapt it to my new purpose. Any help or suggestion is totally welcome!
my %hash;
my #r;
while (<DATA>) {
next if $.==1;
chomp;
my #t = grep $_ ne "NO", split;
push #r, \#t;
$hash{$_}++ for #t[1 .. $#t];
}
for my $l (#r) {
my $k = shift #$l;
my #t = grep { $hash{$_} ==2 } #$l;
print "$k #t\n";
}
__DATA__
G1 G2 G3 G4
Pf1 NO B1 NO D1
Pf2 NO NO C1 D1
Pf3 A1 B1 NO D1
Pf4 A1 NO C1 D2
Pf5 A3 B2 C2 D3
Pf6 NO B3 NO D3
output
Pf1 B1
Pf2 C1
Pf3 A1 B1
Pf4 A1 C1
Pf5 D3
Pf6 D3

How to search pattern in certain range of text and replace with another string?

$ NAME : corry
$$.Inc s d
$$.Oc s
$$.TO
G1 ty n1 EE EE M T1 T2 $$SRU
G2 n1 y OO OO M T3 T4 $$SRU
.EON
$ NAME : patrick
$$.Inc c d
$$.Oc c
$$.TO
G1 td n3 EE EE M T5 T6 $$SRU
G2 n3 y OO OO M T7 T8 $$SRU
.EON
$ NAME : danny
$$.Inc a b
$$.Oc b
$$.TO
#lc1 corry
#lc2 patrick
1 to n0 EE EE M S1 S2 $$SRU
G2 n0 y OO OO M S3 S4 $$SRU
.EON
$ NAME : sandy
$$.Inc m n
$$.Oc n
$$.TO
G1 te n1 EE EE M b1 b2 $$SRU
G2 n1 o OO OO M b3 b4 $$SRU
.EON
$ NAME : manager
$$.Inc o e
$$.Oc e
$$.TO
#lc3 danny
#lc4 sandy
G1o ty n1 EE EE M T1 T2 $$SRU
G2o n1 y OO OO M T3 T4 $$SRU
.EON
How to search for a certain pattern in a certain range? For example, I want to search G1o at range between the section from $ Name : manager until the end of End of name (.EON) and replace it with G1o.corry.n.
perl -pe 's/G1o/G1o.corry.n/ if /\$ NAME : manager/ .. /\.EON/' file
From perlop documentation:
In scalar context, ".." returns a boolean value. The operator is bistable, like a flip-flop, and emulates the line-range (comma) operator of sed, awk, and various editors. Each ".." operator maintains its own boolean state, even across calls to a subroutine that contains it. It is false as long as its left operand is false.
sed '/^\$ NAME : manager/,/\.EON/s/G1o/G1o.corry.n/'