When I was reading spark source code here, I saw code like $(a_variable).
What does it mean?
I copy the code here:
final val blockSize: IntParam = new IntParam(this, "blockSize",
"Block size for stacking input data in matrices. Data is stacked within partitions." +
" If block size is more than remaining data in a partition then " +
"it is adjusted to the size of this data. Recommended size is between 10 and 1000",
ParamValidators.gt(0))
/** #group getParam */
final def getBlockSize: Int = $(blockSize)
That isn't special Scala syntax, it's a method name. In Scala $ is a legal identifier. The method is inherited from the org.apache.spark.ml.param.Param trait.
See the source.
I believe that the dollar sign is usually used for string interpolation. What that means is that if I want to use a val(ue)/var(iable) inside a string (denoted with an s before the first double quotation), I can access such vals/vars using the $ sign. Otherwise, we won't be able to use vals/vars inside a string since it would not know how to escape the string characters.
Example:
val favoriteFruit = "strawberry"
val strawberryCount = 12
println(s"My favorite fruit is ${favoriteFruit}. I've had ${strawberryCount} pieces today!") // string interpolation
In your case, however it seems that $ is a shortcut to getOrDefault the var/val (as sourced by #Michael Zajac and #kingledion...gets the value/variable. If it does not exist, gets the default value/variable). Using getOrDefault may be a more robust solution in cases in which you expect the parameter to not always have a corresponding value, for which you can set a default value.
Related
I currently have two maps: -
mapBuffer = Map[String, ListBuffer[(Int, String, Float)]
personalMapBuffer = Map[mapBuffer, String]
The idea of what I'm trying to do is create a list of something, and then allow a user to create a personalised list which includes a comment, so they'd have their own list of maps.
I am simply trying to print information as everything is good from the above.
To print the Key from mapBuffer, I use: -
mapBuffer.foreach(line => println(line._1))
This returns: -
Sample String 1
Sample String 2
To print the same thing from personalMapBuffer, I am using: -
personalMapBuffer.foreach(line => println(line._1.map(_._1)))
However, this returns: -
List(Sample String 1)
List(Sample String 2)
I obviously would like it to just return "Sample String" and remove the List() aspect. I'm assuming this has something to do with the .map function, although this was the only way I could find to access a tuple within a tuple. Is there a simple way to remove the data type? I was hoping for something simple like: -
line._1.map(_._1).removeDataType
But obviously no such pre-function exists. I'm very new to Scala so this might be something extremely simple (which I hope it is haha) or it could be a bit more complex. Any help would be great.
Thanks.
What you see if default List.toString behaviour. You build your own string with mkString operation :
val separator = ","
personalMapBuffer.foreach(line => println(line._1.map(_._1.mkString(separator))))
which will produce desired result of Sample String 1 or Sample String 1, Sample String 2 if there will be 2 strings.
Hope this helps!
I have found a way to get the result I was looking for, however I'm not sure if it's the best way.
The .map() method just returns a collection. You can see more info on that here:- https://www.geeksforgeeks.org/scala-map-method/
By using any sort of specific element finder at the end, I'm able to return only the element and not the data type. For example: -
line._1.map(_._1).head
As I was writing this Ivan Kurchenko replied above suggesting I use .mkString. This also works and looks a little bit better than .head in my mind.
line._1.map(_._1).mkString("")
Again, I'm not 100% if this is the most efficient way but if it is necessary for something, this way has worked for me for now.
I want to access data using Odata, but there is $expand. I know that when using $ in flutter, it will call new value
access data using odata in flutter
https://192.168.x.x/odata/UserLokasiOdata?$expand=HSEOL_TR_Relate_USERLOK_LOKASI&$filter=USERID eq 'jubang'
But in flutter can not show the respond anything
In a raw string, "$" and "" mean nothing special, they are just characters like any other. In an interpreted string, "$" starts an interpolation and "" starts an escape.
Since you want the interpolation for "$dollars", you can't use "$" literally, so you need to escape it:
int dollars = 100;
print("I have \$$dollars."); //I have $100.
If you don't want to use an escape, you can combine the string from raw and interpreted parts:
int dollars = 100;
print(r"I have $" "$dollars."); //I have $100.
Two adjacent string literals are combined into one string, even if they are different types of string.
or
When you are using literals instead of variables you can also use raw strings:
print(r"I have $100."); //I have $100.
In kdb I would like to have a function which takes a string as a parameter.
myfunc: {[strParam]
....
}
However when I tried to invoke the function.
q) myfunc["test"]
It complains of length error. It seems that function sees "test" as passing 4 char parameters. How can I make the function expect a string?
A string in kdb is defined as a list of characters and so functions using them have to be able to deal with this.
q)count "test"
4
You can also use a symbol instead casting from a string using `symbol$"test". A symbol is atomic and fixed width so can be useful to use as keys to a dictionary or in a table. Some functions for strings will still work on symbols e.g
q)upper `test
`TEST
while list operation will not work and you will have to turn it back into a string using string `test before using those operations.
When a length error is thrown and you go into the debug mode as shown by the q prompt having two brackets q)), you can use the functions .z.ex to show the failed function and .z.ey to see the failed arguments to narrow down which part is throwing the error.
The error can appear due to multiple reasons.
q)f:{[p] show p} //it works fine with any number of elements
q)f["abc"]
"abc"
f:{[p] enlist[`k]!p} //needs a list of single element
f["abc"]
'length
f[enlist "abc"]
(enlist `k)!enlist "abc"
q)f:{[p] 1 2 3 4!p} //the length of values is different from the length of keys
q)f["abc"]
'length
q)f["abcd"]
(1j, 2j, 3j, 4j)!"abcd"
I've been looking all over the web to find out what the expression ## means in chisel, but can't find it anywhere.
For example in this code snippet:
val ways = Module(new BRAM(log2Up(conf.lines), conf.ways * line_size))
val din = Vec.fill(conf.ways) { Bits(width=line_size) }
if(conf.ways == 2) {
ways.io.din := din(1) ## din(0)
}
What is the line in the if-statement doing using the ## expression?
Thanks!
Since you can't really google for symbolic characters such as #, you'll need to find the documentation for the ## method another way. The simplest way would be to just hover over ## in your IDE and let it display the API doc. If this isn't possible because your IDE doesn't do that or you haven't downloaded the API docs, you can find the docs for whichever class the method is called on and look it up there.
In this case you're calling ## on din(1). Since din is a vector of Bits, din(1) is a Bits object. So we can look up the Bits class in the Chisel API docs and find the following on ##:
def ##(other: Bits): UInt
Returns this wire concatenated with other, where this wire forms the most significant part and other forms the least significant part.
The width of the output is sum of the inputs.
I am using scopt to parse command line arguments in scala. I want it to be able to parse options with more than one value. For instance, the range option, if specified, should take exactly two values.
--range 25 45
Coming, from python background, I am basically looking for a way to do the following with scopt instead of python's argparse:
parser.add_argument("--range", default=None, nargs=2, type=float,
metavar=('start', 'end'),
help=(" Foo bar start and stop "))
I dont think minOccurs and maxOccurs solves my problem exactly, nor the key:value example in its help.
Looking at the source code, this is not possible. The Read type class used has a member tuplesToRead, but it doesn't seem to be working when you force it to 2 instead of 1. You will have to make a feature request, I guess, or work around this by using --min 25 --max 45, or --range '25 45' with a custom Read instance that splits this string into two parts. As #roterl noted, this is not a standard way of parsing.
It should be ok if only your values are delimited with something else than a space...
--range 25-45
... although you need to split them manually. Parse it with something like:
opt[String]('r', "range").action { (x, c) =>
val rx = "([0-9]+)\\-([0-9]+)".r
val rx(from, to) = x
c.copy(from = from.toInt, to = to.toInt)
}
// ...
println(s" Got range ${parsedArgs.from}..${parsedArgs.to}")