小编gai*_*epr的帖子

如何创建一个在各种整数类型上通用的is_prime函数?

我刚刚深入研究了Rust,并希望制作一些通用的基本数学函数.我有以下is_prime功能:

fn is_prime(n: i64) -> bool {
    if n == 2 || n == 3 {
        return true;
    } else if n % 2 == 0 || n % 3 == 0 {
        return false;
    }

    let mut i = 5i64;
    let mut w = 2i64;
    while i*i <= n {
        if n % i == 0 {
            return false;
        }
        i += w;
        w = 6 - w;
    }
    true
}
Run Code Online (Sandbox Code Playgroud)

那么将会对我来说,能够通过isize,i64,usize …

traits rust

29
推荐指数
2
解决办法
5681
查看次数

golang在指定的路由返回静态html文件

我正在开发一个简单的待办事项应用程序.

我已经确定除了用户的待办事项列表之外的所有页面都可以安全地成为静态html页面.*登录表格*新帐户表格*索引页面,谈论todo应用程序

我认为这些目前没有理由去模板.

我的问题是如何(在内部,不使用像nginx这样的东西)我可以有一个静态的html设置来最有效地返回特定路线吗?

例如,在"/"处返回index.html

我知道我可以这样做:

func GetNewAccount(res http.ResponseWriter, req *http.Request) {
        body, _ := ioutil.ReadFile("templates/register.html")
        fmt.Fprint(res, string(body))
}
Run Code Online (Sandbox Code Playgroud)

要么

var register, _ = string(ioutil.ReadFile("templates/register.html"))
func GetNewAccount(res http.ResponseWriter, req *http.Request) {
        fmt.Fprint(res, register)
}
Run Code Online (Sandbox Code Playgroud)

对我来说,这些似乎更迂回的方式来做一些看似简单的事情.

go gorilla

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

如何在常见的lisp中描述,实现和接口

我正在实现一个图形数据结构,将任意对象存储为顶点.我想定义一个接口,比如获取一个对象的键,以便所有顶点都有一个键.在我看来,这听起来像我可能使用像java这样的语言的接口.

interface Vertex {
  String key (Vertex v);
  // etc...
}
Run Code Online (Sandbox Code Playgroud)

如何在常见的lisp中模拟接口的行为?

lisp common-lisp clos

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

Go程序没有死锁但也永远不会返回

附加的要点是一个使用生产者/多消费者模型中的渠道的简单程序.由于某些原因,

go run channels.go 打印所有结果但不返回(并且没有死锁或至少不会让我发生死锁的恐慌.)

type walkietalkie struct {
    in chan int
    out chan int
    quit chan bool
}

var items []int = []int{
    0, 1, 2, 3, 4, 5,
}

func work1(q walkietalkie) {
    for {
        select {
        case a, more := <- q.in:
            if more {
                q.out <- a * 2
            }
        default:
            break
        }
    }
}

func work2(q walkietalkie) {
    for  {
        select {
        case a, more := <- q.in:
            if more {
                q.out <- a * …
Run Code Online (Sandbox Code Playgroud)

go channels

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

C++ unordered_map <pair <uint,uint>,UserClass> insertion

我试图使用unordered_map带键的to作为类的一对uint和值Tile.

#include <iostream>
#include <unordered_map>
#include <boost/functional/hash.hpp>

class Tile {
public:
  std::pair<uint, uint> coordinate;

  bool operator==(const Tile &t) {
    return this->coordinate.first == t.coordinate.first
      && this->coordinate.second == t.coordinate.second;
  }

  Tile (uint x, uint y) {
    this->coordinate.first = x;
    this->coordinate.second = y;
  }

  std::pair<uint, uint> GetCoor() const 
  {return this->coordinate;}

  uint GetX() const 
  {return coordinate.first;}      

  uint GetY() const 
  {return coordinate.second;}          
};

struct TileHash {
  std::size_t operator()(const Tile &t) const
  {
    size_t seed = 0;
    boost::hash_combine (seed, t.GetX()); …
Run Code Online (Sandbox Code Playgroud)

c++ unordered-map

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

标签 统计

go ×2

c++ ×1

channels ×1

clos ×1

common-lisp ×1

gorilla ×1

lisp ×1

rust ×1

traits ×1

unordered-map ×1