`
jonathan_zz
  • 浏览: 25998 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Functional Programming in Ruby(一)

    博客分类:
  • ruby
阅读更多

接触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一些。呵呵,这只是我自己的拙见,还有太多的东西要学习。

未完待续……

2
0
分享到:
评论

相关推荐

    Introducing Elixir - Getting Started in Functional Programming

    本书向读者介绍了Elixir如何将Erlang的强大的功能编程与一个看起来更像Ruby的方法相结合。读者将会了解Elixir如何简化Erlang的一些恶劣的角落,并通过强大的宏功能达到元编程。介绍Elixer是开发人员对于编程的开发...

    Learning.Scala.Practical.Functional.Programming.for.the.JVM

    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 ...

    Ruby-Functional-Programming, 来自 Conferencia Rails 2011.zip

    Functional-Programming, 来自 Conferencia Rails 2011 通过 Arnau Sanchez实现的ruby 函數式程式設計簡介理論部分Ruby的函數式程式設計不要更新變數不要重用變量用阻止作為高階函數物件導向與函數式程式設計萬物皆...

    Beginning.Ruby.From.Novice.to.Professional.3rd.Edition.1484212797

    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...

    Programming Elixir

    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....

    Beginning Ruby on Rails

    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 ...

    Scala程序设计(第2版) 英文版 Programming Scala Second Edition

    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 ...

    Learning-Scala-Practical-Functional-Programming-for-the-JVM.pdf

    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. ...

    Ruby Recipes(Apress,2016)

    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 ...

    Apress.Beginning.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 ...

    Beginning Ruby(Apress,3ed,2016)

    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...

    Elixir in Action(Manning,2015)

    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

    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...

    programming-hive

    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 ...

    The Joy of Clojure, 2nd Edition

    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, ...

    Introducing Elixir

    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

    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

    Python Data Structures and Algorithms [2017]

    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 ...

Global site tag (gtag.js) - Google Analytics