`

Elixir简单的wordcount小程序

 
阅读更多

本文来自:fair-jm.iteye.com 转截请注明出处

 

defmodule Wordcount do
   @moduledoc "simple wordcount program by fairjm"
   @nodecount 3
   
   def start(file) do
      s = self
	  spawn(fn -> count(file,s) end)
   end
   
   defp count(file,caller) do
		s = self
		nodes = Enum.map(1..@nodecount,fn _e -> spawn(fn -> countLine end) end)
		count = File.stream!(file)
				|> Enum.reduce(0,fn e,r -> send(Enum.at(nodes,:random.uniform(@nodecount)-1),{e,s});r+1 end)
		IO.puts("total count is #{count}")
		v = countDown(count,%{})
		nodes |> Enum.each(&(send(&1,Stop)))
		send(caller,v)
   end
   
   defp countDown(0,m) do
		IO.puts("ending")
		m
    end
	
   defp countDown(n,m) do
		receive do
			map -> countDown(n-1, Enum.reduce(map,m,fn {k,v},r -> Map.put(r,k,Map.get(r,k,0)+v) end)) 
			after 5000 -> countDown( n-1 , m)
		end
   end
   
   defp countLine do
		receive do
			{line,caller} ->
				IO.puts("#{inspect(self)} is doing work")
				m = line 
					|> String.strip
					|> String.split(" ")
					|> Stream.filter(&(String.length(&1) > 0))
					|> Enum.reduce(%{},fn e,r ->
												case r[e] do
													nil -> Map.put(r,e,1)
													v   -> Map.put(r,e,v+1)
												end
										end
									)
				send(caller,m)
				countLine
			Stop ->
				IO.puts("#{inspect(self)} quits")
			_other ->
				IO.puts("error message")
				countLine
		end
   end
end

 简单的小练习  代码中用三个erlang进程模拟mapper 一个进程模拟reducer  

 elixir的语法比erlang的要平易近人好多

 有兴趣学的同学 推介买一本elixir in action(http://www.manning.com/juric/)

 或者图灵社区有爱的同学整理的教程: http://www.ituring.com.cn/minibook/10831

 此外这位同学的github上也维护着中文学习资料: https://github.com/elixir-lang-cn/programming-elixir

0
0
分享到:
评论

相关推荐

    Elixir程序设计语言.pdf

    4.4 4.5 5.1 5.2 5.3 5.4 5.5 5.6 Mnesia 数据库 调试第五部分:程序库 Guardian(基础) Poolboy Benchee Bypass Distillery(基础) StreamData 3 Introduction 绪⾔第⼀部分:基础 基础集合Enum 模块 模式匹配 控制...

    Elixir编程入门

    Elixir是一门建立在Erlang虚拟机上的函数式的系统编程语言,支持元编程。创始人José Valim是ruby界的知名人士...Elixir受瞩目的原因,是因为它结合了Erlang作为系统编程语言的各种优点,以及ruby那样简单易懂的语法。

    Elixir程序设计(英文版) pdf

    《Elixir 程序设计》作者就是15 年前编写了Programming Ruby,将Ruby 带入大众视野的Dave Thomas。这一次他延续了一贯的写作风格,以一个拥有面向对象的开发经历但并未接触过函数式编程的开发者的角度切入,循序渐进...

    Elixir 程序设计 英文版 epub

    编程思想的转变是困难的, 我在读这本书的时候, 再一次感受到了编程思想的转变,从面向对象到面向进程。...在 Elixir 中变得无比简单, 这也是 Elixir 在现代 IT 系统中的价值:最大化 CPU 的处理能力。

    Programming.Elixir.1.3

    Programming.Elixir.1.3 英文版

    postgrex:Elixir的PostgreSQL驱动程序

    Elixir的PostgreSQL驱动程序。 文档: : 例子 iex> {:ok, pid} = Postgrex.start_link(hostname: "localhost", username: "postgres", password: "postgres", database: "postgres") {:ok, #PID<0>} iex> ...

    Introducing Elixir.pdf

    Introducing Elixir.pdf 介绍Elixir

    Atom-atom-elixir-formatter,Atom编辑器的Elixir源代码格式化程序。对rgreenjr/atom的贡献.zip

    Atom-atom-elixir-formatter.zip,Atom编辑器的Elixir源代码格式化程序描述,atom是一个用web技术构建的开源文本编辑器。

    Elixir erlang的书籍

    包含Elixir in Action,Elixir中文入门文档,Erlang and Elixir for Imperative Programmers,Erlang OTP并发编程实战-中文版,Erlang程序设计(第2版),Erlang编程指南,Functional.Web.Development.with.Elixir....

    elixir-mongodb-driver:Elixir的MongoDB驱动程序

    elixir-mongodb-driver:Elixir的MongoDB驱动程序

    Programming Elixir

    You’re also investigating designs that help you maximize uptime and manage security., This book is your guide to Elixir, a modern, functional, and concurrent programming language. Because Elixir ...

    Introducing Elixir

    By the end of your journey, you’ll understand why Elixir is ideal for concurrency and resilience., - Get comfortable with IEx, Elixir's command line interface, - Become familiar with Elixir’s basic...

    elixir-style-guide, Elixir语言风格指南,由信条实现.zip

    elixir-style-guide, Elixir语言风格指南,由信条实现 信条 Style 序曲存在这里文档存在的两个原因:这是我的个人风格指南,包括我今天编写的Elixir,但更重要的是,我在野生和适应,因为他们使Elixir更可以读,在...

    emacs-elixir, Emacs主要Elixir模式.zip

    emacs-elixir, Emacs主要Elixir模式 Elixir模式提供字体锁定,缩进和导航支持 Elixir程序语言。 安装工具通过 package.el通过 el手动使用情况交互式命令配置文件Keymapping注释工具集成工具。

    Metaprogramming Elixir(Pragmatic,2015)

    Metaprogramming is one of Elixir's greatest features. Maybe you've played with the basics or written a few macros. Now you want to take it to the next level. This book is a guided series of ...

    Elixir转JavaScriptElixirScript.zip

    ElixirScript 工具的目的是为了将你已有的 Elixir 代码转成 JavaScript 代码。使用方法:usage: ex2js <input> [options]  <input> path to elixir files or  the elixir code string if...

    Programming Elixir 1.6

    Programming Elixir 1.6 Functional |> Concurrent |> Pragmatic |> Fun by Dave Thomas Pages: 410 Published: 2018-05-16 Release: P1.0 (2018-05-17) ISBN: 978-1-68050-299-2 This book is the introduction ...

    elixir安装

    elixir是一个新语言,不是也不是它基于erlang的虚拟机,也就是没有erlang就没有elixir,elixir是一个面向并发的编程思想。

    elixir-1.11.2.tar.gz

    Elixir是基于Erlang虚拟机的函数式、面向并行的通用编程语言,源码安装RabbitMQ时需要。elixir-1.11.2.tar.gz elixir-1.11.2.tar.gz elixir-1.11.2.tar.gz

    elixir中文入门文档

    elixir中文入门文档,ruby+erlang=elixir,高并发开发。

Global site tag (gtag.js) - Google Analytics