`
jonathan_zz
  • 浏览: 26593 次
  • 性别: 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
分享到:
评论

相关推荐

    Learning Functional Programming in Go-Packt Publishing(2017).pdf

    《Learning Functional Programming in Go》是一本系统介绍如何在Go语言中运用函数式编程思想的书籍。通过学习本书,开发者可以更好地理解函数式编程的概念,并将其应用于实际项目中,从而编写出更加简洁、高效且...

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

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

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

    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

Global site tag (gtag.js) - Google Analytics