perl6 signatures in POD - return

since --> is the preferred way to state the return type in a signature in perl6 I am wondering if or how it is possible to put the code of a function signature into C<...>.
e.g. C<foo(Int $a --> Bool)>

You can use C<<foo(Int $a --> Bool)>>.

Related

Invalid coercion: () as xs:string in xdmp:xslt-eval

I am making use of MarkLogic's ability to call XQuery functions in the XSL transform.
Let's say I have an XQuery library with a function whose signature looks like the following. This is for illustrative purposes only.
declare function my-func:ex-join($first as xs:string, $last as xs:string) as xs:string
{
fn:concat($first, '-', $last)
}
From XQuery, I can call this function with empty sequence as parameters, with no issues, i.e.
ex-join((), '1244')
The function will just return an empty sequence, but I don't get any errors. If I try to all this function from with in my XSL transform,as in:
<xsl:value-of select="my-func:ex-join(//node/value/text(), 'something')"/>
If the /node/value does not exist, and an empty sequence is passed in, I get the coercion error.
Does anyone have suggestions to work around the coercion problem, outside of checking the values in XSL prior to the select? These are auto-generated XSL templates, which would mean a lot of coded checks.
Thanks,
-tj
Attempts to invoke that function in XQuery would fail too. It is probably due to function mapping that you don't notice this though. Put the following at the top of your XQuery code:
declare option xdmp:mapping "false";
Next to this, you only need to change the signature of your function to accept empty-sequences. Replace as xs:string with as xs:string?:
declare function my-func:ex-join($first as xs:string?, $last as xs:string?) as xs:string
fn:concat will accept empty sequences as arguments, so no further changes required to make it work..
HTH!

how to format a throw statement in erlang

I am new to Erlang syntax and struggling with this
I can do this and compile
throw(Reason)
which is of type throw/1
I want to be able to do this:
%% I have seen this code in sample examples.
?THROW("Couldn't start process: ~p. ~n", [Reason])
I do not think there there is throw/2.
Then how can I define a macro like above?
?THROW is a macro. It should be define somewhere as:
-define(THROW(Format,Params),throw(io_lib:format(Format,Params))).
In this definition, the call to io_lib:format(Format,Params) returns a single string that is used as Reason by the function throw.

Why does io\open not work in moonscript?

Moonscript uses \ to call methods so can someone explain to me why the code below does not work:
> file = io\open("mix.exs", "rb")
[string "tmp"]:1: calling 'open' on bad self (string expected, got table)
but when you call it to read the file it does ?
> file\read!
"Code.ensure_loaded?(Hex) and Hex.start
The io.open function expects to get a string as the first argument but io\open (like io:open in lua itself) is actually passing the io table as the first argument. That is it is a method call.
io\open("file", "mode")/io:open("file", "mode") are syntactic sugar for io.open(io, "file", "mode").
This is why file\read! works without an explicit argument because file gets passed as the first argument to the read("file", "format") function.
Moonscript uses \ to call methods
to call member methods. as in a\b c, ... translates to a.b(a,c,...).
this doesn't work here because io.open is a static function (io.open(what,how)), not a member (io.open(self,what,how)).
you couldn't call io:open in Lua either. the only place where io functions allow for being called as members is when you want to read/write stdio.
but when you call it to read the file it does ?
because now it's a member method of the file. you're actually still using io.read there, but the file object has io as a metatable index, therefore allowing you to access the same function via file.read, and since file\read! translates to file.read(file) it's the same thing.
so essentially the answer boils down to "because io:open doesn't work in Lua".

Make a table containing tokens visible for both .mly and .mll by menhir

I would like to define a keyword_table which maps some strings to some tokens, and I would like to make this table visible for both parser.mly and lexer.mll.
It seems that the table has to be defined in parser.mly,
%{
open Utility (* where hash_table is defined to make a table from a list *)
let keyword_table = hash_table [
"Call", CALL; "Case", CASE; "Close", CLOSE; "Const", CONST;
"Declare", DECLARE; "DefBool", DEFBOOL; "DefByte", DEFBYTE ]
%}
However, I could NOT use it in lexer.mll, for instance
{
open Parser
let x = keyword_table (* doesn't work *)
let x = Parser.keyword_table (* doesn't work *)
let x = Parsing.keyword_table (* doesn't work *)
}
As this comment suggests, menhir has a solution for this, could anyone tell me any details?
The first option is to define tokens in a separate .mly file. Executing menhir for this file with --only-tokens option will generate a module containing type token that you can use in your parser compiled with --external-tokens option.
If this solves the problem with tokens, you can specify all other functions that are used by both parser and lexer in a separate file as Thomash suggested.
There is an alternative solution as well. You can use %parameter<module signature> declaration in the parser to parametrize the entire parser over type and function annotations specified inside given signature. The main advantage is that this signature is provided in the interface file for the parser, so the parser can share this signature with other modules (that can construct modules based on the signature).
I suggest to refer to menhir examples, namely see calc-two to get know about external tokens and to calc-param to know how to create parametrized parsers.
I usually put the keyword_tablein lexer.mll and I see no reason to put it in parser.mly.
If you need to access it from both lexer.mll and parser.mly (but why do you want to access it from parser.mly?), the easiest solution is to put it in a third file keyword.ml and use Keyword.keyword_table (or open Keyword and keyword_table).

F# how to AddHandler alike VB.NET

VB.NET code is :
AddHandler TheGrp.DataChanged, AddressOf theGrp_DataChange
So how can I do same with F# ?
theGrp.DataChanged.AddHandler(X.theGrp_DataChange)
Error 1 This function takes too many arguments, or is used in a context where a function is not expected
Try theGrp.DataChanged.AddHandler(fun o e -> X.theGrp_DataChange(o, e)). The signature for AddHandler indicates that it takes a delegate, so you can either explicitly create one (via something like DataChangedEventHandler(fun o e -> X.theGrp_DataChange(o, e))) or you can let the compiler implicitly add the delegate constructor when given a function definition, but you can't just use the method itself.
Alternatively, if you don't want to create a lambda expression explicitly, you can also write (In this case, the function signature matches the signature required by the delegate, so it should work):
theGrp.DataChanged.AddHandler(DataChangedEventHandler(x.theGrp_DataChanged))
Also, if you don't need the sender argument, you can declare the theGrp_DataChanged method to take only the event args argument and then write just:
theGrp.DataChanged.Add(x.theGrp_DataChanged)

Resources