defmodule My do
def go do
x = nil
if(not x) do
IO.puts "hello"
else
IO.puts "goodbye"
end
end
end
In iex:
/elixir_programs$ iex c.exs
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> My.go
** (ArgumentError) argument error
c.exs:5: My.go/0
iex(1)>
According to Programming Elixir >= 1.6, p.35:
Elixir has three special values related to Boolean operations: true,
false, and nil. nil is treated as false in Boolean contexts.
It doesn't seem to be true:
defmodule My do
def go do
x = false
if (not x) do
IO.puts "hello"
else
IO.puts "goodbye"
end
end
end
In iex:
~/elixir_programs$ iex c.exs
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> My.go
hello
:ok
iex(2)>
#spec not true :: false
#spec not false :: true
def not value do
:erlang.not(value)
end
Elixir 's latest definition of not function shows that it only receive false and true.
However, nil is not belongs to them, so it shows argument error.
Elixir has three special values related to Boolean operations: true, false, and nil. nil is treated as false in Boolean contexts.
nil is just an atom, that is nil === :nil.
You can consider using ! operator, which in fact is Kernel.! macro.
Receives any argument (not just booleans) and returns true if the
argument is false or nil; returns false otherwise.
!nil will return true.
"Kernel.not/1" or not/1 expects a Boolean value
note: Other values different of nil and false are true
try this example:
x = nil
if (x) do true else false end
false
Examples with short-if condition and true, false, nil values
iex> if nil , do: true, else: false
false
iex> if !nil , do: true, else: false
true
iex> if false , do: true, else: false
false
iex> if !false , do: true, else: false
true
iex> if not false , do: true, else: false
true
iex> if true , do: true, else: false
true
iex> if !true , do: true, else: false
false
iex> if not true , do: true, else: false
false
Related
I have the following BraceWrapping options:
BraceWrapping:
AfterEnum: false
AfterStruct: false
SplitEmptyFunction: false
AfterControlStatement: "Never"
AfterFunction: false
AfterNamespace: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
However, clang-format always inserts a new line after an enum:
enum class event_flags : std::uint8_t
{
running = 1 << 0,
executed = 1 << 1,
};
I want it to be like this:
enum class event_flags : std::uint8_t {
running = 1 << 0,
executed = 1 << 1,
};
what am I doing wrong here?
The one option I could find that seems to fix this formatting for you is outside the BraceWrapping section and that is setting
AllowShortEnumsOnASingleLine: true
even though the description of that option doesn't mention it:
true:
enum { A, B } myEnum;
false:
enum {
A,
B
} myEnum;
I would like to identify the string "mystring" in a text (actually a R code), but ensuring that it is a variable name:
it should not be part of a longer variable such as "thisismystring" or "mystringisnice". I guess this can be done by excluding (?!([[:alnum:]]){1}) before and after "mystring"?
It can however be at the beginning of a line (such as "\nmystring") so we have to be careful about this type of exception
What is the best approach for that?
Edit: unit test
pattern = "\\bmystring\\b"
identical(grepl(pattern = pattern,
x = c("thisismystring","mystringisnice","\nmystring", "mystring", "mystring=", "mystring(", " mystring","mystring\n", "6mystring", "mystring0", "= mystring(", "=mystring",
"hop
mystring")),
c(FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE))
Just use the \b (word boundary) token:
\\bmystring\\b
unit test
pattern = "\\bmystring\\b"
`==`(grepl(pattern = pattern,
x = c("thisismystring","mystringisnice","\nmystring", "mystring", "mystring=", "mystring(", " mystring","mystring\n", "6mystring", "mystring0", "= mystring(", "=mystring",
"hop
mystring")),
c(FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE))
# [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
I want the following outcome:
"1" -> true
"0" -> false
nil -> nil
How can this function be improved?
#(when-not (nil? %) (if % "1" "0"))
Your requirement translates directly into a map and maps are also callable as functions in Clojure, so {1 true, 0 false} is the function you want.
({1 true, 0 false} 1) ;;=> true
({1 true, 0 false} 0) ;;=> false
({1 true, 0 false} nil) ;;=> nil
I want to set a variable in Python to true or false. But the words true and false are interpreted as undefined variables:
#!/usr/bin/python
a = true;
b = true;
if a == b:
print("same");
The error I get:
a = true
NameError: global name 'true' is not defined
What is the python syntax to set a variable true or false?
Python 2.7.3
First to answer your question, you set a variable to true or false by assigning True or False to it:
myFirstVar = True
myOtherVar = False
If you have a condition that is basically like this though:
if <condition>:
var = True
else:
var = False
then it is much easier to simply assign the result of the condition directly:
var = <condition>
In your case:
match_var = a == b
match_var = a==b
that should more than suffice
you cant use a - in a variable name as it thinks that is match (minus) var
match=1
var=2
print match-var #prints -1
Python boolean keywords are True and False, notice the capital letters. So like this:
a = True;
b = True;
match_var = True if a == b else False
print match_var;
When compiled and run, this prints:
True
you have to use capital True and False not true and false
as Poke said:
If you have a condition that is basically like this though:
if <condition>:
var = True
else:
var = False
then it is much easier to simply assign the result of the condition
directly:
var = <condition>
but if you want to reverse it you can use:
var = <condition> is False
I'm coding my own version of Tetris. While creating blockRotation method I encountered weird problem with memcpy. During second call for blockRotate, my bool[4*4] array is not saved corectly, despite that is generated properly. What's wrong?
This is my error log:
arr_shift[0]: true
arr_shift[1]: false
arr_shift[2]: false
arr_shift[3]: false
arr_shift[4]: true
arr_shift[5]: true
arr_shift[6]: false
arr_shift[7]: false
arr_shift[8]: true
arr_shift[9]: false
arr_shift[10]: false
arr_shift[11]: false
arr_shift[12]: false
arr_shift[13]: false
arr_shift[14]: false
arr_shift[15]: false
arr_rot[0]: false
arr_rot[1]: false
arr_rot[2]: true
arr_rot[3]: false
arr_rot[4]: false
arr_rot[5]: false
arr_rot[6]: true
arr_rot[7]: true
arr_rot[8]: false
arr_rot[9]: false
arr_rot[10]: true
arr_rot[11]: false
arr_rot[12]: false
arr_rot[13]: false
arr_rot[14]: false
arr_rot[15]: false
shape[0]: true
shape[1]: false
shape[2]: false
shape[3]: false
shape[4]: true
shape[5]: true
shape[6]: false
shape[7]: false
shape[8]: true
shape[9]: false
shape[10]: false
shape[11]: false
shape[12]: false
shape[13]: false
shape[14]: false
shape[15]: false
arr_shift[0]: false
arr_shift[1]: false
arr_shift[2]: false
arr_shift[3]: false
arr_shift[4]: false
arr_shift[5]: false
arr_shift[6]: false
arr_shift[7]: false
arr_shift[8]: false
arr_shift[9]: false
arr_shift[10]: false
arr_shift[11]: false
arr_shift[12]: false
arr_shift[13]: false
arr_shift[14]: false
arr_shift[15]: false
arr_rot[0]: false
arr_rot[1]: true <---
arr_rot[2]: true <---
arr_rot[3]: true <---
arr_rot[4]: false
arr_rot[5]: false
arr_rot[6]: true <---
arr_rot[7]: false
arr_rot[8]: false
arr_rot[9]: false
arr_rot[10]: false
arr_rot[11]: false
arr_rot[12]: false
arr_rot[13]: false
arr_rot[14]: false
arr_rot[15]: false
shape[0]: false
shape[1]: false <---
shape[2]: false <---
shape[3]: false <---
shape[4]: false
shape[5]: false
shape[6]: false <---
shape[7]: false
shape[8]: false
shape[9]: false
shape[10]: false
shape[11]: false
shape[12]: false
shape[13]: false
shape[14]: false
shape[15]: false
And this is my code:
memset(shape, 0, 16*sizeof(bool));
if(toShift == true) {
memcpy(shape, arr_shift, 16*sizeof(bool));
}
else {
memcpy(shape, arr_rot, 16*sizeof(bool));
}
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
fprintf(stderr, "shape[%i]: %s\n", i*4+j, shape[i*4+j]? "true":"false" );
}
}
fprintf(stderr, "\n\n");
Declararation of shape is in Block class header, and is as follows:
bool shape[16];
You haven't shown us where arr_rot is declared or what value toShift is. However, most likely either toShift is true or you declared where arr_rot points to on the stack in another function.