接触Ruby也有一年多了,没有写过多少代码,只写过一些小的脚本。7月份的时候买了《The Ruby programming language》,打算系统的学习Ruby。虽然已经看过不少的ruby代码,但是看到6.8节“Functional Programming”的时候还是有种眼前一亮的感觉。恩,来看看函数像对象一样在代码间轻盈跃动的感觉吧:
# This module defines methods and operators for FP
module Functional
def apply(enum)
eunm.map &self
end
alias | apply
def reduce(enum)
enum.inject &self
end
alias <= reduce
end
然后将这个模块加入到Proc和Method类中:
class Proc; include Functional; end
class Method; include Functional; end
现在我们可以这样写代码:
sum = lambda {|x, y| x+y}
mean = (sum<=a)/a.size
deviation = lambda {|x| x-mean}
square = lambda {|x| x*x}
standardDeviation = Math.sqrt((sum<=square)|(deviation|a))/(a.size-1)
其实在使用map或者inject这类迭代器操纵block的时候,就已经是在用函数式风格编程,只是上面的magic使得整个的编程过程看起来更lisp一些。呵呵,这只是我自己的拙见,还有太多的东西要学习。
未完待续……
分享到:
相关推荐
本书向读者介绍了Elixir如何将Erlang的强大的功能编程与一个看起来更像Ruby的方法相结合。读者将会了解Elixir如何简化Erlang的一些恶劣的角落,并通过强大的宏功能达到元编程。介绍Elixer是开发人员对于编程的开发...
You don’t need to be a data scientist or distributed computing expert to appreciate this object-oriented functional programming language. This practical book provides a comprehensive yet ...
Functional-Programming, 来自 Conferencia Rails 2011 通过 Arnau Sanchez实现的ruby 函數式程式設計簡介理論部分Ruby的函數式程式設計不要更新變數不要重用變量用阻止作為高階函數物件導向與函數式程式設計萬物皆...
Learn the principles behind object-oriented programming and within a few chapters create a fully functional Ruby application. You'll also gain a basic understanding of many ancillary technologies such...
Ruby-like, extendable syntax, compile and runtime evaluation, a hygienic macro system, and more., But, just as importantly, Elixir brings a sense of enjoyment to parallel, functional programming....
Ruby on Rails is the revolutionary online programming tool that makes creating functional e-commerce web sites faster and easier than ever. With the intuitive, straightforward nature of Ruby and the ...
He works with clients on application design strategies that combine object-oriented programming, functional programming, and aspect-oriented programming. He also consults on Agile methods, like Lean ...
as Java, Ruby, or Python and are interested in improving their craft by learning Scala. Java developers will recognize the core object-oriented, static typing and generic col‐ lections in Scala. ...
These scripts cover areas such as collections, classes and structures, functional programming, and log handling. With these handy recipes at your fingertips, you will be able to solve those niggling ...
Learn the principles behind object-oriented programming and within a few chapters create a fully functional Ruby application. You’ll also gain a basic understanding of many ancillary technologies ...
Learn the principles behind object-oriented programming and within a few chapters create a fully functional Ruby application. You'll also gain a basic understanding of many ancillary technologies such...
Then, you’ll learn to think about problems using Elixir’s functional programming mindset. With that solid foundation, you’ll confidently explore Elixir’s seamless integration with BEAM and Erlang...
Programming Language Explorations is a tour of several modern programming languages in use today. The book teaches fundamental language concepts using a language-by-language approach. As each language...
Besides Big Data, he specializes in Scala, the JVM ecosystem, JavaScript, Ruby, functional and object-oriented programming, and Agile methods. Dean is a frequent speaker at industry and academic ...
You'll learn functional and declarative approaches to programming and will master the techniques that make Clojure so elegant and efficient. Purchase of the print book includes a free eBook in PDF, ...
Elixir combines the robust functional programming of Erlang with an approach that looks more like Ruby and reaches toward metaprogramming with powerful macro features., Authors Simon St. Laurent and ...
Introduction to JVM Languages English | 2017 | ISBN-10: ...Boost your knowledge about the dialects of other well-known programming languages that run on JVM, including JavaScript, Python, and Ruby
Get a better understanding of advanced Python concepts such as big-o notation, dynamic programming, and functional data structures. Explore illustrations to present data structures and algorithms, as ...