小编Joe*_*mel的帖子

在Clojure中应用-recur宏

我对Clojure/Lisp宏不是很熟悉.我想写apply-recur一个与其含义相同的宏(apply recur ...)

我想这个宏并不是真的需要,但我认为这是一个很好的练习.所以我要求你的解决方案.

lisp macros clojure

14
推荐指数
2
解决办法
1450
查看次数

在Clojure中实现A*搜索的性能

我已经实现了A*搜索算法,用于查找两个状态之间的最短路径.算法使用哈希映射来存储访问状态的最佳已知距离.并且一个哈希映射用于存储重建最短路径所需的子父关系.

是代码.该算法的实现是通用的(状态只需要"可以"和"可比较"),但在这种特殊情况下,状态是整数的对(向量),[x y]它们代表给定高度图中的一个单元(跳转到相邻单元的成本取决于在高度上的差异).

问题是,是否可以提高性能以及如何提高性能?也许通过使用1.2或未来版本中的一些功能,通过改变算法实现的逻辑(例如,使用不同的方式来存储路径)或在这种特定情况下改变状态表示?

Java实现此映射中立即运行,Clojure实现大约需要40秒.当然,有一些自然而明显的原因:动态类型,持久数据结构,原始类型的不必要(un)装箱......

使用瞬变没有太大的区别.

performance artificial-intelligence a-star clojure

6
推荐指数
1
解决办法
1424
查看次数

如何使用Itertools创建一系列Chrono日期的所有组合?

我正在尝试使用Rust itertools生成日期范围的所有组合,但它表示不满足特征界限.

extern crate chrono;
extern crate itertools;

use itertools::Itertools;
use chrono::prelude::*;

fn main() {
    let min = NaiveDate::from_ymd(2018, 10, 1);
    let max = NaiveDate::from_ymd(2018, 10, 14);
    let combinations = (min..=max).combinations(5);
}
Run Code Online (Sandbox Code Playgroud)

错误消息:

error[E0599]: no method named `combinations` found for type `std::ops::RangeInclusive<chrono::NaiveDate>` in the current scope
  --> src/main.rs:46:36
   |
46 |     let combinations = (min..=max).combinations(5);
   |                                    ^^^^^^^^^^^^
   |
   = note: the method `combinations` exists but the following trait bounds were not satisfied:
           `std::ops::RangeInclusive<chrono::NaiveDate> : itertools::Itertools`
           `&std::ops::RangeInclusive<chrono::NaiveDate> : itertools::Itertools` …
Run Code Online (Sandbox Code Playgroud)

rust

6
推荐指数
1
解决办法
398
查看次数

如何在 actix-web 提取器中使用异步代码?

我正在使用 sqlx 访问数据库的 actix-web 2.0.0 中实现身份验证提取器。我有这个代码:

use actix_web::{dev, web, Error, HttpRequest, FromRequest};
use actix_web::error::ErrorUnauthorized;
use futures::future::{ok, err, Ready};
use sqlx::PgPool;
use serde_derive::Deserialize;

use crate::model::User;

#[derive(Debug, Deserialize)]
pub struct Auth {
    user_id: u32,
}

impl FromRequest for Auth {
    type Error = Error;
    type Future = Ready<Result<Self, Self::Error>>;
    type Config = ();

    fn from_request(req: &HttpRequest, _: &mut dev::Payload) -> Self::Future {
        use actix_web::HttpMessage;

        let db_pool = req.app_data::<web::Data<PgPool>>().unwrap();
        let error = ErrorUnauthorized("{\"details\": \"Please log in\"}");

        if let Some(session_id) = req.cookie("sessionid") {
            log::info!("Session …
Run Code Online (Sandbox Code Playgroud)

rust async-await actix-web

2
推荐指数
1
解决办法
797
查看次数