在我寻求获得Haskell的进一步经验的过程中,我开始使用print和printf.
我想尝试打印一个数组(好吧,几个,但它只是一个开始),我想使用这种格式"%+.4f"
,这意味着我会得到:
+2.1234 or -1.2345
Run Code Online (Sandbox Code Playgroud)
但是我注意到使用printf打印数组非常困难,所以我尝试切换到打印.以这种方式打印列表似乎更容易,但我不确定如何使用与printf相同的格式打印列表元素.
我的列表看起来像这样:
[-1.2, 2.3, 4.7, -6.850399]
Run Code Online (Sandbox Code Playgroud) 我有以下课程,我想尝试使用 querydsl 并进行一些基本查询。使用intelliJ 2017.3,没有生成类QUser。我试过在谷歌上搜索我的问题,每个 SO 答案似乎都提供了不同的解决方案(有些没有用,有些我不明白,因为我以前从未使用过这些东西),而且大多数教程似乎都在做完全不同的事情。
我曾尝试使用 Spring Boot 似乎内置的任何内容进行查询(不知道,似乎只是可行,但从它的外观来看它太基本了)并且查询工作得很好,所以我猜这是一些配置问题(我是一个行家和春天的菜鸟)。
// User.java
@Entity
@Table(name = "symptoms")
public class Symptom
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
private String name;
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
我已将这些内容添加到 pom.xml 中:
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>4.1.4</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>4.1.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId> …
Run Code Online (Sandbox Code Playgroud) 我正在运行 .NET Core 2.2 应用程序,我有一些代码想在 Startup.cs 中的初始设置后立即运行。该类依赖于注册类型,我真的不明白我应该如何创建一个已经注入依赖项的实例。假设我有以下类,我想在设置完成后立即运行。
public class RunAfterStartup
{
private readonly IInjectedService _injectedService;
public RunAfterStartup(IInjectedService injectedService)
{
_injectedService = injectedService;
}
public void Foo()
{
_injectedService.Bar();
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以RunAfterStartup().Foo()
在 Startup 中运行?
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(typeof(IInjectedService), typeof(InjectedService));
...
}
public void Configure(IApplicationBuilder app)
{
app.UseMvc();
// I assume this has to go here, but could be anywhere
var runAfterStartup = MagicallyGetInstance();
runAfterStartup.Foo();
}
Run Code Online (Sandbox Code Playgroud)
我知道在 .NET Framework(不确定 Core)中,您可以使用 SimpleInjector 通过执行类似的操作来完成此操作container.GetInstance<RunAfterStartup>().Foo()
,但我不确定这在 .NET Core 中是如何工作的,我只想使用内置 DI。
我有以下设置:
容器中的一个简单 Flask 应用程序
Postgres 容器
使用以下 Dockerfile:
FROM python:alpine3.7
COPY . /app
WORKDIR /app
RUN apk update
RUN apk add --virtual build-deps gcc python3-dev musl-dev
RUN apk add postgresql-dev
RUN pip install -r requirements.txt
RUN apk del build-deps
EXPOSE 5006
CMD ["python", "./app.py"]
Run Code Online (Sandbox Code Playgroud)
对于数据库:
FROM postgres:11.1-alpine
COPY create_db.sql /docker-entrypoint-initdb.d/
EXPOSE 5432
Run Code Online (Sandbox Code Playgroud)
这是我的 docker-compose yaml(映射到主机的端口以检查容器的工作):
version: '3'
services:
postgres:
image: ccdb
build: ccDb
restart: always
environment:
POSTGRES_PASSWORD: password
ports:
- "5432:5432"
top:
image: ccpytop
build: ccPyTop
ports:
- "5006:5006"
Run Code Online (Sandbox Code Playgroud)
我可以从主机成功连接到数据库,并且可以导航到 …
我有一个有HashSet<int>
会员的模特。由于我无法让 Dapper 自动从数组创建集合,所以我想我只需使用多重映射查询并将数组作为单独的列,对其进行拆分并在 lambda 内创建集合。需要明确的是,我想反序列化整个模型,而不仅仅是集合的部分。
我的第一次多重映射尝试也失败了,所以我决定尝试一些更简单的方法,获取一个数组并将其反序列化为List<int>
or int[]
。这就是我被困住的地方。
我正在使用 PostgreSQL 12 数据库。
代码片段如下
var query = "SELECT ARRAY[1, 2, 3];";
var ints1 = await _conn.QueryAsync<List<int>>(query); // This returns an empty array
var ints2 = await _conn.QueryAsync<int[]>(query); // System.ArgumentException: Invalid type owner for DynamicMethod.
Run Code Online (Sandbox Code Playgroud)
我不太确定我做错了什么,谷歌似乎并没有真正提供帮助,我只找到有关在查询中使用列表的问题。
编辑:好的,我通过将数组转换为 JSON 来使其工作,但我仍然发现这个解决方案很丑陋。有没有办法避免创建 JSON?
我正准备进行测试,我注意到了这个问题:
Define an object x so that (eq? (car x) (cdr x)) returns #t
Run Code Online (Sandbox Code Playgroud)
我最初认为这很简单.cdr x
是一个列表,car x
是一个单独的元素,所以我的猜测是将x中的第一个元素作为一个等于x的尾部的列表.所以我想出来了
(define x (list (list 1) 1))
Run Code Online (Sandbox Code Playgroud)
调用car x
在DrRacket结果(list 1)
也是如此cdr x
,但是当我尝试调用(eq? (car x) (cdr x))
的结果是#F.
我到底错过了什么?那么正确的答案是什么?
我正在尝试使用一个非常基本的(到目前为止)Spring Boot 应用程序在我的 MySQL 数据库中添加一个条目。我使用了在网上找到的一些零碎内容,这是我试图遵循的代码:
netgloo/spring-boot-samples/spring-boot-mysql-springdatajpa-hibernate
目前,我在运行应用程序时遇到以下问题。
当我第一次运行该应用程序时,会引发以下异常:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
Run Code Online (Sandbox Code Playgroud)
当我向控制器发出 POST 请求时(该请求应该在数据库中创建一个新条目),我收到以下异常:
org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement
Run Code Online (Sandbox Code Playgroud)
这又是由以下原因引起的:
Caused by: java.sql.SQLException: No database selected
Run Code Online (Sandbox Code Playgroud)
应用程序属性:
# Web pages extension
spring.mvc.view.suffix =.html
# ==============================
# --- DATA SOURCE
# ==============================
# Database url
spring.datasource.url = jdbc:mysql://localhost:3306?useSSL=false
# Database credentials
spring.datasource.username = root
spring.datasource.password = root
# Keep database connection alive
spring.datasource.tomcat.test-while-idle = true
spring.datasource.tomcat.validation-query = SELECT 1 …
Run Code Online (Sandbox Code Playgroud) 我试图生成一些随机数和新的随机数生成器.我没有太远,但我遇到了这个错误,我不明白如何解决它.
我的代码是:
getGenerator :: RandomGen g => g
getGenerator = snd (next (mkStdGen 42))
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
Couldn't match expected type ‘g’ with actual type ‘StdGen’
‘g’ is a rigid type variable bound by
the type signature
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下我做错了什么吗?我也尝试将RandomGen切换到StdGen但得到错误:
‘StdGen’ is applied to too many type arguments
Run Code Online (Sandbox Code Playgroud)
我看到mkStdGen创建了一个StdGen,但根据维基页面,StdGen是RandomGen的一个实例.有没有办法以某种方式将StdGen转换为RandomGen?我现在真的很困惑.
我注意到我写的一个函数现在无法工作,尽管在其他场合成功使用它.
我的测试文件(仅用于测试此问题)看起来像这样:
import System.Random
generator = next . snd
Run Code Online (Sandbox Code Playgroud)
这导致错误
No instance for (RandomGen g0) arising from a use of ‘next’
The type variable ‘g0’ is ambiguous
Relevant bindings include
generator :: (a, g0) -> (Int, g0) (bound at Test.hs:2:1)
Note: there is a potential instance available:
instance RandomGen StdGen -- Defined in ‘System.Random’
In the first argument of ‘(.)’, namely ‘next’
In the expression: next . snd
In an equation for ‘generator’: generator = next . snd
Run Code Online (Sandbox Code Playgroud)
奇怪的是,如果我打开ghci并输入:
import …
Run Code Online (Sandbox Code Playgroud) 我目前有这段代码:
function string keys = map (xor 1) (map ord string)
Run Code Online (Sandbox Code Playgroud)
它从字符串中获取每个元素,并将xor与1一起使用.我想通过用键中的任何元素替换1来使地图函数更高级.
因此,举例来说,如果string == "Test"
和keys = [1,3,6,9]
我会得到:
'T' xor 1
'e' xor 3
's' xor 6
't' xor 9
Run Code Online (Sandbox Code Playgroud)
有没有办法迭代所有键的元素,以便我可以实现这一点?我对Haskell很新,我对它的概念没有很好的把握.
我解决这个问题的尝试是:
function string keys = map (iterate xor keys) (map ord string)
Run Code Online (Sandbox Code Playgroud)
但我得到了一些错误,我猜这是因为迭代功能.
任何帮助将不胜感激!
正如我发布的那样,我注意到iterate做了一个完全不同的事情,所以在这一点上我知道为什么它不起作用,但我不知道如何替换它.