How I can convert string to raw string via macros? [closed] - rust

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a line example "one two free" and I need programatically to convert this to r#"one two free"# via macros, is this possible?
#[macro_export]
macro_rules! fmt_wrap {
($msg:block) => {
}
}
fn main() {
println!(fmt_wrap!("one two free"));
}

You can't.
The reason is quite simple: macros by example (the things you declare with macro_rules!) work on a token stream, not a stream of characters. The input to a macro invocation has to be a valid token stream. This means that the compiler has to tokenize the code before expanding any macros. But the difference between normal string literals "foo" and raw string literals r#"foo"# is only during tokenization! A string literal is one token.
This means that this:
fmt_wrap!("foo " bar");
Will never work. Before expanding fmt_wrap, the compiler has to convert its input into a valid token stream. But that's not possible!

Related

Even when I have the return value, it still returns none? Python Code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
The goal of this code is to alphabetize and uppercase the inputted tuple of values. However, it is returning none when I run it. I find this odd since I have a return and I beilieve everything is in correct order. If you can help find the answer, thanks. Here is the code:
def sorter(*args):
args = " ".join(args)
uppercased = args.upper()
listed = list(uppercased)
sorted1 = listed.sort()
return sorted1
print(sorter('happy', 'apple', 'zain', 'freindly', 'jakob'))
Run your code in the Python Tutor Visualizer and step through it line by line, and you will see that listed.sort() doesn't return anything but instead mutates listed:
Before executing listed.sort():
After executing listed.sort():
The docs for list.sort also tell you that the list is sorted in-place, and the function signature doesn't have a return value.
The solution is therefore to use listed after sorting it, instead of creating a new variable sorted1.
(Note that there are other logical mistakes in your code which will prevent it from delivering the result you probably expected, even after this specific issue is fixed, but that's beyond the scope of this question and answer.)

regex to match the pattern in python3 using match method [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to know the regex using re.match method to match below pattern.
some action 1.1.1.1 on sub.domain.com
Currently I'm using below code, which works fine but I want to strip of everything except sub.domain.com because using below code I'm getting something like <http:\\/\\/sub.domain.com|sub.domain.com>
some\s+action+\s+(.*)\s+on\s+(.*)
This is where you would use a capturing group. Notice you have two capturing groups in your question, but you only want one. Here would be an example of one where you get the captured group after the first four "words" (defined as one or more non-spaces plus a space):
(?:\S+\s){4}(.*)
Also note that in actual practice this wouldn't work too well -- what if "some action" is ten words? Instead you might want to do something more targeted such as a regex to match (in pseudocode) <IP> on (<domain>).
>>> import re
>>> s='some action 1.1.1.1 on sub.domain.com'
>>> re.match(r'(?:\S+\s){4}(.*)', s).group(1) # not using anchors, not sure if you need that?
# 'sub.domain.com'

How to replace occurances of a string with changing values in kotlin? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have the current string "number number number number number" and I would like to replace it with "1 2 3 4 5". How do i replace occurrences of a string with incrementing numbers in Kotlin?
Your idea of a solution in the comment is in the right direction.
The way to do this using a single call to the standard library is this:
var i = 1
val result = text.replace("number".toRegex()) { _ -> "${i++}" }
(runnable sample)
This replace overload accepts a function that is called on each MatchResult (which is ignored in this case)

Can someone help export a variable in node.js [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am doing an exercise in the Eloquent Javascript book and am having trouble getting access to a variable with JSON information in an adjacent .js file.
My file structure looks like this: eloquentJs (folder)> ancestry.js, chapter5json.js
I am including a require statement at the top of my chapter5json.js file:
require("./ancestry.js");
as well as:
module.exports = ANCESTRY_FILE;
at the bottom of my ancestry.js file.
When I try to run the following code in chapter5json.js:
var ancestry = JSON.parse(ANCESTRY_FILE);
console.log(ancestry.length);
I get an error that the
variable ANCESTRY_FILE is not defined.
Does anyone see what I'm doing wrong here?
You need to store the require statement like this: let ancestor = require("./ancestry.js")
Since the file is simply a json, simply use the ancestor like this:
let ancestry = JSON.parse(ancestor);
Now you can use all the variables using the reference of ancestry

How to check if a given String is an English word in Scala? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am looking for some library which take string as input and result boolean based on, if input string is present in english dictionary
For mac user default english dictionary can be found at location /usr/share/dict/web2
Hence we can,
>> var dict = scala.io.Source.fromFile("/usr/share/dict/web2").getLines.toSet
>> dict.contains("apple")
result : true
>> dict.contains("appl")
result : false

Resources