What's the syntax on this line of code? - printing

What's the syntax on this line of code (part of a script that create a html window and prints?)
function (write)
{var ctx=$(this)[offset]();
ctx[drawImage](this,ctx[left]-slidePos[left],ctx[top]-slidePos[top]);
});

This invokes the jQuery function and returns a jQuery object (referencing this in the current context):
$(this)
An object's properties (some of which may be functions) can be indexed on the object, so this is indexing a specific property:
$(this)[offset]
Turns out this property is a function, because it's invoked:
$(this)[offset]()
The returned result of that function is stored in a variable:
var ctx=$(this)[offset]()
That variable is presumably another object, which can also have its properties indexed:
ctx[drawImage]
And that indexed property can also be a function:
ctx[drawImage]()
That function takes three arguments:
ctx[drawImage](this,ctx[left]-slidePos[left],ctx[top]-slidePos[top])
The first argument is simply this in the current context. The second argument is an arithmetic expression of another property on that object minus a property on another object:
ctx[left]-slidePos[left]
So is the third argument:
ctx[top]-slidePos[top]
These two lines together are encapsulated within a function:
function (write)
{
// ...
}
Oddly enough, that function doesn't appear to use the write argument that it expects.

Related

Someone explain What goes on in the 2nd line of code? Why does map function use name while the variable name is names? Flutter/dart

final names = ['Max', 'Manu', 'Julie'];
final result = names.map((name) => Text(name)).toList();
map() expects as an argument a function. In this case,
(name) => Text(name)
is an anonymous function, i.e. it is not bound to a name. Similarly to how you can have values like 3 or "a string" without binding them to a variable, so they have no "name", you can have unnamed functions.
The syntax of the anonymous function is (<arguments>) => <what to do with the arguments>. This function has one argument, name, and it takes it and makes a Text with it.
As stated in a previous answer, the map method expects a Function as it's argument.
Imagine the code was like this:
Text stringToText(String name){
return Text(name);
}
final names = ['Max', 'Manu', 'Julie'];
final result = names.map(stringToText).toList();
This makes it pretty clear. Each String in the names list will be passed into the stringToText function, and a Text object is cranked out. The stringToText function says, "whatever my input is, I'm gonna call that 'name'. Whenever I use 'name', I'm referring to my input value".
But, this is only nice and easy to follow because the function that we're passing has a name.
As stated in a previous answer, you can create an anonymous function - a function with no name.
If it's a single line that returns something, like in our case, you can use a "fat-arrow" function, like this:
(inputVariableName) => OutputObjectProducer(inputVariableName)
If it's more than one line, you can create it in a standard form, but with no name and no listed return type, like so (you can even leave out the types of the parameters because the compiler can already tell what the types are supposed to be!):
(inputVariableName) {
var someNeededVariable = someCalculation();
//... some other stuff here
return OutputObjectProducer(someNeededVariable);
}
You could even create the function as an Object like so:
Function stringToText = (String name) => Text(name);
As of the time of this writing, you can't pass a constructor as a Function reference even if the signature is a match.
I hope this helped clear things up: the name name is a newly defined parameter for an anonymous function defined within the call to .map

Lua functions: how to use tables as function arguments and self-document the code

When I have functions which take many arguments it's sometimes useful to pass a single table as an argument instead of many local variables.
function example_1(arg_one, arg_two, arg_three)
end
becomes
function example_2(arg_table)
arg_table.arg_one, arg_table.arg_two, arg_table.arg_three
end
The problem is, when calling the function somewhere else in code, it's hard to remember what arg_table needs to consist of. There are plenty of code-completion plugins for many editors which will help you with remembering the arguments for the example_1 function, but not for example_2.
Is there any way to write the example_2 function with a table parameter in a way that is still a table, but also shows the necessary parameters for the function inside the ()?
something like this (which does not work):
function example_2(arg_table = {arg_one, arg_two, arg_three})
end
Write your formal parameter list and documentation with separate parameters, as usual. Then document that if the first (and only) actual argument is a table, the effective arguments will be taken from the table using the formal parameter names as string keys.
You can keep the function parameters as they are and pass a table for convenience.
For example:
function example(arg_one, arg_two, arg_three)
-- do stuff
end
local tbl = {"value for arg_one", "value for arg_two", "value for arg_three"}
example(table.unpack(tbl))
Note that this doesnt work for tables with named keys.

What does a colon after a tuple but before another type mean within a method signature?

What does a colon that's positioned after a tuple but before another type mean within a method signature?
Here's the syntax:
member this.Post (portalId : string, req : PushRequestDtr) : IHttpActionResult =
Here's the context:
type PushController (imp) =
    inherit ApiController ()
    member this.Post (portalId : string, req : PushRequestDtr) : IHttpActionResult =
        match imp req with
        | Success () -> this.Ok () :> _
        | Failure (ValidationFailure msg) -> this.BadRequest msg :> _
        | Failure (IntegrationFailure msg) ->
            this.InternalServerError (InvalidOperationException msg) :> _
Specifically, what does this method signature mean?
Does this method take two parameters or one parameter?
I understand this:
(portalId : string, req : PushRequestDtr)
But I'm confused about this syntax that's appended to the end of it:
: IHttpActionResult
That would be the return type, i.e. the type of the value returned by the method.
From the F# online docummentation:
https://learn.microsoft.com/en-us/dotnet/articles/fsharp/language-reference/members/methods
// Instance method definition.
[ attributes ]
member [inline] self-identifier.method-nameparameter-list [ : return-type ]=
method-body
In this case return_type is IHttpActionResult, which means this method will return an object that implements the IHttpActionResult interface.
Also, although (portalId : string, req : PushRequestDtr) looks like a tuple (and in a certain way it is syntax-wise) it is not, in fact, treated as a tuple. In this case, this is a specific F# syntax for declaring method arguments while defining a method of a F# object. This is the part represented by method-nameparameter-list in the F# method template declaration. This means that the Post method receives two arguments: portalId and req, not a single argument as a tuple.
Specifically, this syntax of a list of arguments that looks like a tuple but that they are not a tuple has to be used when declaring method arguments instead of function arguments. The member keyword is the one that makes this line a method declaration instead of a function declaration.
--
Regarding the :> operator: This is a cast operator. More specifically a upcasting operator (which changes the type of a more derived type to the type of some higher type in the type hierarchy).
In this case, it is being used to explicitly tell the compiler that each branch in the match expression will return some type that is derived (or that implements) IHttpActionResult. I am not quite sure why this cast is needed (something to do with F# not being able to infer the correct type in this context, see this other question: Type mismatch error. F# type inference fail?) but in fact, it is casting every possible return value to IHttpActionResult which is the method's return type.
https://learn.microsoft.com/en-us/dotnet/articles/fsharp/language-reference/casting-and-conversions

using a computed property in a template dom repeat seems to not actually get data

The use case I have been working with is that i have some markup as follows.
<my-radiolist value="{{myValue}}">
<template is="dom-repeat" items="{{test}}">
<my-radiobutton value="{{item}}" label="{{item}}"></my-radiobutton>
</template>
</my-radiolist>
and when test is defined as:
#property List<int> test = [1,2,3];
it works just fine.
Now, I wanted to populate this list by looping over a more complicated object. I wanted to make it computed, and then execute the given function. I update the sample as follows:
#Property(computed:"getTest()")
List<Map> test = null;
#reflectable
List<Map> getTest() {
print("Inside my getTest Function");
return [{"key": "test", "value": "test"}, {"key":"test 2","value":"test 2"}];
}
I noticed that my print statement has never fired, so my property has my default value which means there is nothing to iterate over.
I have done these before, but not for template repeaters. Is there something I am doing wrong?
My thought is that since computed updates when an argument is updated, and since i am not using any arguments, using a computed property in this case is not the course of action I should take.
The value is interpreted as a method name and argument list. The method is invoked to calculate the value whenever any of the argument values changes. Computed properties should never be written to directly. See Computed properties for more information.
When there are no parameters, then in my opinion a computed property doesn't make sense here anyway.
See also https://github.com/dart-lang/polymer-dart/wiki/properties#computed-properties

Extract function body

How can I extract function body (as a string)? For example I call C function, extract function from stack, check if type is LUA_TFUNCTION and what do I need to do to get its body?
When the function is on the stack, it has already been compiled. The best you can try to do is a lua_dump and then decode the bytecode.
You can call lua_getinfo with a string parameter of "S", then check the "source" member of the lua_Debug structure. If that string starts with '#', it's a filename, and you'll need to re-read the file if you want the source (Lua only read the file incrementally to load the function and never saved it as a string). Otherwise, its contents will be the string loaded as the chunk the function was defined in.
Note that in either case the source returned will be the entire chunk that defined the function in question. You can narrow the string down to only that function using the other fields defined in the structure: note, however, that this is not a guarantee that you will be able to load that string back in to get the same behavior (the function definition may refer to variables defined in an outer scope, for instance).
The Debug library can do this. The Lua C API doesn't have it, you'd want to call a Lua function for this purpose.

Resources