public
Description: Aims to extend Ruby standard library, providing some useful tools that's not existed in the standard library, especially for functional programming.
Homepage: http://rubyforge.org/projects/ludy
Clone URL: git://github.com/godfat/ludy.git
ludy / README
100644 216 lines (166 sloc) 5.995 kb
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
= ludy 0.1.13
by Lin Jen-Shin (a.k.a. godfat-真常[http://godfat.org])
  godfat (XD) godfat.org
 
== LINKS:
 
* rdoc[http://ludy.rubyforge.org]
* rubyforge-project[http://rubyforge.org/projects/ludy]
* github-project[http://github.com/godfat/ludy]
* redmine-project[https://redmine.godfat.org/projects/show/ludy]
 
== DESCRIPTION:
 
Aims to extend Ruby standard library, providing some useful tools that's not existed in the standard library, especially for functional programming.
 
== FEATURES:
 
1. ludy standard library extension, especially for functional programming.
2. rails/array paginator, included since 0.1.2
3. c++ erb meta-programming, included since 0.1.0 (support erubis 2.5+)
4. puzzle_generator, included since 0.0.8
 
== SYNOPSIS:
 
please see unit test for all examples.
 
    $ ludy test # run all ludy tests
    $ ludy # see all tasks related to ludy
 
 please don't run any task about release... contact me first
 if you would like to make changes into official ludy release.
 
    require 'ludy/all' # for all ludy things
    require 'ludy/kernel' # for all kernel methods
    require 'ludy/tasks' # for all ludy tasks
    require 'ludy/proc/curry' # for proc's method curry
 
 you can make any change and then $ ludy gem:install on your local machine.
 below is some example extracted from unit test.
 
curry:
  require 'ludy/proc/curry'
  assert_equal 29, :+.to_proc.curry[18][11]
 
bind:
  require 'ludy/proc/bind'
  assert_equal [9,8,7], ([1,2,3].map &(lambda{|lhs, rhs| lhs-rhs}.bind 10, :_1))
 
defun:
  require 'ludy/kernel/defun'
  defun :fact, 0 do |n|
    1
  end
 
  defun :fact, Integer do |n|
    n * fact(n-1)
  end
 
  assert_equal 3628800, fact(10)
 
  defun :f, Integer do |n| 1; end
  defun :f, String do |n| '2'; end
  defun(:f, Integer, Integer) do |n,g| 3; end
 
  assert_equal 1, f(10)
  assert_equal '2', f('')
  assert_equal 3, f(1,1)
  assert_raise NoMethodError do f('1', 2); end
 
lazy:
  require 'ludy/lazy'
  Y = lambda{|f|
    lambda{|x| lazy{f[x[x]]} }[lambda{|x| lazy{f[x[x]]} }]
  }
 
y_combinator:
  require 'ludy/y_combinator'
  fact = Y[lambda{|this|
    lambda{|n| n==1 ? 1 : n*this[n-1]}
  }]
  assert_equal(3628800, fact[10])
 
paginator:
  pager = Ludy::RailsPaginator.new(Topic)
  pager = Ludy::ArrayPaginator.new(TestPaginator.data)
  # or the most flexible paginator:
  pager = Ludy::Paginator.new(
  lambda{ |offset, per_page|
    # if for rails,
    # Data.find :all, :offset => offset, :limit => per_page
    TestPaginator.data[offset, per_page]
  }, lambda{
    # if for rails,
    # Data.count
    TestPaginator.data.size
  })
  pager.per_page = 10
 
  pager.page(1)
  pager[1] # same as above
  pager.page(2).next.prev.each{|e| e} # you can enumerate
  pager[3].map{|e| e*2}
  pager.size # number of pages
  pager.inject(0){|r, i| r += i.inject &:+ } # total sum of all data
 
ludy tasks:
 
 
header_guard:
  require 'ludy/tasks/erb_cpp/header_guard'
  Project.name = 'header_sample' # remember to set project name
 
  # in utils/SimpleTest.hpp.erb
  <% header_guard do %>
  class C;
  <% end %>
 
  # run preprocessing task
  $ rake preprocessing
 
  # produce:
  #ifndef _HEADER_SAMPLE_UTILS_SIMPLETEST_
  #define _HEADER_SAMPLE_UTILS_SIMPLETEST_
  class C;
  #endif
 
attr_builder:
  require 'ludy/tasks/erb_cpp' # it would require all erb_cpp tasks.
 
  # in MapSetting.hpp.erb
  class MapSetting{
  <%= accessor :int, :frequency %>
  <%= accessor :double, :speed, :damage_factor %>
  <%= reader :int, :width, :height, :size %>
  };
 
  # run preprocessing task
  $ rake preprocessing
 
  # produce:
  class MapSetting{
  public:
      int frequency() const{ return frequency_; }
  public:
      MapSetting& frequency(int const& new_frequency){ frequency_ = new_frequency; return *this; }
  private:
      int frequency_;
 
  public:
      double speed() const{ return speed_; }
      double damage_factor() const{ return damage_factor_; }
  public:
      MapSetting& speed(double const& new_speed){ speed_ = new_speed; return *this; }
      MapSetting& damage_factor(double const& new_damage_factor){ damage_factor_ = new_damage_factor; return *this; }
  private:
      double speed_, damage_factor_;
 
  public:
      int width() const{ return width_; }
      int height() const{ return height_; }
      int size() const{ return size_; }
  private:
      int width_, height_, size_;
  };
 
template_forward_parameters:
  require 'ludy/tasks/erb_cpp/template_forward_parameters'
 
  # in test.hpp.erb
  <% for_template_parameters_within(1..5, []){ |args_list| %>
      template <%= template_parameters args_list %>
      static element_type create(<%= forward_parameters args_list %>){
          return element_type(SPool::Instance().construct(<%= arguments args_list %>), Deleter());
      }
  <% } %>
 
  # run preprocessing task
  $ rake preprocess
 
  # one of the produce: (can't list all...)
  template <class T0, class T1, class T2, class T3>
  static element_type create(T0 const& a, T1 const& b, T2 & c, T3 const& d){
      return element_type(SPool::Instance().construct(a, b, c, d), Deleter());
  }
 
== REQUIREMENTS:
 
* ruby 1.8/1.9 (1.9 is prefered...)
* rake in some features
* erb in task preprocess:erb
* gem erubis if you are using preprocess:erubis
* gem facets is recommended if you are using puzzle_generator
* gem ZenTest if you are running multiruby.sh in test
 
== INSTALL:
 
* sudo gem install ludy
 
== LICENSE:
 
Apache License 2.0
 
Copyright (c) 2008, Lin Jen-Shin (a.k.a. godfat 真常)
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
   http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.