我努力學習仙丹(但主要是函數式編程)藥劑和功能輸入驗證
我實現一個非常簡單的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