考虑在无形状回购中找到的单例函数的定义:
/** Polymorphic function selecting an arbitrary element from a non-empty `Set`. */
object choose extends (Set ~> Option) {
def apply[T](s : Set[T]) = s.headOption
}
Run Code Online (Sandbox Code Playgroud)
def在以下示例中,将其与传统语法进行对比:
package utils
object UtilWithASingleMethod {
def isSatisfyingSomePredicate(foo: Foo): Boolean = ???
}
Run Code Online (Sandbox Code Playgroud)
与
package utils
object isSatisfyingSomePredicate extends (Foo => Boolean) {
def apply(foo: Foo): Boolean = ???
}
Run Code Online (Sandbox Code Playgroud)
注意呼叫站点现在如何变成
isSatisfyingSomePredicate(foo)
Run Code Online (Sandbox Code Playgroud)
代替
UtilWithASingleMethod.isSatisfyingSomePredicate(foo)
Run Code Online (Sandbox Code Playgroud)
要么
import UtilWithASingleMethod._
isSatisfyingSomePredicate(foo)
Run Code Online (Sandbox Code Playgroud)
就个人而言,UtilWithASingleMethod软件包似乎只是为了能够使用熟悉的def语法而没有添加任何有用的信息。
除了主观的缺点(例如不熟悉或与工厂模式中使用的对象+应用样式混淆)之外,单例函数定义还有其他技术缺点吗?
我是Scala语言的新手,我试图定义一个可以翻转其参数的基本函数,我这样定义它:
var flipArguments = ((a: Any, b: Any) => Any ) => ((b: Any, a: Any) => Any)
但是我遇到了一个编译错误,该错误突出显示了带有消息的第二个箭头
';' or newline expected
Run Code Online (Sandbox Code Playgroud)
而且我不明白我在哪里犯语法错误。
void remove_element(struct Node *list)
{
struct Node *temp = list;
printf("Enter the element value you want to remove");
int value;
scanf("%d",&value);
if(temp->data == value){ //first node is to be deleted
*list = temp->next; // error here
free(temp);
}
}
Run Code Online (Sandbox Code Playgroud)
错误:从类型“struct Node *”分配给类型“struct Node”时的类型不兼容| 虽然这已经成功编译
struct Node *temp = list;
Run Code Online (Sandbox Code Playgroud)
这一行很相似,但没有显示错误。
我正在编写一个 C 程序,该程序将连接所有行(\n包括“ ”),同时将指针保存到最终字符串的最后一个字符。但是,我没有得到我预期的结果。可能是什么问题呢?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
struct Node *next;
struct Node *prev;
};
struct Node *CreateNewNode() {
struct Node *newNode = malloc(sizeof(struct Node));
return newNode;
}
struct PieceTable {
char *buffer;
char *ptr_to_last_character;
} PT;
void strconcatenate(char *source) {
size_t source_len = strlen(source);
size_t buffer_len = strlen(PT.buffer);
PT.buffer = realloc(PT.buffer, buffer_len + source_len + 1);
while (*source)
*PT.ptr_to_last_character++ = *source++;
*PT.ptr_to_last_character = '\0';
}
int main(int argc, char *argv[]) {
char …Run Code Online (Sandbox Code Playgroud) c pointers c-strings string-concatenation function-definition
我有以下功能。它适用于short:
void foo (short n, short * res)
{
*res = n*2;
}
Run Code Online (Sandbox Code Playgroud)
我想将结果存储在double变量中。例如:
short in = 5;
double out = 0;
foo(in,&out);
Run Code Online (Sandbox Code Playgroud)
但这样的结果是一些垃圾。有没有办法在这种情况下强制转换这些类型,或者唯一的解决方案是使用一些类型的临时变量short?
c floating-point integer type-conversion function-definition
这是代码:
#include<stdio.h>
int main(){
// prints I stars
void printIStars(int i) {
// Count (call it j) from 1 to i (inclusive)
for (int j = 1; j <= i; j++) {
// Print a star
printf("*");
}
}
// prints a triangle of n stars
void printStarTriangle(int n) {
// Count (call it i) from 1 to n (inclusive)
for (int i = 1; i <= n; i++) {
// Print I stars
printIStars (i);
// Print a newline …Run Code Online (Sandbox Code Playgroud) 我想写一个函数 calc(array, n1 , n2)
array是一个整数向量。的n1和n2参数是由以下关系定义的整数0<= n1<= n2<array.size()。
该calc方法应返回其索引属于该[n1; n2]区间的数组的整数之和。
我试试这个代码,但它不对
class Answer {
public:
static int cal(const vector<int>& array, int n1, int n2) {
int sum = 0;
for (vector<int>::iterator it = array[0]+n1; it != array[0]+n2; ++it)
{
sum + = *it;
}
return sum;
}
};
Run Code Online (Sandbox Code Playgroud) 我有这个代码,它在将一个数字提高到第 n 个数字后返回答案。
int getPower(int base, int x){
int result=1;
while (x != 0) {
result *= base;
--x;
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
我尝试测试 base 为 97 且 x 为 5 的位置。我得到了-2594335. 我尝试将结果的数据类型更改为 long,但仍然得到相同的负值。
我可以在声明数组的函数中使用指针概念访问数组。但是,我无法从另一个抛出错误的函数访问:indirection requires pointer operation ('int' invalid)。代码是:
#include <stdio.h>
const int ROW = 3;
const int COL = 3;
void printArray(int *ptr);
int main(void)
{
int arr[ROW][COL];
int count = 2;
// initialize 2D array
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
arr[i][j] = count++;
}
}
// print 2D array
for (int i = 0; i < ROW; i++)
{
for (int j = 0; …Run Code Online (Sandbox Code Playgroud) c pointers function multidimensional-array function-definition
foo使用函数参数作为循环变量
void foo(int i, int j) {
for (; i < 5; ++i)
for (; j < 5; ++j)
std::cout << i << " " << j << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
和foo(0, 0)打印
0 0
0 1
0 2
0 3
0 4
Run Code Online (Sandbox Code Playgroud)
我想知道为什么i总是0。