小编Ale*_* jg的帖子

Haskell:抽象遗传算法

我是Haskell编程世界的新手,我正在研究一种简单的遗传算法,以找到旅行商问题的良好解决方案.我将解决方案表示为Integers上的排列,因此我有这种类型的同义词

type Genome = [Int]
Run Code Online (Sandbox Code Playgroud)

算法本身是一组对解决方案进行操作的函数:

mutation :: Genome -> Genome
selectParents :: [Genome] -> [Genome] -> [Genome]
crossover :: Genome -> Genome -> (Genome, Genome)
selectSurvivors :: [Genome] -> [Genome] -> [Genome]
Run Code Online (Sandbox Code Playgroud)

我不确定我的代码中有多少与我的问题相关,所以请询问是否需要更多详细信息.值得一提的一件事是上面的类型签名实际上是简化的,我实际上是使用State monad来携带一个StdGen所以这些函数实际上都返回有状态计算.

有几件事我想对此做些什么,但不能完全理解.我希望能够为解决方案选择不同的表示形式,在我看来,这将是一个使用类型类的自然场所,因此这Genome将是类型类和它[Int]的特定实例Genome.

现在,我希望能够尝试实现,并希望能够在其他项目中使用该代码.使用这样的类型类需要我创建的每个新算法都要求我创建另一个实例Genome,这是创建库的好方法吗?

一个额外的问题,只是困扰我的事情,是否有任何方法可以创建类似函数的类型同义词,这样如果我正在编写一个函数,它将函数作为参数,我可以编写同义词而不是整个类型函数的签名,即以下类似的东西可以工作.

type someFunc = [Int] -> [Int] -> Int
someOtherFunc :: someFunc -> [Int] -> Int
Run Code Online (Sandbox Code Playgroud)

是的,希望这是对问题的一个清晰的解释,感觉我错过了真正明显的答案,但它没有跳出来对我.干杯

haskell evolutionary-algorithm

18
推荐指数
1
解决办法
2554
查看次数

我应该在clojure中实现Seq接口

我在Clojure中有一个数据结构,它代表了一组实验结果:

(defprotocol ResultSet
  (columns [rs] "return a collection of the columns in the resultset, represented by keywords")
  (rows [rs] [rs column-keys] "returns a seq of the rows in the resultset, order and column specified as keywords by column-keys. with a single argument returns rows with all columns present"))
Run Code Online (Sandbox Code Playgroud)

我有一个deftype实现这个协议.我感兴趣的是编写函数,这些函数可以在函数结果集中的所有结果上映射函数,或者在结果集上进行折叠,基本上与内置的seq函数执行相同的操作.

在Haskell中,我会通过实现相关的类型类(例如Functor)然后使用fmap或mfilter等函数来实现.所以我在Clojure中做了这个,并结束了有关实现ISeq接口的一些想法.

那么,这是个好主意吗?我找不到很多关于实现ISeq的资源,我想知道对此的惯用方法是什么.

functional-programming clojure

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

将PIL图像转换为Cairo ImageSurface

我正在尝试从PIL图像创建一个cairo ImageSurface,到目前为止我的代码是:

im = Image.open(filename)
imstr = im.tostring()
a = array.array('B', imstr)
height, width = im.size
stride = cairo.ImageSurface.format_stride_for_width(cairo.FORMAT_RGB24, width)
return cairo.ImageSurface.create_for_data(a, cairo.FORMAT_ARGB24, width, height, stride)
Run Code Online (Sandbox Code Playgroud)

但是这给了我

TypeError: buffer is not long enough.
Run Code Online (Sandbox Code Playgroud)

我真的不明白为什么会这样,也许我对图像格式不太了解.

我正在使用cairo 1.10.

python cairo python-imaging-library

5
推荐指数
2
解决办法
3063
查看次数

使用PostgreSQL聚合ORDER BY和sqlalchemy

我有一个查询,它使用PostgreSQL的语法在聚合函数中使用ORDER BY,如下所示:

SELECT some_agg_func(a ORDER BY b DESC) FROM table;
Run Code Online (Sandbox Code Playgroud)

有没有人知道用sqlalchemy表达式语言来做这件事?

python postgresql sqlalchemy

4
推荐指数
2
解决办法
2575
查看次数

带有声明文件的打字稿“找不到模块”

我正在使用带有 react 的打字稿,并且需要为名为react-sticky. 我不想发布类型模块,我只想编写类型并将它们包含在我的代码库中,但我遇到了麻烦。这是设置

App.tsx

/// <reference path="./typings/index.d.ts" />
/// <reference path="./typings/react-sticky.d.ts" />
import * as React from "react"
import { Sticky, StickyContainer } from "react-sticky"

const App: React.StatelessComponent<{}> = () => {
    return <StickyContainer>
        <Sticky>
        <h1>Hello World</h1>
        </Sticky>
    </StickyContainer>
}
Run Code Online (Sandbox Code Playgroud)

/typings/react-sticky.d.ts

/// <reference path="./modules/react/index.d.ts" />
import * as React from "react"

declare module "react-sticky" {

    export var StickyContainer: React.Component<{}, {}>
    export interface StickyProps {
        stickyStyle?: any
        stickyClassName?: string
        topOffset?: number
        bottomOffset?: number
        className?: string
        style?: {}
        onStickyStateChange?: …
Run Code Online (Sandbox Code Playgroud)

typescript

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

如何使用 rust 中的宏生成复杂的枚举变体

我正在编写一个用于解析 OVPN 配置文件的小库。OVPN 配置文件具有这种格式

command arg1 arg2
othercommand arg1 arg2
Run Code Online (Sandbox Code Playgroud)

有一组固定的命令,其中一些具有可选参数。我想将解析的命令表示为枚举。所以上面的内容可能最终会被这样表示:

enum ConfigDirective{
    Command{arg1: String},
    OtherCommand{arg1: String, optinal_arg1: Option<String>},
}

fn parse_line(command: String, args: Vec<String>) -> ConfigDirective {
    match command {
        "command" => ConfigDirective::Command{arg1: args[0]},
        "other_command" => ConfigDirective:OtherCommand{arg1: args[0], optional_arg1: args.get(1),
    }
}
Run Code Online (Sandbox Code Playgroud)

我喜欢这种结构,但有很多可能的命令(在 280 区域内的某处)。所以我想写一个宏来生成大部分样板文件。理想情况下,我会写如下内容:

define_config_directive!{
    {command => "command1", rust_name => CommandOne, args => [arg1], optional_args => []},    
    {command => "other_command", rust_name => OtherCommand, args => [arg1], optional_args => [optional_arg1]},
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我能够得到的最接近的是:

macro_rules! define_config_directives {
    ($({
        rust_name => $rust_name:ident, …
Run Code Online (Sandbox Code Playgroud)

macros rust

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