arrays 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 10644639116d6dabbe13a4ff4aea0b9b096a12ff
4
+ data.tar.gz: 80b948385a8bfcee4cd1a8660aa313d1f7763c3b
5
+ SHA512:
6
+ metadata.gz: c06a24e7b1e3ef299ce0f39fd71dc04981384d25ea0f5e2ca7c24043fd2ffc38e2c89c6472c023012bb255688e19605e9d2f1163933caeb93e248abf8852ebe7
7
+ data.tar.gz: d7fd728eb22770cf0357e33be3b329ee13e0f31f396a9b2952043684fa18a9254c31644841d37b29a50165ed590006d05afbd2becafd2adebe2dc3f59cde17f8
data/RakeFile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
data/arrays-0.0.1.gem ADDED
Binary file
data/arrays.gemspec ADDED
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'arrays'
3
+ s.version = '0.0.1'
4
+ s.date = '2013-07-23'
5
+ s.summary = "useful method for array like list"
6
+ s.description = "array extensions for functional programming"
7
+ s.authors = ["daewon"]
8
+ s.email = "blueiur@gmail.com"
9
+ s.files = Dir.glob("**/**/**")
10
+ s.homepage = 'http://daewon.github.com'
11
+ end
12
+
13
+
14
+
data/lib/arrays.rb ADDED
@@ -0,0 +1,51 @@
1
+ # Array extension
2
+ class Array
3
+
4
+ # cluster element with keep order
5
+ # accept binary predicate function
6
+ # [1, 2, 2, 2].cluster { |a, b| a == b }
7
+ # => [[1], [2, 2, 2]]
8
+ def cluster &block
9
+ self.inject([]) { |acc, item|
10
+ if acc.empty?
11
+ [[item]]
12
+ else
13
+ if block.call(acc[-1][-1], item)
14
+ acc[-1] << item
15
+ else
16
+ acc << [item]
17
+ end
18
+ acc
19
+ end
20
+ }
21
+ end
22
+
23
+ # throw exception if list is empty
24
+ def check_empty_list
25
+ throw ":empty list" if self.empty?
26
+ end
27
+
28
+ # drop last one element
29
+ # [1, 2].tail
30
+ # => [2]
31
+ def init
32
+ check_empty_list
33
+ self.take(self.length-1)
34
+ end
35
+
36
+ # drop one element
37
+ # [1, 2].tail
38
+ # => [2]
39
+ def tail
40
+ check_empty_list
41
+ self.drop(1)
42
+ end
43
+
44
+ # take one element
45
+ # [1, 2].head
46
+ # => 1
47
+ def head
48
+ self.first
49
+ end
50
+ end
51
+
data/lib/arrays.rb~ ADDED
@@ -0,0 +1,65 @@
1
+ #!`which ruby`
2
+
3
+ # Array extension
4
+ class Array
5
+ # cluster element with keep order
6
+ # accept binary predicate function
7
+ # [1, 2, 2, 2].cluster { |a, b| a == b }
8
+ # => [[1], [2, 2, 2]]
9
+ def cluster &block
10
+ self.inject([]) { |acc, item|
11
+ if acc.empty?
12
+ [[item]]
13
+ else
14
+ if block.call(acc[-1][-1], item)
15
+ acc[-1] << item
16
+ else
17
+ acc << [item]
18
+ end
19
+ acc
20
+ end
21
+ }
22
+ end
23
+
24
+ # throw exception if list is empty
25
+ def check_empty_list
26
+ throw ":empty list" if self.empty?
27
+ end
28
+
29
+ # drop last one element
30
+ # [1, 2].tail
31
+ # => [2]
32
+ def init
33
+ check_empty_list
34
+ self.take(self.length-1)
35
+ end
36
+
37
+ # drop one element
38
+ # [1, 2].tail
39
+ # => [2]
40
+ def tail
41
+ check_empty_list
42
+ self.drop(1)
43
+ end
44
+
45
+ # take one element
46
+ # [1, 2].head
47
+ # => 1
48
+ def head
49
+ self.first
50
+ end
51
+
52
+ end
53
+
54
+ # test
55
+ cluster = [1, 2, 3, 3, 3, 5, 5, 6].cluster {|a, b| a == b }
56
+ p cluster.inspect
57
+
58
+ init = [1, 2].init
59
+ p init.inspect
60
+
61
+ tail = [1, 2].tail
62
+ p tail.inspect
63
+
64
+ head = [1, 2].head
65
+ p head.inspect
@@ -0,0 +1,21 @@
1
+ require 'test/unit'
2
+ require 'arrays'
3
+
4
+ class ArraysTest < Test::Unit::TestCase
5
+ def test_head
6
+ assert_equal(1, [1, 2, 3].head)
7
+ assert_equal(nil, [].head)
8
+ end
9
+
10
+ def test_init
11
+ assert_equal([1, 2], [1, 2, 3].init)
12
+ end
13
+
14
+ def test_tail
15
+ assert_equal([2, 3], [1, 2, 3].tail)
16
+ end
17
+
18
+ def test_cluster
19
+ assert_equal([[1], [2], [3]], [1, 2, 3].cluster { |a, b| a == b })
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ require 'test/unit'
2
+ require 'hola'
3
+
4
+ class HolaTest < Test::Unit::TestCase
5
+ def test_english_hello
6
+ assert_equal "hello world",
7
+ Hola.hi("english")
8
+ end
9
+
10
+ def test_any_hello
11
+ assert_equal "hello world",
12
+ Hola.hi("ruby")
13
+ end
14
+
15
+ def test_spanish_hello
16
+ assert_equal "hola mundo",
17
+ Hola.hi("spanish")
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arrays
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - daewon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: array extensions for functional programming
14
+ email: blueiur@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - arrays-0.0.1.gem
20
+ - arrays.gemspec
21
+ - lib/arrays.rb
22
+ - lib/arrays.rb~
23
+ - RakeFile
24
+ - test/test_arrays.rb
25
+ - test/test_arrays.rb~
26
+ homepage: http://daewon.github.com
27
+ licenses: []
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.0.5
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: useful method for array like list
49
+ test_files: []