是否有函數式編程的優秀實例?

函數式編程的概念看了以後覺得理解了,但是應用起來卻無從下手。是否有函數式編程的優秀實例可以參考的,請大神們附上github地址,小弟感激不已。


謝邀, 之前看到這個問題 title 就很伸手, 看完說明之後就更覺得伸手了

一來沒提什麼語言, 就提了個函數式, 關於這點可以看這裡: 知乎專欄

二來

&> 函數式編程的概念看了以後覺得理解了,但是應用起來卻無從下手

在偉大的 KR 里有一句

&> The only way to learn a new programming language is by writing programs in it.

都沒動手, 談何學習, 談何理解

----------------------------------------焦作人的分割線----------------------------------------

既然加了 Haskell 的標籤就拿一個 Haskell 項目作為例子, 選的是: xmonad - Wikipedia

( 不知道什麼是窗口管理器或者常年用 Windows 的童鞋自己到上面的鏈接接受科普)

XMonad 這項目稱得上優秀的地方有:

- 是個實際項目, 已經走進千萬尋常百姓家

- 涉及到好多 Haskell 的特性和函數式編程里的概念(FFI, 持久化數據結構, Monad, API 設計)

- 用了 Quickcheck 來推進 API 設計和測試

- 代碼量不大

- SPJ 也拿它作為例子

----------------------------------------胡說八道的分割線----------------------------------------

首先一個 Real World 的程序肯定是個大大的 Monad, Real World 裡面充滿了 State, IO, bla bla 各種 Side Effect, 而且 XMonad要跟 X11 交互, 所以要有 FFI

然後我們來抽象問題, 對問題進行建模, 搞數據結構. 智商180, 吃過金坷垃, 拿過 PHD, Paper 過目不忘信手拈來, 一下子想好了:

牛逼的架構:

精簡的核心:

API 設計得太牛逼, 還"axiomatic"了, 那誰蛋疼拿 Coq 寫了一遍

代碼: wouter-swierstra/xmonad 論文: science.uu.nl 的頁面

參考:

看 wiki 下面的鏈接就行了


聽說有人理解了函數式編程,然後想要個優秀的實例, ,,英國格拉斯哥大學的ghc的項目,還是十分優秀的,據說,帶著一堆論文,

github的連接https://github.com/ghc/ghc

(1.12更新,修正一下Glasgow 的翻譯)

所以說,趕快逃。。。


泛泛而談,很多項目都吸取了函數式編程的良好實踐。

可以參考一下Martin Abadi在ICFP2016上的keynote: TensorFlow: Learning Functions at Scale


Introduction to Lodash - js Video Tutorial #free


Wolfram Demonstrations Project

Mathematica 官方示例,數學、計算科學、物理、生物、商業、工程……例子非常多,涵蓋的學科範圍也極廣。

mathematica本身就是偏函數式的編程語言


scalaz,雪川大蟲的博客分析這個了,感覺很好


怎麼就沒有 erlang 了 Erlang -- Concurrent Programming#A Larger Example

一個簡易的聊天伺服器

-module(messenger).
-export([start_server/0, server/1, logon/1, logoff/0, message/2, client/2]).

%%% Change the function below to return the name of the node where the
%%% messenger server runs
server_node() -&>
messenger@localhost. % 本地跑就 localhost 吧

%%% This is the server process for the "messenger"
%%% the user list has the format [{ClientPid1, Name1},{ClientPid22, Name2},...]
server(User_List) -&>
receive
{From, logon, Name} -&>
New_User_List = server_logon(From, Name, User_List),
server(New_User_List);
{From, logoff} -&>
New_User_List = server_logoff(From, User_List),
server(New_User_List);
{From, message_to, To, Message} -&>
server_transfer(From, To, Message, User_List),
io:format("list is now: ~p~n", [User_List]),
server(User_List)
end.

%%% Start the server
start_server() -&>
register(messenger, spawn(messenger, server, [[]])).

%%% Server adds a new user to the user list
server_logon(From, Name, User_List) -&>
%% check if logged on anywhere else
case lists:keymember(Name, 2, User_List) of
true -&>
From ! {messenger, stop, user_exists_at_other_node}, %reject logon
User_List;
false -&>
From ! {messenger, logged_on},
[{From, Name} | User_List] %add user to the list
end.

%%% Server deletes a user from the user list
server_logoff(From, User_List) -&>
lists:keydelete(From, 1, User_List).

%%% Server transfers a message between user
server_transfer(From, To, Message, User_List) -&>
%% check that the user is logged on and who he is
case lists:keysearch(From, 1, User_List) of
false -&>
From ! {messenger, stop, you_are_not_logged_on};
{value, {From, Name}} -&>
server_transfer(From, Name, To, Message, User_List)
end.
%%% If the user exists, send the message
server_transfer(From, Name, To, Message, User_List) -&>
%% Find the receiver and send the message
case lists:keysearch(To, 2, User_List) of
false -&>
From ! {messenger, receiver_not_found};
{value, {ToPid, To}} -&>
ToPid ! {message_from, Name, Message},
From ! {messenger, sent}
end.

%%% User Commands
logon(Name) -&>
case whereis(mess_client) of
undefined -&>
register(mess_client,
spawn(messenger, client, [server_node(), Name]));
_ -&> already_logged_on
end.

logoff() -&>
mess_client ! logoff.

message(ToName, Message) -&>
case whereis(mess_client) of % Test if the client is running
undefined -&>
not_logged_on;
_ -&> mess_client ! {message_to, ToName, Message},
ok
end.

%%% The client process which runs on each server node
client(Server_Node, Name) -&>
{messenger, Server_Node} ! {self(), logon, Name},
await_result(),
client(Server_Node).

client(Server_Node) -&>
receive
logoff -&>
{messenger, Server_Node} ! {self(), logoff},
exit(normal);
{message_to, ToName, Message} -&>
{messenger, Server_Node} ! {self(), message_to, ToName, Message},
await_result();
{message_from, FromName, Message} -&>
io:format("Message from ~p: ~p~n", [FromName, Message])
end,
client(Server_Node).

%%% wait for a response from the server
await_result() -&>
receive
{messenger, stop, Why} -&> % Stop the client
io:format("~p~n", [Why]),
exit(normal);
{messenger, What} -&> % Normal response
io:format("~p~n", [What])
end.


推薦閱讀:

JSX有哪些缺陷?
Vue 官網上的這個圖是用什麼工具畫的?

TAG:JavaScript | 函數式編程 | Haskell |