variadic-functions

How to write a variadic function in F# emulating a similar Haskell solution?

ぐ巨炮叔叔 提交于 2019-11-29 04:22:17
How can I (if at all) emulate variadic functions (not methods) so that I could write sum 1 2 3 sum 1 2 3 4 5 sum 1 2 3 4 5 6 7 // etc. The code above is just meant as an example - obviously if I would have to sum up a list then [ 1; 2 ; 3] |> List.sum is a much better way. However I am looking for a structurally similar solution like this Haskell solution What is also important is that the normal syntax for function calls and parameter values remains the same. So sum 1 2 3 vs sum(1, 2, 3) which effectively means that let sum ([<ParamArray>] arr) = ... is not wanted in this specific case. The

Delphi “array of const” to “varargs”

懵懂的女人 提交于 2019-11-29 04:16:51
Please help! I need this conversion to write wrapper for some C headers for Delphi. As an example: function pushfstring(fmt: PAnsiChar): PAnsiChar; cdecl; varargs; external; ... function PushString(fmt: AnsiString; const args: array of const): AnsiString; begin Result := AnsiString(pushfstring(PAnsiString(fmt), args)); // it's incorrect :/ end; How can I convert "array of const" to "varargs"? edit : function PushString is actually inside the record (I gave a simplified example), and I do not have direct access to pushfstring. Direct call is excluded. edit 2 :I write the units for LUA library

Repeated use of a variadic function argument doesn't work

风流意气都作罢 提交于 2019-11-29 03:43:33
I have a function that tries to log stuff to the console and also to a log file, but it doesn't work. The second use of the variable length argument gives garbage written to the console. Any ideas? void logPrintf(const char *fmt, ...) { va_list ap; // log to logfile va_start(ap, fmt); logOpen; vfprintf(flog, fmt, ap); logClose; va_end(ap); va_list ap2; // log to console va_start(ap2, fmt); printf(fmt, ap2); va_end(ap2); } The original code fails because it tries to use printf() where it needs to use vprintf() . Taking dubious points like the logOpen and logClose statements at face value (given

PHP Spread Syntax in Array Declaration

大城市里の小女人 提交于 2019-11-29 03:04:19
PHP supports the spread syntax for variadic functions . In JavaScript, you can use the spread syntax to do this : var a = [1, 2]; var b = [...a, 3, 4]; console.log(b); // [1, 2, 3, 4] However, trying to do this in PHP: $a = [1, 2]; $b = [...$a, 3, 4]; var_dump($b);die; Results in this error: Parse error: syntax error, unexpected '...' (T_ELLIPSIS), expecting ']' Is using the spread syntax this way not allowed in PHP? If so, is there an equally-as-elegant way to achieve the same effect? Erald Karakashi The spread operator in the arrays RFC has been implemented in PHP 7.4: $ary = [3, 4, 5];

Explain Keyword-Only Arguments (VarArgs) in Python

流过昼夜 提交于 2019-11-29 02:19:48
问题 Please see the code below:- #!/usr/bin/python # Filename: total.py def total(initial=5, *numbers, **keywords): count = initial for number in numbers: count += number for key in keywords: count += keywords[key] return count print(total(10, 1, 2, 3, vegetables=50, fruits=100)) Can someone please explain how is *numbers and **keywords picking up the arguments? A simple explaination is very much appreciayed Thanks in advance 回答1: In your code numbers is assigned the (1,2,3) tuple. keywords is

pass variable number of arguments in scala (2.8) case class to parent constructor

血红的双手。 提交于 2019-11-29 02:07:21
I was experimenting with variable constructor arguments for case classes in Scala, but am unable to pass them to the constructor of a case classes' parent: abstract case class Node(val blocks: (Node => Option[Node])*) case class Root(val elementBlocks: (Node => Option[Node])*) extends Node(elementBlocks) the above doesn't compile... is it actually possible to do this? This works with 2.7: abstract case class A(val a: String*) case class B(val b: String*) extends A(b:_*) Should work with 2.8. You need to use the :_* syntax which means "treat this sequence as a sequence" ! Otherwise, your

(Im)perfect forwarding with variadic templates

孤者浪人 提交于 2019-11-29 00:41:25
问题 Synopsis Given a type with a variadic template constructor that forwards the arguments to an implementation class, is it possible to restrict the types being forwarded with SFINAE? Details First, consider the non-variadic case with a constructor taking a universal reference. Here one can disable forwarding of a non-const lvalue reference via SFINAE to use the copy constructor instead. struct foo { foo() = default; foo(foo const&) { std::cout << "copy" << std::endl; } template < typename T,

How do I handle an unspecified number of parameters in Scheme?

六眼飞鱼酱① 提交于 2019-11-28 20:27:31
For example ((fn-stringappend string-append) "a" "b" "c") I know how to handle this (f x y z) . But what if there's an unknown number of parameters? Is there any way to handle this kind of problem? In Scheme you can use the dot notation for declaring a procedure that receives a variable number of arguments (also known as varargs or variadic function ): (define (procedure . args) ...) Inside procedure , args will be a list with the zero or more arguments passed; call it like this: (procedure "a" "b" "c") As pointed out by @Arafinwe, here's the equivalent notation for an anonymous procedure:

Create a Map in Golang from database Rows

跟風遠走 提交于 2019-11-28 20:24:15
Basically after doing a query I'd like to take the resulting rows and produce a []map[string]interface{} , but I do not see how to do this with the API since the Rows.Scan() function needs a specific number of parameters matching the requested number of columns (and possibly the types as well) to correctly obtain the data. Again, I'd like to generalize this call and take any query and turn it into a []map[string]interface{} , where the map contains column names mapped to the values for that row. This is likely very inefficient, and I plan on changing the structure later so that interface{} is

Ambiguous Reference to overloaded definition - One vs Two Parameters

妖精的绣舞 提交于 2019-11-28 19:35:17
Given the following companion object with overloaded versions of apply : object List { def apply[T](): List[T] = new Nil def apply[T](x1: T): List[T] = new Cons(x1, new Nil) def apply[T](x1: T, x2: T): List[T] = new Cons(x1, new Cons(x2, new Nil)) def apply[T](elems: T*): List[T] = elems.foldRight(List[T])((elem, l) => new Cons(elem, l)) } And the two instantiations List(1) // Error - Ambiguity List('a', 'b') // Works fine scalac complains about the first instantiation ( ambiguous reference to overloaded definition ) because both the single argument and the varargs method are equally specific