我努力学习仙丹(但主要是函数式编程)药剂和功能输入验证
我实现一个非常简单的GenServer基本上包装条目列表。条目,每个条目参数的时最大计数和最大尺寸(以字节为单位)限制(配置文件)
defmodule List do
def init(_) do
{:ok, []}
end
def handle_call({:insert, param1, param2, param3}, from, list) do
import Application
param1_max_size = get_env(:app, ....)
param2_max_size = get_env(:app, ....)
param2_max_size = get_env(:app, ....)
max_items_count = get_env(:app, ....)
## should be {:reply, {:error, :your_list_is_full}, list} if list is full
## should be {:reply, {:error, {:check_this_args_please, wrong_params_list}, list} if any param is wrong. wrong_params_list contains the offending params
## should be {:reply, {:ok}, [{param1, param2, param3} | list ]} otherwise
end
end
我知道这似乎很容易,但基本上我试图找到一个优雅的功能性的方式来做到这一点。我的头脑是程序性的,并且我总是以=运算符的形式出现在“嵌套的if-else-hell”中,就像它是C语言一样。
THX
这真的很好,thx – justatester