I am new to Elixir language and I am having some issues while writing a piece of code.
What I am given is a 2D array like
list1 = [
[1 ,2,3,4,"nil"],
[6,7,8,9,10,],
[11,"nil",13,"nil",15],
[16,17,"nil",19,20] ]
Now, what I've to do is to get all the elements that have values between 10 and 20, so what I'm doing is:
final_list = []
Enum.each(list1, fn row ->
Enum.each(row, &(if (&1 >= 10 and &1 <= 99) do final_list = final_list ++ &1 end))
end
)
Doing this, I'm expecting that I'll get my list of numbers in final_list but I'm getting blank final list with a warning like:
warning: variable "final_list" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)
iex:5
:ok
and upon printing final_list, it is not updated.
When I try to check whether my code is working properly or not, using IO.puts as:
iex(5)> Enum.each(list1, fn row -> ...(5)> Enum.each(row, &(if (&1 >= 10 and &1 <= 99) do IO.puts(final_list ++ &1) end))
...(5)> end
...(5)> )
The Output is:
10
11
13
15
16
17
19
20
:ok
What could I possibly be doing wrong here? Shouldn't it add the elements to the final_list?
If this is wrong ( probably it is), what should be the possible solution to this?
Any kind of help will be appreciated.
As mentioned in Adam's comments, this is a FAQ and the important thing is the message "warning: variable "final_list" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)" This message actually indicates a very serious problem.
It tells you that the assignment "final_list = final_list ++ &1" is useless since it just creates a local variable, hiding the external one. Elixir variables are not mutable so you need to reorganize seriously your code.
The simplest way is
final_list =
for sublist <- list1,
n <- sublist,
is_number(n),
n in 10..20,
do: n
Note that every time you write final_list = ..., you actually declare a new variable with the same name, so the final_list you declared inside your anonymous function is not the final_list outside the anonymous function.
how would this function be completed to return the common integers between two lists?
how would i complete the get_common_elements(list1, list2) function?. The function should select all the common integers from both parameters and displays them in the result.
ie numbers 1 = 3,6,8,9,12,35
numbers 2 = 6,7,13,34, 35
result = 6,35
you can assume that each number only occurs in each list once
def common_member(a, b):
a_set = set(a)
b_set = set(b)
if (a_set & b_set):
print(a_set & b_set)
else:
print("No common elements")
I have a list of expressions, f.x
"a1 || a4"
"a3 && a5
and a dictionary with the truth values of these
a1 -> true
a2 -> false
I need to evaluate expressions from the list, with the true/false values from the dictionary
Any ideas how this can be achieved easily? Any help is greatly appreciated
I feel like there might be a more elegant solution but this will certainly get the job done.
truths = Dict("a1" => true, "a2" => false)
expressions = ["a1 || a4", "a1 && a2"]
for (key, value) in truths
info("evaluating: '$key = $value'")
eval(parse("$key = $value"))
end
for exp in expressions
info("$exp => $(eval(parse(exp)))")
end
What is f.x? What do you mean by a list? In future, please give working code, not code snippets like this.
In Julia you almost certainly do not want to store Julia code like a1 || a4 in strings, but rather as Julia expressions (which is what parse of a string actually gives):
ex = [:(a1 || a4), :(a3 && a5)]
Secondly, you probably don't want to use variables with names like this, but rather an array:
a = bitrand(5) # 5 random bits
a[1] # gives true
Then you would use
ex = [:(a[1] || a[4]), :(a[3] && [a[5])]
and you can just do
map(eval, ex)
which evaluates each expression in the vector.
If you think that using a[1] instead of a1 is too much typing, you can walk the syntax tree and replace a_i with a[i]. Or, if you have the strings, just use the replace function on the string:
replace("hello a3 a34", "a3", "a[3]")
(so be careful!)
I have this database:
CREATE (A:A {name:"A"})-[:R]->(B:B {name:"B"})-[:R]->(C:B {name:"C"})-[:R]->(D:A {name:"D"})-[:R]->(E:A {name:"E"})
This query
MATCH p = (:A)-[*]->(:B) WITH NODES(p)[1..] AS p_nodes RETURN p_nodes
returns edge (B)-->(C). And B and C have both label B. Why then does this query
MATCH p = (:A)-[*]->(:B) WITH NODES(p)[1..] AS p_nodes
WHERE ALL(x IN p_nodes[0..] WHERE LABELS(x) = "B") RETURN p_nodes
return nothing (no rows)? The only thing it does is make sure that p_nodes contains B labeled nodes only. And as the first query showed it does.
The labels(x) function will return a collection of strings, not a string. This is because nodes can have multiple labels.
So instead of comparing labels(x) = "B" use the IN operator "B" in labels(x):
MATCH p =(:A)-[*]->(:B)
WITH NODES(p)[1..] AS p_nodes
WHERE ALL (x IN p_nodes[0..]
WHERE "B" IN LABELS(x))
RETURN p_nodes
I'm a beginner with R (and coding in general). In January 14 I hopefully can begin and finish a R course, but I would like to learn before. I have understanding of the basics and have used functions like read.table,intersect,cbind,paste,write.table.
But I only was able to achieve partially what I want to do with two input files (shortened samples):
REF.CSV
SNP,Pos,Mut,Hg
M522 L16 S138 PF3493 rs9786714,7173143,G->A,IJKLT-M522
P128 PF5504 rs17250121,20837553,C->T,KLT-M9
M429 P125 rs17306671,14031334,T->A,IJ-M429
M170 PF3715 rs2032597,14847792,A->C,I-M170
M304 Page16 PF4609 rs13447352,22749853,A->C,J-M304
M172 Page28 PF4908 rs2032604,14969634,T->G,J2-M172
L228,7771358,C->T,J2-M172
L212,22711465,T->C,J2a-M410
SAMPLE.CSV
SNP,Chr,Allele1,Allele2
L16,Y,A,A
P128,Y,C,C
M170,Y,A,A
P123,Y,C,C
M304,Y,C,C
M172,Y,T,G
L212,Y,-0,-0
Description what I like to do:
A) Check if SAMPLE.SNP is in REF.SNP
B) if YES check SAMPLE.Allele status (first read, second read) vs REF.Mut (Ancestral->Derived)
B1) if both Alleles are the same and match Derived create output "+ Allele1-Allele2"
B2) if both Alleles are the same and match Ancestral create output "- Allele1-Allele2"
B3) if Alleles are not the same check if Allele2 is Derived and create output "+ Allele1-Allele2"
B4) if both Alleles are "-0" create output "? NC"
B5) else create output "? Allele1-Allele2"
B6) if NO create output "? NA"
C) Write REF.CSV + output in new row (Sample) and create OUTPUT file
OUTPUT.CSV (like wanted)
SNP,Pos,Mut,Hg,Sample
M522 L16 S138 PF3493 rs9786714,7173143,G->A,IJKLT-M522,+ A-A
P128 PF5504 rs17250121,20837553,C->T,KLT-M9,- C-C
M429 P125 rs17306671,14031334,T->A,IJ-M429,? NA
M170 PF3715 rs2032597,14847792,A->C,I-M170,- A-A
M304 Page16 PF4609 rs13447352,22749853,A->C,J-M304,+ C-C
M172 Page28 PF4908 rs2032604,14969634,T->G,J2-M172,+ T-G
L228,7771358,C->T,J2-M172,? NA
L212,22711465,T->C,J2a-M410,? NC
What functions I have found interesting and tried so far.
Variant1: A) is done, but I guess it is not possible to write C) with this?
Have not tried to code down B) here
GT <- read.table("SAMPLE.CSV",sep=',',skip=1)[,c(1,3,4)]
REF <- read.table("REF.CSV",sep=',')
rownames(REF) <- REF[,1]
COMMON <- intersect(rownames(GT),rownames(REF))
REF <- REF[COMMON,]
GT <- GT[COMMON,]
GT<-cbind(REF,paste(GT[,2],'-',X[,3],sep=','))
write.table(GT,file='OUTPUT.CSV',quote=F,row.names=F,col.names=Fââ)
Variant2: This is probably a complete mess, forgive me. I was just rying to build a solution on for if looping functions, but I haven't understood R's syntax and logic in this probably.
I was not able to get this to run - A) and C)
Have not tried to code down B) here
GT<-read.table("SAMPLE.CSV",sep=',',skip=1)[,c(1,3,4)]
rownames(GT)<-GT[,1]
REF <- read.table("REF.CSV",sep=',')
rownames(REF)<-REF[,1]
for (i in (nrow(REF))) {
for (j in (nrow(GT))) {
if (GT[j,] %in% REF[i,]) {
ROWC[i,]<-cbind(REF[i,],paste(GT[j,2],"-",GT[j,3],sep=','))
} else {
ROWC[i,]<-cbind(REF[i,],"NA",sep=',')
}
}
}
write.table(ROWC,file='OUTPUT.CSV',quote=F,row.names=F,col.names=F)
I would be just happy if you can indicate what logic/functions would lead to reach the task I have described. I will then try to figure it out. Thx.