Examples of functional codes in ruby

I am looking for examples of functional code in ruby. Maybe you know some gems where I can find such a code?

+4
source share
6 answers

I have compiled examples of functional programming with Ruby:

+7
source

Have you looked at Enumerable?

(1..100).select { |i| i % 5 == 0 }.map { |i| i * 5 }.take(3) #=> [25, 50, 75]
+4
source

, mixin, , Enumerable, , . , , , , fold - , foreach, , .

, g, . , , , , , Enumerable PITA Ruby. (, , Ruby . Ruby, Enumerable Java # , , , Ruby, .)

Enumerable , , , fold ( reduce/inject, Ruby) , .

module Enumerable
  def all?
    reduce(true) {|res, el| res && yield(el) }
  end

  def any?
    reduce(false) {|res, el| res || yield(el) }
  end

  def collect
    reduce([]) {|res, el| res + yield(el) }
  end
  alias_method :map, :collect

  def count
    reduce(0) {|res, el| res + 1 if yield el }
  end

  def detect
    reduce(nil) {|res, el| if yield el then el end unless res }
  end
  alias_method :find, :detect

  def drop(n=1)
    reduce([]) {|res, el| res.tap {|res| res + el unless n -= 1 >= 0 }}
  end

  def each
    reduce(nil) {|_, el| yield el }
  end

  def each_with_index
    reduce(-1) {|i, el| (i+1).tap {|i| yield el, i }}
  end

  def find_all
    reduce([]) {|res, el| res.tap {|res| res + el if yield el }}
  end
  alias_method :select, :find_all

  def grep(pattern)
    reduce([]) {|res, el| res.tap {|res| res + yield(el) if pattern === el }}
  end

  def group_by
    reduce(Hash.new {|hsh, key| hsh[key] = [] }) {|res, el| res.tap {|res|
        res[yield el] << el
    }}
  end

  def include?(obj)
    reduce(false) {|res, el| break true if res || el == obj }
  end

  def reject
    reduce([]) {|res, el| res.tap {|res| res + el unless yield el }}
  end
end

, . , . , , reduce, mixin , .

Prelude Ruby, Haskell Prelude ( Haskell Ruby ) Ruby.

+4

" Ruby" , , Haskell.

, "Ruby", " " .

+1

. .

Something you need to think about when implementing functional concepts in ruby ​​is that our vm is not designed for it, so without transparent link optimization, you will consume a lot more memory in a functional style.

# non functional
class Post
  attr_accessor :title, :body
end

all_posts = []

# create a post
p = Post.new
p.title = "Hello world"
p.body  = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

# put post in the array
all_posts << p

# fix post title
all_posts[0].title = "Opps, fixed title"

#functional
class Post
  attr_reader :title, :body

  def initialize(attrs={})
    attrs.each {|k,v| instance_variable_set "@#{k.to_s}", v }
  end
end

all_posts = []

# create a post
p = Post.new({title: "Hello world", body: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."})
# add to array via new array creation
all_posts = all_posts + [p] # be wary when all_posts is a very large collection!

old_post = p
p = Post.new({title: "Oops, fixed title", body: old_post.body})
all_posts = all_posts - [old_post] + [p]

I often post functional ruby ​​programming on my blog: http://rubylove.io

0
source

All Articles