Is there any way to get the number of "source code lines" that could result from calling nested function?
For this question my example:
Selection of functions
Function name
Lines of Code (LOC) [function body]
Function1
10
Function2
20
Function3
20
Example 1
Function1
...|-> calls Function2
......|-> calls Function3
Sum of LOC= 50
Example 2
Function1
...|-> calls Function3
......|-> calls Function2
Sum of LOC= 50
Example 3
Function1
..|-> calls Function2
..|-> calls Function3
..|-> calls Function2
Sum of LOC= 70
Here is a code query that computes the Response for each method of a code base:
from m in Application.Methods
let methodsCalledMetric =
m.MethodsCalled.FillIterative(
methods => methods.SelectMany(mm => mm.MethodsCalled))
let methodsCalled = methodsCalledMetric.DefinitionDomain
let responseLoc = methodsCalled.Sum(mm => mm.NbLinesOfCode)
select new { m, methodsCalled, responseLoc }
The result looks like:
The set of methods called by any method can be either listed (by clicking the 136 methods cell for example) or exported to the dependency graph to visualize it in a clearer way:
Related
I have a function called collatz and I need to find how many times it calls itself, but I'm not allowed to use any vars.
The function works perfectly with vars but i have to use vals.
In case sinanspd's answer does not answer your question, here is some example code.
Let us say your function collatz has a signature as follows:
def collatz(input: Type1): Type2 = ???
The way to count how many times it is called is to either modify collatz itself, or else to use some wrapper function.
def counter(oldCount: Int, fun: Type1 => Type2, input: Type1): (Int, Type2) = {
val output = fun(input)
(oldCount + 1, output)
}
The first time you call counter, call it with oldCount set to 0. Every time you call counter, pass in the old version of count. The first value of your output tuple will be the new count.
I have this function f
f:{{z+x*y}[x]/[y]}
I am able to call f without a 3rd parameter and I get that, but how is the inner {z+x*y} able to complete without a third parameter?
kdb will assume, if given a single list to a function which takes two parameters, that you want the first one to be x and the remainder to be y (within the context of over and scan, not in general). For example:
q){x+y}/[1;2 3 4]
10
can also be achieved by:
q){x+y}/[1 2 3 4]
10
This is likely what's happening in your example.
EDIT:
In particular, you would use this function like
q){{z+x*y}[x]/[y]}[2;3 4 5 6]
56
which is equivalent to (due to the projection of x):
q){y+2*x}/[3 4 5 6]
56
which is equivalent to (due to my original point above):
q){y+2*x}/[3;4 5 6]
56
Which explains why the "third" parameter wasn't needed
You need to understand 2 things: 'over' behavior with dyadic functions and projection.
1. Understand how over/scan works on dyadic function:
http://code.kx.com/q/ref/adverbs/#over
If you have a list like (x1,x2,x3) and funtion 'f' then
f/(x1,x2,x3) ~ f[ f[x1;x2];x3]
So in every iteration it takes one element from list which will be 'y' and result from last iteration will be 'x'. Except in first iteration where first element will be 'x' and second 'y'.
Ex:
q) f:{x*y} / call with -> f/ (4 5 6)
first iteration : x=4, y=5, result=20
second iteration: x=20, y=6, result=120
2. Projection:
Lets take an example funtion f3 which takes 3 parameters:
q) f3:{[a;b;c] a+b+c}
now we can project it to f2 by fixing (passing) one parameter
q) f2:f3[4] / which means=> f2={[b;c] 4+b+c}
so f2 is dyadic now- it accepts only 2 parameters.
So now coming to your example and applying above 2 concepts, inner function will eventually become dyadic because of projection and then finally 'over' function works on this new dyadic function.
We can rewrite the function as :
f:{
f3:{z+x*y};
f2:f3[x];
f2/y
}
It was suggested in this comment that there is a difference between how Matlab and Python pass around functions. From what I can tell by looking and using the two, there is no difference between the two, but maybe I'm missing something?
In Matlab, you would create a quick function handle like this:
fun = #(x) x.^2 + 1;
In Python, using a lambda function, you could create a similar function like this:
def fun(x):
return x^2
In both languages, it's possible to send the term 'fun' to another function as an argument - but the commenter I linked to insinuated that they are not the same and/or need to be used differently.
What am I missing?
The first comment seems to simply reiterate the idea that you can pass a MATLAB function handle as an argument (although the answer didn't state anything that would make me think otherwise). The second comment seemed to interpret this to mean that the first commenter thought that you couldn't do this in Python and responded to state that you can use either a lambda or pass the function directly.
Regardless, assuming that you use them correctly, a function handle in MATLAB is functionally equivalent to using either a lambda or function object as an input argument in Python.
In python, if you don't append the () to the end of the function, it doesn't execute the function and instead yields the function object which can then be passed to another function.
# Function which accepts a function as an input
def evalute(func, val)
# Execute the function that's passed in
return func(val)
# Standard function definition
def square_and_add(x):
return x**2 + 1
# Create a lambda function which does the same thing.
lambda_square_and_add = lambda x: x**2 + 1
# Now pass the function to another function directly
evaluate(square_and_add, 2)
# Or pass a lambda function to the other function
evaluate(lambda_square_and_add, 2)
In MATLAB, you have to use a function handle because MATLAB attempts to execute a function even if you omit the ().
function res = evaluate(func, val)
res = func(val)
end
function y = square_and_add(x)
y = x^2 + 1;
end
%// Will try to execute square_and_add with no inputs resulting in an error
evaluate(square_and_add)
%// Must use a function handle
evaluate(#square_and_add, 2)
In the following example:
def maybeTwice2(b: Boolean, i: => Int) = {
lazy val j = i
if (b) j+j else 0
}
Why is hi not printed twice when I call it like:
maybeTwice2(true, { println("hi"); 1+41 })
This example is actually from the book "Functional Programming in Scala" and the reason given as why "hi" not getting printed twice is not convincing enough for me. So just thought of asking this here!
So i is a function that gives an integer right? When you call the method you pass b as true and the if statement's first branch is executed.
What happens is that j is set to i and the first time it is later used in a computation it executes the function, printing "hi" and caching the resulting value 1 + 41 = 42. The second time it is used the resulting value is already computed and hence the function returns 84, without needing to compute the function twice because of the lazy val j.
This SO answer explores how a lazy val is internally implemented. In j + j, j is a lazy val, which amounts to a function which executes the code you provide for the definition of the lazy val, returns an integer and caches it for further calls. So it prints hi and returns 1+41 = 42. Then the second j gets evaluated, and calls the same function. Except this time, instead of running your code, it fetches the value (42) from the cache. The two integers are then added (returning 84).
I have couple methods in Matlab class. I am accessing few of them in some of these methods but somehow, in order to call those methods, I need to place "instaceName." in start of method names. I have c# background and does not feel comfortable. In C#, different level of visibility of methods can be defined.
classdef SampleClass
%UNTITLED2 Summary of this class goes here
% Detailed explanation goes here
properties
result ;
end
methods
function method1 (obj, a , b)
obj.result = obj.method2 (a,b) ;
end
end
methods (Access= private)
function y = method3 ( a , b)
y = a + b ;
end
end
end
If I define methods as static which I want to use internally in that class then will it work.
Is there a suggestion to deal this issue.
classdef SampleClass
%UNTITLED2 Summary of this class goes here
% Detailed explanation goes here
properties
result ;
end
methods
function method1 (obj, a , b)
obj.result = SampleClass.method2 (a,b) ;
end
end
methods (Static)
function y = method2 ( a , b)
y = a + b ;
end
end
end
Unfortunately there is no workaround for the issue that you mention. Class method signatures must always pass the "instance" into itself:
function output = DoSomething(this, arg1, arg2)
There are several methods to call a class method (take for example the above method DoSomething from Class MyClass):
my_instance = MyClass(...);
% Call type 1
output = my_instance.DoSomething(arg1, arg2)
% Call type 2
output = DoSomething(my_instance, arg1, arg2)
The type 2 feels more intuitive and aligned with the function signature.
In your example, both the instance method and the static method are interchangeable because you are not using method properties. But that's not always the case: static methods cannot access the private/protected properties of the class.
Extra note: as far as I know, many OO languages do the same but behind the scenes (C++ and C# automatically add the instance argument to the method, but correct me if I am wrong). The only difference is that in Matlab you must do it explicitly.
C++ explanation.