elixir.rb 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ module Elixir
2
+ module File
3
+ module_function
4
+
5
+ def cd path
6
+ :ok if Dir.chdir(path).zero?
7
+ rescue Errno::ENOENT
8
+ [:error, :enoent]
9
+ end
10
+
11
+ def cd! path
12
+ :ok if Dir.chdir(path).zero?
13
+ end
14
+
15
+ # TODO: All the other File module functions.
16
+ end
17
+ end
@@ -0,0 +1,34 @@
1
+ module Elixir
2
+ module Float
3
+ module_function
4
+
5
+ def ceil number, precision = 0
6
+ # TODO: precision
7
+ number.ceil
8
+ end
9
+
10
+ def floor number, precision = 0
11
+ # TODO: precision
12
+ number.floor
13
+ end
14
+
15
+ def parse string
16
+ return :error unless string =~ /\A\d/
17
+ string_match = string.match /\A\d+\.?\d*/
18
+
19
+ [Float(string_match.to_s), string_match.post_match]
20
+ end
21
+
22
+ def round number, precision = 0
23
+ number.round precision
24
+ end
25
+
26
+ def to_char_list number, options = nil
27
+ # TODO
28
+ end
29
+
30
+ def to_string float, options = nil
31
+ # TODO
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,31 @@
1
+ module Elixir
2
+ module Integer
3
+ module_function
4
+
5
+ def is_even integer
6
+ integer.even?
7
+ end
8
+
9
+ def is_odd integer
10
+ integer.odd?
11
+ end
12
+
13
+ def parse string
14
+ return :error unless string =~ /\A\d/
15
+
16
+ if remainder_index = string =~ /\D/
17
+ [Integer(string[0...remainder_index]), string[remainder_index..-1]]
18
+ else
19
+ [Integer(string), '']
20
+ end
21
+ end
22
+
23
+ def to_char_list integer, base = 10
24
+ integer.to_s(base).upcase.chars
25
+ end
26
+
27
+ def to_string integer, base = 10
28
+ integer.to_s(base).upcase
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,121 @@
1
+ module Elixir
2
+ module List
3
+ module_function
4
+
5
+ def delete array, item
6
+ array.delete item
7
+
8
+ array
9
+ end
10
+
11
+ def delete_at array, index
12
+ array.delete_at index
13
+
14
+ array
15
+ end
16
+
17
+ def duplicate elem, n
18
+ Array.new n, elem
19
+ end
20
+
21
+ def first array
22
+ array.first
23
+ end
24
+
25
+ def flatten array, tail = []
26
+ array.flatten + tail
27
+ end
28
+
29
+ def foldl array, acc, &fun
30
+ array.reduce acc do |accumulator, item|
31
+ fun.call item, accumulator
32
+ end
33
+ end
34
+
35
+ def foldr array, acc, &fun
36
+ array.reverse_each.reduce acc do |accumulator, item|
37
+ fun.call item, accumulator
38
+ end
39
+ end
40
+
41
+ def insert_at array, index, value
42
+ array.insert index, value
43
+ end
44
+
45
+ def keydelete array, key, position
46
+ # TODO
47
+ end
48
+
49
+ def keyfind array, key, position, default = nil
50
+ # TODO
51
+ end
52
+
53
+ def keymember? array, key, position
54
+ # TODO
55
+ end
56
+
57
+ def keyreplace array, key, position, new_array
58
+ # TODO
59
+ end
60
+
61
+ def keysort array, position
62
+ # TODO
63
+ end
64
+
65
+ def last array
66
+ array.last
67
+ end
68
+
69
+ def replace_at array, index, value
70
+ # TODO
71
+ end
72
+
73
+ def to_atom char_array
74
+ # TODO
75
+ end
76
+
77
+ def to_existing_atom char_array
78
+ # TODO
79
+ end
80
+
81
+ def to_float char_array
82
+ # TODO
83
+ end
84
+
85
+ def to_integer char_array
86
+ # TODO
87
+ end
88
+
89
+ def to_string array
90
+ array.flatten.map do |elem|
91
+ case elem
92
+ when ::Integer
93
+ elem.chr
94
+ when ::String
95
+ elem
96
+ else
97
+ raise ArgumentError
98
+ end
99
+ end.join
100
+ end
101
+
102
+ def to_tuple array
103
+ array
104
+ end
105
+
106
+ def update_at array, index, &fun
107
+ # TODO
108
+ end
109
+
110
+ def wrap obj
111
+ obj.respond_to?(:to_a) ? obj.to_a : [obj]
112
+ end
113
+
114
+ def zip array_of_arrays
115
+ array_of_arrays.transpose
116
+ rescue IndexError
117
+ min_size = array_of_arrays.min_by(&:size).size
118
+ array_of_arrays.map { |array| array.first min_size }.transpose
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,91 @@
1
+ require 'shellwords'
2
+ require 'strscan'
3
+
4
+ module Elixir
5
+ module OptionParser
6
+ module_function
7
+
8
+ def next argv, switches = []
9
+ # TODO: switches
10
+ argv = argv.dup
11
+
12
+ option = argv.shift
13
+ return [:error, argv] unless option
14
+
15
+ if option.start_with? '-'
16
+ key = option.sub(/\-{,2}/, '').gsub(/\-/, '_').to_sym
17
+
18
+ if argv.empty? || argv.first.start_with?('-')
19
+ value = true
20
+ else
21
+ value = argv.shift
22
+ end
23
+
24
+ [:ok, key, value, argv]
25
+ else
26
+ [:error, argv.unshift(option)]
27
+ end
28
+ end
29
+
30
+ def parse argv, switches = []
31
+ # TODO: switches
32
+ parsed = {}
33
+ argv = argv.dup
34
+ extras = []
35
+ errors = {}
36
+
37
+ loop do
38
+ option_tuple = self.next argv
39
+
40
+ case option_tuple.first
41
+ when :ok
42
+ _, key, value, argv = option_tuple
43
+
44
+ parsed[key] = value
45
+ when :error
46
+ _, string = option_tuple
47
+ argv.shift
48
+ extra = string.shift
49
+
50
+ extras << extra if extra
51
+ end
52
+
53
+ break [parsed, extras, errors] if option_tuple.last.empty?
54
+ end
55
+ end
56
+
57
+ def parse_head argv, switches = []
58
+ # TODO: switches
59
+ parsed = {}
60
+ argv = argv.dup
61
+ errors = {}
62
+
63
+ loop do
64
+ option_tuple = self.next argv
65
+
66
+ case option_tuple.first
67
+ when :ok
68
+ _, key, value, argv = option_tuple
69
+
70
+ parsed[key] = value
71
+ when :error
72
+ _, argv = option_tuple
73
+
74
+ break [parsed, argv, errors]
75
+ end
76
+ end
77
+ end
78
+
79
+ def split string
80
+ Shellwords.shellsplit string
81
+ end
82
+
83
+ def to_argv enum
84
+ enum.flat_map do |k, v|
85
+ return unless v
86
+
87
+ v == true ? "--#{k}" : ["--#{k}", v]
88
+ end.compact
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,29 @@
1
+ module Elixir
2
+ module Path
3
+ module_function
4
+
5
+ def absname path
6
+ ::File.expand_path path
7
+ end
8
+
9
+ def basename path
10
+ base = ::File.basename path
11
+
12
+ base == '/' ? '' : base
13
+ end
14
+
15
+ def dirname path
16
+ ::File.dirname path
17
+ end
18
+
19
+ def expand path
20
+ ::File.expand_path path
21
+ end
22
+
23
+ def extname path
24
+ ::File.extname path
25
+ end
26
+
27
+ # TODO: The other Path module functions.
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ module Elixir
2
+ module Range
3
+ module_function
4
+
5
+ def new first, last
6
+ first..last
7
+ end
8
+
9
+ def range? range
10
+ range.instance_of? ::Range
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,49 @@
1
+ module Elixir
2
+ module Set
3
+ module_function
4
+
5
+ def delete set, value
6
+ set.delete value
7
+ end
8
+
9
+ def difference set1, set2
10
+ set1.difference set2
11
+ end
12
+
13
+ def disjoint? set1, set2
14
+ set1.disjoint? set2
15
+ end
16
+
17
+ def equal? set1, set2
18
+ set1 === set2
19
+ end
20
+
21
+ def intersection set1, set2
22
+ set1.intersection set2
23
+ end
24
+
25
+ def member? set, value
26
+ set.member? value
27
+ end
28
+
29
+ def put set, value
30
+ set << value
31
+ end
32
+
33
+ def size set
34
+ set.size
35
+ end
36
+
37
+ def subset? set1, set2
38
+ set1.subset? set2
39
+ end
40
+
41
+ def to_list set
42
+ set.to_a
43
+ end
44
+
45
+ def union set1, set2
46
+ set1.union set2
47
+ end
48
+ end
49
+ end
@@ -1,5 +1,7 @@
1
1
  module Elixir
2
2
  module Stream
3
+ INFINITY = ::Float::INFINITY
4
+
3
5
  module_function
4
6
 
5
7
  def cycle collection
@@ -7,8 +9,8 @@ module Elixir
7
9
  end
8
10
 
9
11
  def interval milliseconds
10
- Enumerator.new Float::INFINITY do |yielder|
11
- 0.upto Float::INFINITY do |n|
12
+ Enumerator.new INFINITY do |yielder|
13
+ 0.upto INFINITY do |n|
12
14
  sleep milliseconds.fdiv 1000
13
15
  yielder << n
14
16
  end
@@ -16,7 +18,7 @@ module Elixir
16
18
  end
17
19
 
18
20
  def iterate value
19
- Enumerator.new Float::INFINITY do |yielder|
21
+ Enumerator.new INFINITY do |yielder|
20
22
  loop do
21
23
  yielder << value
22
24
  value = yield value
@@ -25,7 +27,7 @@ module Elixir
25
27
  end
26
28
 
27
29
  def repeatedly
28
- Enumerator.new Float::INFINITY do |yielder|
30
+ Enumerator.new INFINITY do |yielder|
29
31
  loop do
30
32
  yielder << yield
31
33
  end
@@ -40,12 +42,14 @@ module Elixir
40
42
  end
41
43
 
42
44
  def unfold tuple
43
- Enumerator.new Float::INFINITY do |yielder|
45
+ Enumerator.new INFINITY do |yielder|
44
46
  loop do
45
47
  value, tuple = yield tuple
46
48
  yielder << value
47
49
  end
48
50
  end
49
51
  end
52
+
53
+ # TODO: The other Stream module functions.
50
54
  end
51
55
  end
@@ -0,0 +1,154 @@
1
+ module Elixir
2
+ module String
3
+ module_function
4
+
5
+ def at string, index
6
+ string[index]
7
+ end
8
+
9
+ def capitalize string
10
+ string.capitalize
11
+ end
12
+
13
+ def chunk string, trait
14
+ # TODO
15
+ end
16
+
17
+ def codepoints string
18
+ string.chars
19
+ end
20
+
21
+ def contains? string, contents
22
+ warn '[deprecation] Calling String.contains?/2 with an empty string is deprecated and will fail in the future' if contents.empty?
23
+
24
+ string.include? contents
25
+ end
26
+
27
+ def downcase string
28
+ string.downcase
29
+ end
30
+
31
+ def duplicate string, n
32
+ string * n
33
+ end
34
+
35
+ def ends_with? string, *suffixes
36
+ warn '[deprecation] Calling String.ends_with?/2 with an empty string is deprecated and will fail in the future' if string.empty?
37
+
38
+ string.end_with? *suffixes.flatten(1)
39
+ end
40
+
41
+ def first string
42
+ string[0]
43
+ end
44
+
45
+ def graphemes string
46
+ string.chars
47
+ end
48
+
49
+ def last string
50
+ string[-1]
51
+ end
52
+
53
+ def length string
54
+ string.size
55
+ end
56
+
57
+ def ljust string, length, padding = ' '
58
+ string.ljust length, padding
59
+ end
60
+
61
+ def lstrip string, char = :todo
62
+ string.lstrip
63
+ end
64
+
65
+ def match? string, regex
66
+ # TODO
67
+ end
68
+
69
+ def next_codepoint string
70
+ [string[0], string[1..-1]]
71
+ end
72
+
73
+ def next_grapheme string
74
+ [string[0], string[1..-1]]
75
+ end
76
+
77
+ def printable? string
78
+ # TODO
79
+ end
80
+
81
+ def replace string, pattern, replacement, options = []
82
+ # TODO
83
+ end
84
+
85
+ def reverse string
86
+ string.reverse
87
+ end
88
+
89
+ def rjust string, len, padding
90
+ # TODO
91
+ end
92
+
93
+ def rstrip string, char = ' '
94
+ # TODO
95
+ end
96
+
97
+ def slice string, range
98
+ # TODO
99
+ end
100
+
101
+ def split string
102
+ string.split
103
+ end
104
+
105
+ def split_at string, offset
106
+ # TODO
107
+ end
108
+
109
+ def starts_with string, prefix
110
+ string.start_with? prefix
111
+ end
112
+
113
+ def strip string, char = nil
114
+ # TODO
115
+ end
116
+
117
+ def to_atom string
118
+ string.to_sym
119
+ end
120
+
121
+ def to_char_list string
122
+ string.chars
123
+ end
124
+
125
+ def to_existing_atom string
126
+ # Nothing to see here... >.>
127
+ if Symbol.all_symbols.map(&:to_s).include? string
128
+ string.to_sym
129
+ else
130
+ raise ArgumentError
131
+ end
132
+ end
133
+
134
+ def to_float string
135
+ Float(string)
136
+ end
137
+
138
+ def to_integer string, base = 10
139
+ Integer(string)
140
+ end
141
+
142
+ def upcase string
143
+ string.upcase
144
+ end
145
+
146
+ def valid? string
147
+ string.valid_encoding?
148
+ end
149
+
150
+ def valid_character? char
151
+ char.size == 1 && char.valid_encoding?
152
+ end
153
+ end
154
+ end