What does "!" before a function do in D - d

I'm trying to learn some D for a project and frequently see code such as
foreach (i; 0 .. 10)
benchmark!test(1)[0]
.to!("msecs", double)
.reverseArgs!writefln
(" took: %.2f miliseconds");
I'm uncertain on why some function calls have ! before them while others do not.

Well, ! BEFORE a function inverts the result, just like if(!a). But ! AFTER a function separates it from its compile-time (template) arguments, like to!int. The parenthesis on those arguments are optional iff it is one simple word, so like benchmark!test or to!int, but if it is more than that, it needs parens like to!("msecs", double).
In a lot of cases, a function is passed like reverseArgs!writefln which just passed the writefln function as a compile-time argument to the reverseArgs template/function.
So in general: foo!(compile, time, args)(run, time, args) where not all functions have !(compile, time args).
Does that make sense? I can try to edit if not...

Related

Functions with parametrised arity

Is it possible to write functions that returns functions whose signature depends on the arguments to the builder function?
Specifically I am refining an implementation of primitive recursion I wrote. I want to have factory like functions that for numeric parameters generate a function that works on tuples or lists of the length equal to the arguments passed for the numeric parameters. Currently I am handling cases like pr 1 2 [0,5,13], which is an invalid statement from the perspective of primitive recursion, at runtime through Either:
pr :: (Show a) => Int -> Int -> [a] -> Either String a
pr i k args
| 1 <= i && i <= k && length args == k = Right $ args!!(i-1)
| i <= 0 = Left "first argument of pr needs to be greater or equal to 1"
| k < i = Left "first argument of pr needs to be lesser or equal to the second argument"
| length args /= k = Left $ "pr expected "++(show k)++" arguments, got "++(show $ length args)++": pr "++(concat[show i, " ", show k, " ", show args])
But I would like to somehow catch that case at compile time, as from the perspective of the formal system I want to implement with this, this is a compile time error -- passing more arguments to a function than its domain specifies.
Is this somehow possible and if not what would be the correct approach to get compile time errors for what should be invalid statements.
What you want is a sized vector. It is like a list but in addition to the type of its elements, it is also parametrised by type level natural numbers.
sized-vector package on Hackage is what you need. As it happens, the function you're trying to implement is the last function in this library.
Note that every time you call last you will have to prove the compiler that its argument vector is of size at least 1. You can do this by constructing the vector in the source code (for example, the compiler will understand 1 :- 2 :- Nil is of size 2) or if the vector is obtained at runtime perhaps by conversion from a list, you'll have to write a function that either gives a run time error if it has no elements or constructs a vector of size at least one i.e. have the type level size S n for some n.
If you're not familiar with dependently typed programming (a paradigm that includes this and much much more) I suggest you look through some tutorials first. For example, this post is a good example that includes how to implement vectors from scratch and write functions for them.
A word of caution, learning and using dependently typed programming is exciting, addictive, but also time consuming. So if you want to focus on the task at hand, you might like to live with runtime checks for now.

Assumed string length input into a Fortran function

I am writing the following simple routine:
program scratch
character*4 :: word
word = 'hell'
print *, concat(word)
end program scratch
function concat(x)
character*(*) x
concat = x // 'plus stuff'
end function concat
The program should be taking the string 'hell' and concatenating to it the string 'plus stuff'. I would like the function to be able to take in any length string (I am planning to use the word 'heaven' as well) and concatenate to it the string 'plus stuff'.
Currently, when I run this on Visual Studio 2012 I get the following error:
Error 1 error #6303: The assignment operation or the binary
expression operation is invalid for the data types of the two
operands. D:\aboufira\Desktop\TEMP\Visual
Studio\test\logicalfunction\scratch.f90 9
This error is for the following line:
concat = x // 'plus stuff'
It is not apparent to me why the two operands are not compatible. I have set them both to be strings. Why will they not concatenate?
High Performance Mark's comment tells you about why the compiler complains: implicit typing.
The result of the function concat is implicitly typed because you haven't declared its type otherwise. Although x // 'plus stuff' is the correct way to concatenate character variables, you're attempting to assign that new character object to a (implictly) real function result.
Which leads to the question: "just how do I declare the function result to be a character?". Answer: much as you would any other character variable:
character(len=length) concat
[note that I use character(len=...) rather than character*.... I'll come on to exactly why later, but I'll also point out that the form character*4 is obsolete according to current Fortran, and may eventually be deleted entirely.]
The tricky part is: what is the length it should be declared as?
When declaring the length of a character function result which we don't know ahead of time there are two1 approaches:
an automatic character object;
a deferred length character object.
In the case of this function, we know that the length of the result is 10 longer than the input. We can declare
character(len=LEN(x)+10) concat
To do this we cannot use the form character*(LEN(x)+10).
In a more general case, deferred length:
character(len=:), allocatable :: concat ! Deferred length, will be defined on allocation
where later
concat = x//'plus stuff' ! Using automatic allocation on intrinsic assignment
Using these forms adds the requirement that the function concat has an explicit interface in the main program. You'll find much about that in other questions and resources. Providing an explicit interface will also remove the problem that, in the main program, concat also implicitly has a real result.
To stress:
program
implicit none
character(len=[something]) concat
print *, concat('hell')
end program
will not work for concat having result of the "length unknown at compile time" forms. Ideally the function will be an internal one, or one accessed from a module.
1 There is a third: assumed length function result. Anyone who wants to know about this could read this separate question. Everyone else should pretend this doesn't exist. Just like the writers of the Fortran standard.

c++ passing variable number of arguments to a function [duplicate]

This question already has an answer here:
Generic template for calling function with vector elements
(1 answer)
Closed 6 years ago.
I have functions that take a different number of arguments (one or more double values). Pointers to these functions are stored in a vector within a formula-interpreter and will be used to call functions at runtime when needed. The function to call and the number of arguments to pass depends on the formula itself and will be known only at runtime.
From interpreting the formula I get an iterator pointing to the function to call and a vector<double> with the arguments to pass. In the current version I solve the need to pass variable number of arguments by code duplication like :
// (the number of args in dblVec to match the number of args the function accepts is checked previously)
if (dblVec.size() == 1) result = (*funcIter)->function(dblVec.at(0));
else if (dblVec.size() == 2) result = (*funcIter)->function(dblVec.at(0), dblVec.at(1));
else if (dblVec.size() == 3) result = (*funcIter)->function(dblVec.at(0), dblVec.at(1), dblVec.at(2));
...
This is not very elegant and has a limit but it works if the number of arguments is not larger than the number of above tests. My question is:
Is there some way to have an "implicit" loop within the argument list for a function to call, like (pseudo code)
result = (*funcIter)->function(passVectorContentsAsMultipleSeparatedDoubles(dblVec));
It is not a solution to pass the whole vector because the functions will be "standard" functions (like sin(double), cos(double), atan2(double, double) etc.) as well as custom functions passed to the interpreting class by the user (programmer that uses the interpreting class).
Thanks in advance for any hints.
With C++11 this is actually possible and I had a quite similar problem. Have a look at the following:
Generic template for calling function with vector elements

Clojure creating higher order first function

; Now create a function that takes a function (which produces a sequence)
; as an argument. Your function should invoke that function and return and
; return the first element from the returned sequence.
(is (higher-order-first-function? __))
Guys it is actually a part of my homework. I have 1000 lines of codes to do. This is just a part that i could not figure out how to do it. Can anyone help me how to solve this ? I tried every possible way but. I could not pass the testing.
So the steps are pretty clear from the comments:
Write a function that expects one parameter (use defn, fn, or #())
That parameter should be a function, so check for that (fn?)
Call that function
Return the first element in the returned sequence (first)

How to pattern match execute a fuction and return a value in ocaml

I have a function, I need to pattern match on another function to get two values. One need to be used in one function the other needs to be returned as output.
let myf A=
match (Functio A) with
|(frr,adll) -> funct frr 45
I need to execute the function funct and return adll as the output of the function myf. How can I do that?
Taking what you say at face value, the following code will do what you want I think:
let myf a =
let (frr, adll) = functio a in
funct frr 45;
adll
You may still have typing problems depending on what funct returns.
Note that you can replace a match that has only one alternative with just a let.
Also note that names beginning with an upper case letter are reserved for certain specific uses (such as value constructors). So you can't have a function named Functio or a parameter named A.