github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

shvets / design_patterns_in_ruby

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 165
    • 5
  • Source
  • Commits
  • Network (5)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Implementation of Design Patterns in Ruby — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

added act_as_observable to make dynamic code generation for observables Comment
shvets (author)
Sun Mar 22 08:28:29 -0700 2009
commit  698dcfab8a2d89909d49f8b6207beb7df4563bf1
tree    e63925a79d07d54eb8612e034e06f535a6768960
parent  b739593f4346a72e183c0b2ec95ea3ac5d6baace
design_patterns_in_ruby / enterprise / map-reduce.rb enterprise/map-reduce.rb
100644 115 lines (77 sloc) 2.723 kb
edit raw blame history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# map-reduce.bsh
 
# http://www.theserverside.com/tt/knowledgecenter-tc/knowledgecenter-tc.tss?l=MapReduce
# http://www.theserverside.com/tt/articles/article.tss?l=MapReduceRedux
 
# MapReduce is a distributed programming model intended for processing massive amounts of data in large clusters,
# developed by Jeffrey Dean and Sanjay Ghemawat at Google. The implementation of MapReduce separates the business
# logic from the multi-processing logic.
#
# MapReduce is implemented as two functions, Map which applies a function to all the members of a collection and
# returns a list of results based on that processing, and Reduce, which collates and resolves the results from
# two or more Maps executed in parallel by multiple threads, processors, or stand-alone systems. Both Map()
# and Reduce() may run in parallel, though not necessarily in the same system at the same time.
 
# 1. functors interfaces
 
class MapFunctor
  def function(object)
  end
end
 
class ReduceFunctor
  def function(list, object)
  end
end
 
# 2. map-reduce interface
 
class MapReduce
  def map(map_functor, list)
  end
 
  def reduce(reduce_functor, list, init_list)
  end
end
 
 
# 3. implementations
 
class MapFunctorImpl < MapFunctor
 def function(object) # copier
   object
 end
end
 
class ReduceFunctorImpl < ReduceFunctor
 
 def function(list, object) # duplication reducer
   if(!list.include? object)
     list << object
   end
 
   list
 end
 
end
 
class MapReduceImpl < MapReduce
 
  def map(map_functor, list)
    intermediate_result = []
 
    list.each do |element|
      result = map_functor.function(element)
 
      intermediate_result << result
    end
 
    intermediate_result
  end
 
  def reduce(reduce_functor, list, init_list)
    result = init_list
 
    list.each do |value|
      result = reduce_functor.function(result, value)
    end
 
    result
  end
 
end
 
# 4. test
 
# input data
 
bucket1 = [ "1", "2", "3", "4", "5" ]
 
bucket2 = [ "6", "4", "8", "5", "10" ]
 
# Business logic is concentrated in functors
 
map_functor = MapFunctorImpl.new
reduce_functor = ReduceFunctorImpl.new
 
# Different instances of map-reduce objects. They can be used for "map" or "reduce" operations.
 
map_reduce1 = MapReduceImpl.new
map_reduce2 = MapReduceImpl.new
map_reduce3 = MapReduceImpl.new
 
intermediate_data1 = map_reduce1.map(map_functor, bucket1)
intermediate_data2 = map_reduce2.map(map_functor, bucket2)
 
final_data = map_reduce3.reduce(reduce_functor, intermediate_data2, intermediate_data1)
 
puts "bucket1: " + bucket1.join(', ')
puts "bucket2: " + bucket2.join(', ')
 
puts "intermediate data 1: " + intermediate_data1.join(', ')
puts "intermediate data 2: " + intermediate_data2.join(', ')
 
puts "final data: " + final_data.join(', ')
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server