小编Зел*_*ный的帖子

erlang - quicksort.来自learnyousomeerlang的标准快速排序程序

有什么不同?为什么我只获得列表中的第一个元素?

    -module(hello).
-export(quicksort/1,                                                                                 
    lc_quicksort/1]).

quicksort([]) -> []; 
quicksort([Pivot|Rest]) ->
    quicksort([x || x <- Rest, x =< Pivot])
    ++[Pivot]
    ++ quicksort([y || y <- Rest, y > Pivot]).

lc_quicksort([]) -> []; 
lc_quicksort([Pivot|Rest]) ->
lc_quicksort([Smaller || Smaller <- Rest, Smaller =< Pivot])
++ [Pivot] ++
lc_quicksort([Larger || Larger <- Rest, Larger > Pivot]).



1> c("hello.erl").

{ok,hello}

2> hello:quicksort([3, 1, 4, 2, 5, 9]).

[3]

3>  hello:lc_quicksort([3, 1, 4, 2, 5, 9]).

[1,2,3,4,5,9]
Run Code Online (Sandbox Code Playgroud)

我错过了什么?发布这个以了解我在学习时错过的erlang功能.

谢谢.

syntax erlang quicksort

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

使用Ruby 2.1.1中的else语句定义语法

ruby是否支持使用以下代码的语法:

class Test
 def test
  #some code here
 else
  #some code here
 end
end
Run Code Online (Sandbox Code Playgroud)

我发现这个语法是有效的,并且ruby解释器没有为此标记任何异常.如果这是有效的,任何人都可以解释此语法的用法.

目前使用Ruby 2.1.1

ruby syntax ruby-2.1

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

here-document delete first line from file with vim like ed

I am using this snippet code (just delete first line with ed). I wanna know if i can make something like this in vim. I wrote the script and passed the file as an argument.

file:

# This is a comment #

foo bar
Run Code Online (Sandbox Code Playgroud)

edit with ed:

ed $1 << EOF
1/^[ ]*$/d
w 
q
EOF
Run Code Online (Sandbox Code Playgroud)

I tried with vim:

vi $1 << EOF
dd
w 
q
EOF

> Vim: Warning: Input is not from a …
Run Code Online (Sandbox Code Playgroud)

vim bash ed

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

ActiveRecord :: StatementInvalid(PG :: SyntaxError:ERROR:语法错误位于或接近"."

我不确定为什么我的查询在localhost上运行但在服务器上失败.当我尝试创建一个路由到QuizzesController #new的测验时会发生这种情况

# GET /quizzes/new
  def new
    @quiz = current_user.quizzes.new
  end
Run Code Online (Sandbox Code Playgroud)

这是查询:

SELECT COUNT(*) FROM "questions" INNER JOIN "question_categories" ON "question_categories"."question_id" = "questions"."id" WHERE "questions"."deleted_at" IS NULL AND (`question_categories`.`category_id` IN (87,1))

(1.0ms)  ROLLBACK
Completed 500 Internal Server Error in 58ms (ActiveRecord: 13.4ms)
Run Code Online (Sandbox Code Playgroud)

我得到了一个错误.

ActiveRecord::StatementInvalid (PG::SyntaxError: ERROR:  syntax error at or near "." LINE 1: ...s"."deleted_at" IS NULL AND (`question_categories`.`category...
Run Code Online (Sandbox Code Playgroud)

quiz.rb在创建之前我会运行build_parts,它应该随机抓取问题并将它们放入测验中.class Quiz <ActiveRecord :: Base belongs_to:user belongs_to:subject has_many:quiz_categories has_many:categories,through :: quiz_categories has_many:quiz_parts

  accepts_nested_attributes_for :categories
  accepts_nested_attributes_for :quiz_parts

  validates :user, :subject, :number_of_questions, presence: …
Run Code Online (Sandbox Code Playgroud)

postgresql activerecord ruby-on-rails

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

周二和周五使用Rails-Schedule

我正在使用我的Rails应用程序中的任何计划。在gem的文档中,我找不到如何在每周的第二个星期二和星期五运行cronjobs。

这就是我现在正在做的正确吗?

every [:tuesday, :friday], at: '12:00am', :roles => [:my_role] do
  runner "User.notify"
end
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails crontab whenever

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

从Nginx提供Golang脚本

我想用Go语言编写一个Web应用程序。

运行时:

go run myscript.go
Run Code Online (Sandbox Code Playgroud)

它工作正常,我使用过“ net / http”模块,这是我在go脚本中所做的事情:

http.ListenAndServe(":8081", nil)
Run Code Online (Sandbox Code Playgroud)

我现在想与Nginx合作。我已经读过我应该将nginx置于代理模式。这意味着,当nginx在80 http端口上收到http请求时,它将代理它做8081端口。

我如何自动优化并重新启动“运行”过程?

nginx go

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

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

link_to first name and last name as one link

I have the first name and last name which both link to the users profile:

= link_to (@post.user.fname), user_path(@post.user_id) 
= link_to (@post.user.lname), user_path(@post.user_id)
Run Code Online (Sandbox Code Playgroud)

These are two separate links. When I hover the mouse over the first name it only highlights the first name. And vice versa. (Example is just a randomly generated name)

How do I make the first name and last name one single link_to so it underlines the full name?

enter image description here

ruby ruby-on-rails hyperlink link-to

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

检查范围内的日期

我想检查日期2017-09-21 15:30:00 -0400是否在本周范围内.

这是下面的代码:我试图输出true或false但它没有输出正确的答案.理论上这应该有效.

 dateTime = Time.new(2017, 9, 21, 15, 30) => 2017-09-21 15:30:00 -0400 

 Range.new((Date.today), (Date.today+7.days)).include?(dateTime)
Run Code Online (Sandbox Code Playgroud)

基本上我试图检查dateTime变量是否在本周内.但我使用Time.new初始化它

ruby ruby-on-rails

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

如何将 interface {} 转换为 struct

我一直在寻找如何将接口转换为结构,但我不知道我怎么做不到。

我会尽力解释我的问题。

type Result struct {
    Http_code int
    Http_msg  string
    Response  interface{}}
Run Code Online (Sandbox Code Playgroud)

该结构由向服务器发出 HTTP 请求的函数返回,另一方面,我有不同类型的结构来包装响应。

这是我想要转换接口的结构。

type ResHealth struct {
    Type       string
    Get_health struct {
       Healthy bool
 }}    
Run Code Online (Sandbox Code Playgroud)

我的问题是,当我尝试做出断言时,我总是遇到段冲突或程序无法编译。

工作流程是:

package test

type Result struct {
    Http_code int
    Http_msg  string
    Response  interface{}
}

type ResHealth struct {
    Type       string
    Get_health struct {
        Healthy bool
    }
}
func Do() Result {
  var http_response Result
  var health ResHealth
  +++do something+++
  http_response.Response = health
  return http_response
}

package Test2

aux := post.Do()
aux.Response.(ResHealth) …
Run Code Online (Sandbox Code Playgroud)

struct casting type-conversion data-conversion go

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