How to call a separate function file in swift 4 - swift

I am used to working with Matlab and am struggling to work out how to do some of the same things in swift.
In Matlab, I can create a function in a separate file and save it (in the same directory) as function1.m
and in my second function "function2.m" I can simply call the function with
function1(Input arguments) and it calls this separate file within "function 2".
is there any way to save just a function file in swift and call it in a separate playground or project. I am aware of how to call a function from within the same file in swift, however, if this function is written in a separate file, is there any way to call it, without having to explicitly copy/rewrite the function in the new file.
I have a file saved in the same directory that is called Fibonacci.swift which just contains a function for calculating the Fibonacci series, the function is called fibonacci and within that file i can call it simply by fibonacci(until: Int)
i have tried calling this fibonacci function from a separate file the same way with;
fibonacci (until: Int)
and also tried
Import Fibonacci.swift but this was more of a "I dont know what i'm doing" attempt
neither of the above attempts worked, so any help would be appreciated.
Cheers
- HB

Your function needed to be public:
public func fibonacci(until: Int) {
// Do something
}

Related

Does anonymous function requires lambda format (println ex)?

I got to know recently that Anonymous function
is given in the form of: function body that follows argument Lambda
(=>)
and it has no name
is used once in the code
So, you don't have to add a complete function to do this but rather, you can put the function body in use directly in your code (main function).
I read that in spark, "println" statement is always considered anonymous method for the following reasons:
println statement is considered a function body
Moreover, it wasn't added to a method with a name but was rather
used directly in the main class.
Also, it was used once in the code.
Ex:
Before converting to anonymous
click to view pic1
After converting to anonymous
click to view pic2
However, my question is what if the Lambda function wasn't used.. will println be considered as anonymous function still as shown in the ex below?
main question Ex:
click to view pic3
println is a function, but it is not an anonymous function since it has a name.

Command syntax for matlab function still generates output?

There are two ways to call functions in Matlab, the command syntax and function syntax.
I am viewing a code written by someone else in which there's a statement as follows in one .m file:
params=sys_params;
while sys_params is defined as a function in another .m file as:
function params=sys_params()
params happens to be a structure.
What I wish to know is, if according to Matlab documentation, a command syntax cannot be used to output from a function, then how is the first statement working perfectly well?
Two things:
The distinction between command and function syntax comes into play when arguments are passed.
The parentheses for calling a function in MATLAB are optional when calling with no arguments. MATLAB will call the function without an invoking () unlike some other languages.
One exception to this that comes to mind is that () is required to invoke a function handle/anonymous function.
From Calling Functions:
To call a function that does not require any inputs and does not return any outputs, type only the function name
The one ambiguous thing not explicitly told there is that assigning output of such a function call is perfectly valid.
I'll note that I don't really like that () is optional as it hides function calls at-first-glance. Therefore, I try to use () as often as possible to make it clear I am invoking a function, so nearly all of my scripts start with clc();clear();.

found a function in a file and after select it to do operation in my program matlab

I created this function to browse all the functions stored in a file and now I want to select my function in my main program how can I select it
This is my function:
function testMode(i)
a=dir('H_*.m');
if exist('i','var')
if isempty(i)
z={a.name}';
[selection,ok]=listdlg('ListString',z,'SelectionMode','single');
if ok
i=find(selection,1,'first');
end
end
nom=a(i).name;
nom=nom(1:end-2);
disp(nom)
else
disp('fonction a un argument')
end
It looks like you will have the name of the function as a string. To call that function, you can either use feval, or you can convert the string to a function handle and call that.
result = feval(nom, argument1);
or
fcn = str2func(nom);
fcn(argument1);
The latter should be preferred, as the conversion can be done once and the function handle re-used after that, and because the calling syntax is exactly the same as calling a regular function.
For more information on function handles, see doc function_handle.
Also, for a different approach, see this document: http://www.mathworks.com/help/matlab/ref/localfunctions.html. If you can create your "menu" of functions as local functions in a single file, this approach could be very clean and simple, as it will directly give you a cell array of function handles of all of those subfunctions. From there, you can get easily get the function name for display using functions command.

MATLAB: Script with all my functions

Maybe it's a basic question but here I go. I would like to have a .m with all the functions that will be accessed by other scripts and functions.
I tried just doing a script with all the functions and call it in other functions code.
And I got and error. Could you please explain me how can I solve this?
I'm trying this, which gives me no error, and does what I want it to do, still, is it a good way to do it? Any suggestions?
function PruebasLlamaFuncion
funcionFEM=#PruebasTodasFunciones;
a=funcionFEM('OVERPOWER',1,5)
b=funcionFEM('POWEROVERWELMING',2)
end
...
function a=f(nombre,varargin)
f=str2func(nombre)
a=f(varargin{1:end});
end
function d=OVERPOWER(J,c)
d=J*c;
end
function e=POWEROVERWELMING(J)
e=J;
end
Function placement
Matlab, unlike a number of other languages, permits a single file to contain only one main function that is visible to the rest of the system. The main function is the first function. (Documentation)
Any functions that are defined after the main function body are called local functions. These functions each create their own separate workspace (scope) and can be called by one another and, of course, by the main function.
Any functions that are defined within the main function body are called nested functions. These functions have their own workspace but are also able to access and change the variables of their parent function under certain conditions. Nested functions at the same nesting level can call each other and local functions, but local functions cannot call nested functions since they are out of scope.
Workarounds
There are several options available to you depending on how you would like to proceed.
At the risk of giving too many options but desiring to be exhaustive, I'll put the list from what I would do first to what I would do last.
For most things, I would recommend 1 or 2.
The other options are more for creating libraries/APIs, but I included them to show what can be done.
Define Function1 and Function2 in separate m-files on the Matlab path or in the present working directory to call them normally.
Wrap the main body of your work (the one calling the functions) in a function itself and define the other functions as local functions or nested functions. Example:
function output = main(a,b,c)
Result=Function1(a,b,c);
Result2=Function2(b,d);
...
% You can define Function1 and Function2 here for nested functions
end
% Or you can define Function1 and Function2 here for local functions
You can get a bit more fancy and have a function that returns function handles (pointers) to the local or nested functions and then use the (in the example) struct to call the functions in another script:
function Functions = GetFunctions()
Functions.F1 = #(a,b,c) Function1(a,b,c);
Functions.F2 = #(a,b) Function2(a,b);
% You can define Function1 and Function2 here for nested functions
end
% Or you can define Function1 and Function2 here for local functions
If you have R2013b or above, you can do the same thing as above using the localfunctions function to create a cell array of handles to all local functions (in this case, the definitions are required to be outside the main function body).
function Functions = GetFunctions()
Functions = localfunctions(); % R2013b+ only
end
% Define Function1 and Function2 here for local functions
You can also create a class with static functions:
classdef Functions
methods(Static)
% Define Function1 and Function2 here and call them just like the struct above.
end
end
I hope that makes sense and hopefully helps.
I think you're misunderstanding something. A script is for calling a series of functions/other scripts in sequence. If you just want your functions to be accessible in other code, you only need to make sure they're on the path. You would never need a "script containing all the functions". You may be thinking of local functions, but these are the exact opposite of what you want (they can't be called from outside the function where they're defined or other local functions in the same file).
e.g. if Function1 and Function2 are on your path, you could write a script like this, perhaps as a demo for how to use those two functions:
a = 0;
b = 1;
c = 2;
d = 'Unicorns';
Result=Function1(a,b,c);
Result2=Function2(b,d);
It does not and should not have any function definitions in it. If your script can't find the functions, use addpath (see docs), to put the folder where these function files reside into your path. The m files should be given the same name, e.g. the following needs to go in a file called myfunc.m
function result = myfunc(a,b,c)
Functions in your working directory can also be called even if that directory isn't on your path.

New MATLAB version overrides my function with class method. Can I still call my function?

I had a function in a file harmonic.m in my matlab path with prototype:
function D = harmonic(A,B,C)
where, importantly, A is expected to be a matrix of type double.
In version r2014a, apparently MATLAB has created a new builtin class method double.harmonic. Thus when I call my function I get an error inside the wrong harmonic. It doesn't help that my harmonic is closer in the path list (which harmonic reveals my path) because my first input is A and harmonic(A,B,C) seems to be equivalent to A.harmonic(B,C).
Is there any way to call my function directly? To ignore this double.harmonic function? I know I can create a function handle from the path, but that's nasty. I'm looking for a more elegant solution or workaround. The most obvious being change my function's name, but then I'll feel bullied : - (.
Put your version of harmonic into a folder #double, and make sure that your folder #double is above \toolbox\symbolic\symbolic\#double on the path (this new double.harmonic is from Symbolic Toolbox).
That will force your function to become a method of double i.e. it will be double.harmonic, rather than a generic function harmonic. When deciding which thing to dispatch to, MATLAB will consider methods first, then generic functions later. Since your double.harmonic and the other one are both methods, and yours is ahead on the path, yours will win. BAM - eat that, MATLAB!