标签: elixir

如何编写在日期时间字段上执行group_by MONTH的Ecto查询

我正在做一个ecto查询,我正在尝试分组q.created_date.此查询成功执行GROUP BY,但它在第二次执行.我试图按月分组.

MYQUERY |> group_by([q], [q.created_date, q.id])
Run Code Online (Sandbox Code Playgroud)

是否有类似的东西:

MYQUERY |> group_by([q], [month(q.created_date), q.id])
Run Code Online (Sandbox Code Playgroud)

elixir ecto phoenix-framework

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

在Elixir中使用drop_while?

Elixir的文档说明

drop_while(enumerable, fun)
   Drops items at the beginning of the enumerable while fun returns a truthy value
Run Code Online (Sandbox Code Playgroud)

但是我对下面的输出感到困惑。这是否意味着一旦获得!truthy所有其他一切,就被视为错误?

iex> Enum.drop_while([0,1,2,3,4,5], fn(x) -> rem(x,2) == 0 end)
[1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)

我期望输出是[1,3,5]因为

 iex> Enum.map([0,1,2,3,4,5], fn(x) -> rem(x,2) == 0 end)
[true,false,true,false,true,false]
Run Code Online (Sandbox Code Playgroud)

我试图了解它的工作原理,而不是试图获得我想要的输出(Enum.filter实现结果)

elixir

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

Ecto查询字符串字段包含其他字符串

我正在构建一个简单的搜索功能,我想找到字符串字段中包含字符串的所有记录.

这是我尝试过的.

term = "Moby"
MyApp.Book
|> where([p], String.contains?(p, term))
|> order_by(desc: :inserted_at)
Run Code Online (Sandbox Code Playgroud)

这将返回以下书籍:

  • 白鲸记
  • Sandich Moby Lean
  • 我的Mobyand示例

但我得到:

`String.contains?(p, term)` is not a valid query expression
Run Code Online (Sandbox Code Playgroud)

elixir sql-like ecto

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

结构的透析器错误

这是Elixir 1.3中最小的破坏示例:

defmodule Foo do
  @type t :: %__MODULE__{x: non_neg_integer}
  defstruct x: 0

  @spec test(t) :: t
  def test(%__MODULE__{} = foo), do: test2(foo)

  @spec test2(t) :: t
  defp test2(%__MODULE__{} = foo), do: %__MODULE__{foo | x: 5}

end
Run Code Online (Sandbox Code Playgroud)

这无法使用以下方式键入check : foo.ex:9: The variable _@1 can never match since previous clauses completely covered the type #{'__struct__':='Elixir.Foo', _=>_}.

我搜索和搜索过,不能为我的生活找到解释这意味着什么,或如何解决它.

types elixir dialyzer

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

如何通过其中一个值从列表中获取元组?

我有一个req_headers像元组一样的凤凰列表{key, value}.如何通过列表中的键获取元组的值?像那样:

[{"host", "localhost:4000"}, {"user-agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0"}, {"accept", "application/json, text/plain, */*"}, {"accept-language", "en-US,en;q=0.5"}, {"accept-encoding", "gzip, deflate"}, {"content-type", "application/json;charset=utf-8"}, {"referer", "http://localhost:4000/"}, {"content-length", "16"}, {"connection", "keep-alive"}]
Run Code Online (Sandbox Code Playgroud)

我想获取密钥"host"的值,即" http:// localhost:4000 ".我怎样才能做到这一点?

elixir phoenix-framework

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

为什么el_atom(nil)在elixir中等于true?

非常简单的问题,我在exfile库上工作时注意到了它.

该表达式返回:

is_atom(nil) # => true
Run Code Online (Sandbox Code Playgroud)

这有点不可预测.我的问题是:它是如何发生的?为什么会回来 true?

elixir

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

Elixir中"fun1 arg |> fun2"和"fun1(arg)|> fun2"之间的区别

我一直在Elixir中尝试一些简单的键盘IO,主要是在IO模块中.IO.puts在最基本的形式中它非常方便,它会打印一个提示并等待输入字符串.

我遇到了一些奇怪的行为; "奇怪",因为我无法弄清楚究竟发生了什么.以下是iex会议以及评论和问题:

iex(85)> IO.gets "Your name: "    
Your name: polo
"polo\n"
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好

iex(86)> IO.gets "Your name: " |> String.trim
Your name:polo
"polo\n"
Run Code Online (Sandbox Code Playgroud)

第一个通知,polo不再像以前那样用提示中的空格分隔.此外,该trim函数不适用,因为我得到\n字符串的末尾.

现在,如果我输入:

iex(87)> String.trim(IO.gets "Your name: ")  
Your name: polo
"polo"
Run Code Online (Sandbox Code Playgroud)

一切似乎都回到正常行为:提示中的空间在这里,输入按预期修剪.

最后我还记得读一本书(或者是博客?)两种语法之间有一些细微的差别,所以我试过:

iex(88)> IO.gets("Your name: ") |> String.trim
Your name: polo
"polo"
Run Code Online (Sandbox Code Playgroud)

和宾果游戏!

所以,基本上问题是,和之间有什么区别IO.gets "Your name: " |> String.trimIO.gets("Your name: ") |> String.trim?

elixir

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

为什么我不能在elixir的filter函数中使用or语句?

在灵药中,如果我在列表上操作,得到3的倍数:

1..10 |> Enum.filter(fn(x) -> (rem(x,3) == 0) end)
Run Code Online (Sandbox Code Playgroud)

输出: [3, 6, 9]


好像我添加一个或声明的地方:

1..10 |> Enum.filter(fn(x) -> (rem(x,3) == 0 || rem(x,5)) end)
Run Code Online (Sandbox Code Playgroud)

输出: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


为什么or语句(已被括号括起来)会产生意外输出?

elixir

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

使用Phoenix/Elixir和nginx在服务器上设置https

我知道如何使用nginx为clojure web app设置https.如何为凤凰城做到这一点?在prod.exs我有这个:

config :my_app, MyApp.Endpoint,
  url: [host: "my_website.com", port: 443],
  http: [port: 4000],
#  https: [port: 443,
#          keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"),
#          certfile: System.get_env("SOME_APP_SSL_CERT_PATH")],

  cache_static_manifest: "priv/static/manifest.json"
Run Code Online (Sandbox Code Playgroud)

我有这个:

ssl_certificate:  /etc/letsencrypt/live/my_app.com/fullchain.pem;
ssl_certificate_key: /etc/letsencrypt/live/my_app.com/privkey.pem;
Run Code Online (Sandbox Code Playgroud)

我想在凤凰城也使用nginx.

1)我应该从"prod.exs"中删除"http:[port:4000]"吗?

2)我应该取消注释"https:[port:443,...."?或者我应该同时拥有它们?我不希望网站可以在http访问,或者我让nginx通过将用户从http重定向到https来处理它.

3)或者我应该删除https和http并让nginx处理它?

4)关键"url"及其"端口"怎么样?

ssl https nginx elixir phoenix-framework

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

为什么我不能直接管道进入Elixir String.length.为什么我必须将它包装在另一个函数中?

为什么这不起作用:

iex(2)> Enum.map(["the", "huge", "elephant"], String.length)
** (UndefinedFunctionError) function String.length/0 is undefined or private. 
Run Code Online (Sandbox Code Playgroud)

但这样做:

iex(2)> Enum.map(["the", "huge", "elephant"], fn x -> String.length(x) end) 
[3, 4, 8]
Run Code Online (Sandbox Code Playgroud)

我的意思是,String.length是一个函数,对吗?就像我的匿名包装一样?或者是根据错误消息的某种范围问题?

我对另一种功能(ish)语言的唯一体验是R,这样可以正常工作:

> sapply(c("the", "huge", "elephant"), nchar)
     the     huge elephant 
       3        4        8 
> sapply(c("the", "huge", "elephant"), function(x) nchar(x))
     the     huge elephant 
       3        4        8 
Run Code Online (Sandbox Code Playgroud)

elixir

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

标签 统计

elixir ×10

phoenix-framework ×3

ecto ×2

dialyzer ×1

https ×1

nginx ×1

sql-like ×1

ssl ×1

types ×1