I am trying to write a function to call a function from a package, snippets as below:
library(optionstrat)
# sameple detla
# do not run
# calldelta(s,x,sigma,t,r)
# putdelta(s,x,sigma,t,r)
x=10
sigma=0.25
t=0.25
r=0.05
delta<-function(option_type,stock_price) {
if (option_type="c") {
delta<-calldelta(s,x,sigma,t,r)
} else {
delta<-putdelta(s,x,sigma,t,r)
}
}
both calldelta and putdelta are built in functions from optionstrat package, and I would like to write a function so that if option_type="c", then return with a call delta value based on the stock price input. Likewise, if option_type!="c", then return with a put delta value based on the stock price input.
My end goal here is to come up with a function like delta(c,10) then return with a call delta value based on stock price 10. May I know how should I do from here? Thanks.
Update 1:
Now I try working with the below snippets:
x=10
sigma=0.25
t=0.25
r=0.05
stock_delta<-function(option_type,s) {
if (option_type="c") {
delta<-calldelta(s,x,sigma,t,r)
} else {
delta<-putdelta(s,x,sigma,t,r)
}
}
And again, if I define optiontype equals to c & s equals to 10, the same warning msg is returned...
Update 2:
Thanks to #akrun, now the function is created & now I would like to add one more return value from the delta function by adding:
calleval(s,x,sigma,t,r)$Gamma
puteval(s,x,sigma,t,r)$Gamma
The first line will return with the call gamma value & the latter will return with put gamma. May I know how do I string it with the function written previously?
There are multiple issues in the function - 1) function arguments passed should match the arguments to the inner function, 2) = is assignment and == is comparison operator, 3) if the last statement is assigned to an object, it wouldn't print on the console. We may either need to return(delta) or in this case there is no need to create an object delta inside (when the function name is also the same), 4) Passing unquoted argument (c) checks for object name c and even have an additional issue as c is also a function name. Instead, pass a string "c" as is expected in the if condition
delta<-function(option_type,stock_price)
{
if (option_type=="c")
calldelta(stock_price,x,sigma,t,r)
else
putdelta(stock_price,x,sigma,t,r)
}
-testing
> delta("c", 10)
[1] 0.5645439
I am writing a custom User defined function in kusto where I want to have some optional parameters to the function which will be used in the "where" clause. I want to dynamically handle this. For example: If the value is present, then my where clause should consider that column filter, else it should not be included in the "where" clause>
Eg (psuedo code where value is not null):
function Calculate(string:test)
{
T | where test == test | order by timestamp
}
Eg (psuedo code where value is null or empty. My final query should look like this):
function Calculate(string:test)
{
T | order by timestamp
}
What is the efficient way to implement this. I will call this function from my c# class.
you can define a function with a default value for its argument, and use the logical or operator and the isempty() function to implement the condition you've described.
for example:
(note: the following is demonstrated using a let statement, but can be applied similarly to stored functions)
let T = range x from 1 to 5 step 1 | project x = tostring(x)
;
let F = (_x: string = "") {
T
| where isempty(_x) or _x == x
| order by x desc
}
;
F("abc")
if you run F() or F("") (i.e. no argument, or an empty string as the argument) - it will return all values 1-5.
if you run F("3") - it will return a single record with the value 3.
if you run F("abc") - it will return no records.
I'm new to Rust, and I'm trying to make an interface where the user can choose a file by typing the filename from a list of available files.
This function is supposed to return the DirEntry corresponding to the chosen file:
fn ask_user_to_pick_file(available_files: Vec<DirEntry>) -> DirEntry {
println!("Which month would you like to sum?");
print_file_names(&available_files);
let input = read_line_from_stdin();
let chosen = available_files.iter()
.find(|dir_entry| dir_entry.file_name().into_string().unwrap() == input )
.expect("didnt match any files");
return chosen
}
However, it appears chosen is somehow borrowed here? I get the following error:
35 | return chosen
| ^^^^^^ expected struct `DirEntry`, found `&DirEntry`
Is there a way I can "unborrow" it? Or do I have to implement the Copy trait for DirEntry?
If it matters I don't care about theVec after this method, so if "unborrowing" chosen destroys the Vec, thats okay by me (as long as the compiler agrees).
Use into_iter() instead of iter() so you get owned values instead of references out of the iterator. After that change the code will compile and work as expected:
fn ask_user_to_pick_file(available_files: Vec<DirEntry>) -> DirEntry {
println!("Which month would you like to sum?");
print_file_names(&available_files);
let input = read_line_from_stdin();
let chosen = available_files
.into_iter() // changed from iter() to into_iter() here
.find(|dir_entry| dir_entry.file_name().into_string().unwrap() == input)
.expect("didnt match any files");
chosen
}
I'm trying to pass a cursor to a function like this:
.create-or-alter function GetLatestValues(cursor:string) {
Logs | where cursor_after(cursor)
}
But I get a Error cursor_after(): argument #1 is invalid response. Is there something I'm missing? Is string the wrong data type to use?
This approach works properly for lambda functions:
let GetLatestValues = (cursor:string) {
Logs | where cursor_after(cursor)
};
GetLatestValues('') | take 1
By default, defining a function in Kusto validates stored function, and in case you're using cursor the system will attempt to do it with empty parameter, leading to failure.
You can override this behavior using skipvalidation=true parameter (see docs at:
https://learn.microsoft.com/en-us/azure/kusto/management/functions#create-function)
.create-or-alter function with (skipvalidation = "true")
GetLatestValues(cursor:string)
{
Logs | where cursor_after(cursor)
}
I need to evaluate an expression which I am supposed to append.
eAddressType can be M, E, PH or FX
What will be the best way to evaluate the expression initData.alternateContactLabelM using a combination of (alternateContactLabel + eAddressType)?
<ux-list-item primary_text="{{initData.alternateContactLabel + this.implEAddressType.eAddressType}}"
secondary_text="{{this.value}}"
cta="{text: '{{initData.uxButtonChangeLabel}}', onclick: 'alternateContactClick:{{index}},{{this.implEAddressType.eAddressType}}'}"
ariaLabel="{{initData.uxButtonChangeLabel}} {{initData.alternateContactLabelPH}}">
</ux-list-item>
In JSON I have:
"initData": {
"alternateContactLabelM":"Alternative Mobile Number (SMS)",
"alternateContactLabelE":"Alternative Email Address",
"alternateContactLabelPH":"Alternative Contact Number",
"alternateContactLabelFX":"Alternative Fax",
}
You will need to use a combination of a custom helper to produce the name of the key and the built-in lookup helper to get the value of that property on the initData object.
Handlebars helpers for string concatentation have doubtlessly been written before, but I will write my own simple implementation here:
Handlebars.registerHelper('concat', function () {
return Array.prototype.slice.call(arguments, 0, -1).join('');
});
This helper concatenates all of the parameters passed to it except for the last parameter, which we know is the Handlebars options object.
We can now use this helper in our template to dynamically return the key name. We will then use the lookup helper to find the value of initData at that key:
primary_text="{{lookup initData (concat 'alternateContactLabel' implEAddressType.eAddressType)}}"
For a working example, please see this fiddle.