I'm trying to implement Macro to expand Verilog Bus as Vim - Macro to expand verilog bus and this is really working good for one variable.
But I've got the problem because I want to implement multiple bus as the below
Source:
{test[13:10],thisistest[3:0],BUS[2:1]}
Result:
test[13]
test[12]
test[11]
test[10]
thisistest[3]
thisistest[2]
thisistest[1]
thisistest[0]
BUS[2]
BUS[1]
What I tried to do :
I made similar function as
fun! split()
let line = getline('.')
let result = []
let list = split(line, ",")
let length = len(list)
for i in length
call ExpandIt(list[length])
endfor
endf
fun! ExpandIt()
let pat = '^\(.*\)\[\(\d\+\):\(\d\+\)\]\s*$'
let line = getline('.')
let lnr = line('.')
if line !~ pat
return
endif
let exestr = substitute(line,pat,'range(\2,\3,-1)','g')
let text = substitute(line,pat,'\1','g')
exec 'let range='.exestr
let result = []
for i in range
call add(result, text.'['.i.']')
endfor
call append(lnr, result)
exec lnr'.d'
endf
nnoremap <F6> :call split()<cr>
Could you please let me know what am I supposed to do to go with right way?
Based on the obvious errors in your added split() function (the function name must be capitalized to avoid E128, you pass an argument to ExpandIt(), but it doesn't take any, length is not iterable by :for), you seem to have a limited understanding of Vimscript, and just try to "make it work" through brute-force attempts. Therefore it was good to ask here on Stack Overflow, but please be aware that just relying on helpful strangers to fix things for you is a lopsided approach, so please use this and future questions to slowly but steadily learn about Vim, to become a master of it yourself!
The following seems to do what you're asking for, by doing the following modifications:
fixing the obvious errors listed above
collecting the result List in Split() is the right idea, but the actual insertion (:call append()) and deletion of the original line has to be moved there as well, and ExpandIt() has to return the result
likewise, ExpandIt() needs to be passed the extracted element instead of directly getting the current line
the split() of the line into comma-separated elements needs to drop the surrounding { and }; substitute() can do this.
fun! Split()
let line = getline('.')
let result = []
for l in split(substitute(line, '[{}]', '', 'g'), ",")
let result += ExpandIt(l)
endfor
let lnr = line('.')
call append(lnr, result)
exec lnr'.d'
endf
fun! ExpandIt(line)
let pat = '^\(.*\)\[\(\d\+\):\(\d\+\)\]\s*$'
if a:line !~ pat
return
endif
let exestr = substitute(a:line,pat,'range(\2,\3,-1)','g')
let text = substitute(a:line,pat,'\1','g')
exec 'let range='.exestr
let result = []
for i in range
call add(result, text.'['.i.']')
endfor
return result
endf
nnoremap <F6> :call Split()<cr>
Related
I am new to Spark. I want to output the top 2 twitter mentions using this test.txt file:
"I love to dance #Kelsey, especially with you #Kelsey!"
"Can't believe you went to #harvard. Come on man #harvard"
"I love #harvard"
Essentially, multiple mentions in a single tweet only counts once. So the output would be like:
(2, #harvard)
(1, #Kelsey)
So far, my codes looks like the following:
val tweets = sc.textFile("testFile")
val myReg = """(?<=#)([\\w]+)""".r
val mentions = tweets.filter(x => (myReg.pattern.matcher(x).matches))
However, it would not work because x is still a line and it will not match as a result. Is there anyway I can test the word in the line instead of the line itself? Also, how do I check if that mention is redundant in the tweet?
I adjusted your regex a little and you might need to translate it back to spark syntax, but this way you find all mentions and group them. The .toSet is important to remove duplicates, .toLowercase would also make sense there
val tweets = List("I love to dance #Kelsey, especially with you #Kelsey!",
"Can't believe you went to #harvard. Come on man #harvard",
"I love #harvard")
val myReg = """(#\w+)""".r
val mentions = tweets.flatMap(x => myReg.findAllIn(x).toSet).groupBy(identity).mapValues(_.length)
println(mentions)
That works for me, the regexs is more tweeter exact
val myReg = "(^|[^#\\w])#(\\w{1,15})\\b".r
val mentions = tweets.flatMap(x => myReg.findAllIn(x).matchData.map(_.group(0).trim -> 1)).reduceByKey(_ + _)
I'm trying to write a function that will return the second smallest number in a list. I keep getting a syntax error but I can't really pinpoint what the issue is. Can I please get help on this?
Deleted code
You forget to close local let bindings using in. The correct (and indented) code should be:
let second_smallest_helper1 lst=
let second_smallest_helper2 currentMinimum currentNumber =
if currentMinimum < currentNumber then currentMinimum else currentNumber
in List.fold_left second_smallest_helper2 (List.hd lst) lst
;;
let delete (x, mylist) = List.filter (fun y -> y != x) mylist;;
let second_smallest myList =
let x = second_smallest_helper1 myList in
let newList = delete (x,myList) in
second_smallest_helper1 newList
;;
Top level let binding has the form
let <pattern> = <expression>;; (* ;; is optional, but beginners should have it *)
but local let binding has the form
let <pattern> = <expression> in <expression>
You absolutely need to use a proper OCaml indentation tool for your editor to avoid this kind of errors.
One more thing. I am not sure your use of != is ok. This is physical pointer comparison. Probably you want to use <>, the structural comparison.
The OP tried to edit and delete all of the answer due to "personal reasons". I myself skipped the edit approval and left it to the community, which apparently rejected it. Meta SO discussion about this kind of thing is found at What to do when an OP asks to delete my code from my answer? , including what the OP should do.
I am new in swift, I have been working with it only few weeks and now I am trying to parse something like a price list from incoming string. It has the next format:
2.99 X 3.00 = 10 A
Some text here
1.22 X 1.5 10 A
And the hardest part is that sometime A or some digit is missing but X should be in the place.
I would like to find out how it is possible to use regex in swift (or something like that if it does not exist) to write a template for parsing the next value
d.dd X d.d SomeValueIfExists
I would very appreciate any useful information, topics to read or any other resources to get more knowledge about swift.
PS. I have access to the dev. forums but I've never used them before.
I did an example recentl, and maybe a little harder than necessary, to demonstrate RegEx use in Swift:
let str1: NSString = "I run 12 miles"
let str2 = "I run 12 miles"
let match = str1.rangeOfString("\\d+", options: .RegularExpressionSearch)
let finalStr = str1.substringWithRange(match).toInt()
let n: Double = 2.2*Double(finalStr!)
let newStr = str2.stringByReplacingOccurrencesOfString("\\d+", withString: "\(n)", options: NSStringCompareOptions.RegularExpressionSearch, range: nil)
println(newStr) //I run 26.4 miles
Two of these have "RegularExpressionSearch". If you put this in a playground you can see what each line does. Note the double \ escapes. One for the normal RegEx use and anther because \ is a special character in Swift.
Also a good article:
http://benscheirman.com/2014/06/regex-in-swift/
Me coming from a c# and python background, feels there must be a better way to read a file and populate a classic F# list. But then I know that a f# list is immutable. There must be an alternative using a List<string> object and calling its Add method.
So far what I have at hand:
let ptr = new StreamReader("stop-words.txt")
let lst = new List<string>()
let ProcessLine line =
match line with
| null -> false
| s ->
lst.Add(s)
true
while ProcessLine (ptr.ReadLine()) do ()
If I were to write the similar stuff in python I'd do something like:
[x[:-1] for x in open('stop-words.txt')]
Simple solution
System.IO.File.ReadAllLines(filename) |> List.ofArray
Although you can write a recursive function
let processline fname =
let file = new System.IO.StreamReader("stop-words.txt")
let rec dowork() =
match file.ReadLine() with
|null -> []
|t -> t::(dowork())
dowork()
If you want to read all lines from a file, you can just use ReadAllLines. The method returns the data as an array, but you can easily turn that into F# list using List.ofArray or process it using the functions in the Seq module:
open System.IO
File.ReadAllLines("stop-words.txt")
Alternatively, if you do not want to read all the contents into memory, you can use File.ReadLines which reads the lines lazily.
This is the error I'm getting and I have no idea why: "Error: Unbound record field label length "
Does anyonw know?
let rastavi str =
let sublist = ref [] in
let list = ref [] in
for i = ((str.length str)1) [down]to 0 do
if str.[i] =' ' then (str.[i] :: !sublist)
else (list := (!sublist:: !list)) sublist = []
done ;;
You're using OO notation to get the length of a string. OCaml uses functional notation. So it looks like this:
String.length str
Not like this:
str.length (* OO notation, not in OCaml *)
Edit:
Side comment: this solution is very much an imperative take on the problem. If you're trying to learn the FP mindset, you should try to think recursively and immutably. Since this looks like homework, it's very likely a functional solution is what you want.
But here are a few other problems in your original code:
You have two expressions next to each other with nothing in between. If you want to "do" two things, you need to separate them with a semicolon ; (however, this is imperative style)
You're using = which compares two values for equality. If you want to assign a value to a reference you need to use :=. (Imperative style, again.)