rudash 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: afd9b119a04862ff0b8854acae76dda92540d506deaee13b84ad3689294bfa2d
4
- data.tar.gz: d779e6b89af80cf98e9617de7b2ef3253b3ecb897443d0e858398fc277022981
3
+ metadata.gz: 064bfa431a92255617323d2a88029201fd4e88e38d73de7e6f5ad6c701edc47b
4
+ data.tar.gz: f7b8aec8cd3c9c036bcf5a4466e5655b25f7e242f6b173f48bb036960bebff03
5
5
  SHA512:
6
- metadata.gz: a2bda67eb2dbfc8c70ed26bd43026948df9ffcec32d4dbb1a797190554ee64bc72afb6d631658693d28415b71e97d547764dedbab31b9e6c424b3917accaca4a
7
- data.tar.gz: d847cdb6c9b43bae6ddfe9ffd1aaeb1d88e4ec7ca1e241499b530c5ce3aba1f0c98c4427674e4c41b8dae0323b214b7f652605eb95741abdc2b805c5e59eab0a
6
+ metadata.gz: 076d48427362914d7342e53951a33efc05dbada37b3831a3a58248f35a946462c1682abcd0215c0f93c3ca4ffbfa357a459014f5b12a3faf64aea7bd58534608
7
+ data.tar.gz: 164842487b1cb948775efaff8ab87bea1463010f86ad2a7d99d727184f0532ac391c5ac2f8bb8c04737636a1825fc3ec9eedba4227638d8c7c380cff50316341
@@ -0,0 +1,12 @@
1
+ require_relative 'is_nil.rb'
2
+
3
+ module Compact
4
+ extend IsNil
5
+
6
+ def compact
7
+ compact_proc = -> (array) {
8
+ return [] if !array.is_a?(Array)
9
+ array.compact
10
+ }
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ require_relative 'is_nil.rb'
2
+
3
+ module Concat
4
+ extend IsNil
5
+
6
+ def concat
7
+ concat_proc = -> (head, *values) {
8
+ head_arr = head.is_a?(Array) ? head : [head]
9
+ if values.size == 0
10
+ return head_arr
11
+ else
12
+ return head_arr + self.concat[*values]
13
+ end
14
+ }
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ module Curry
2
+ def curry
3
+ curry_proc = -> (a_proc) {
4
+ case a_proc
5
+ when Proc
6
+ a_proc.curry
7
+ else
8
+ raise 'Expected a Proc'
9
+ end
10
+ a_proc.curry
11
+ }
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ require_relative 'reduce.rb'
2
+
3
+ module Difference
4
+ extend Reduce
5
+
6
+ def difference
7
+ difference_proc = -> (*values) {
8
+ diff_reducer = -> (acc, current) {
9
+ return [] if !acc.is_a?(Array)
10
+ return acc if !current.is_a?(Array)
11
+
12
+ acc - current
13
+ }
14
+
15
+ self.reduce[values, diff_reducer]
16
+ }
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ require_relative 'map.rb'
2
+
3
+ module Each
4
+ extend Map
5
+
6
+ def each
7
+ each_proc = -> (collection, *rest_args) {
8
+ self.map[collection, *rest_args]
9
+ collection
10
+ }
11
+ end
12
+
13
+ def for_each
14
+ self.each
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ require_relative 'filter.rb'
2
+
3
+ module Every
4
+ extend Filter
5
+
6
+ def every?
7
+ every_proc = -> (array, filter) {
8
+ filtered_arr = self.filter[array, filter]
9
+
10
+ filtered_arr.length == array.length
11
+ }
12
+ end
13
+ end
@@ -0,0 +1,48 @@
1
+ require_relative 'is_nil.rb'
2
+ require_relative 'identity.rb'
3
+ require_relative 'head.rb'
4
+
5
+ module Filter
6
+ extend IsNil
7
+ extend Identity
8
+ extend Head
9
+
10
+ def filter
11
+ filter_proc = -> (collection, *rest_args) {
12
+ filter = self.head[rest_args] || self.identity
13
+
14
+ if collection.is_a?(Array) and filter.is_a?(Hash)
15
+ filtered_arr = []
16
+ collection.each do |v|
17
+ match = true
18
+ filter.each do |key, value|
19
+ if (value != v[key])
20
+ match = false
21
+ break
22
+ end
23
+ end
24
+
25
+ if (match == true)
26
+ filtered_arr << v
27
+ end
28
+ end
29
+
30
+ return filtered_arr
31
+ elsif collection.is_a?(Array)
32
+ if filter.arity == 1
33
+ return collection.select { |x| filter[x] }
34
+ else
35
+ return collection.select.with_index { |x, idx| filter[x, idx] }
36
+ end
37
+ elsif collection.is_a?(Hash)
38
+ if filter.arity == 1
39
+ return collection.select { |k, v| filter[v] }
40
+ else
41
+ return collection.select { |k, v| filter[v, k] }
42
+ end
43
+ else
44
+ return []
45
+ end
46
+ }
47
+ end
48
+ end
@@ -0,0 +1,18 @@
1
+ require_relative 'filter.rb'
2
+ require_relative 'head.rb'
3
+ require_relative 'identity.rb'
4
+
5
+ module Find
6
+ extend Filter
7
+ extend Head
8
+ extend Identity
9
+
10
+ def find
11
+ find_proc = -> (collection, *rest_args) {
12
+ filter_proc = self.head[rest_args] || self.identity
13
+ filtered_arr = self.filter[collection, filter_proc]
14
+
15
+ filtered_arr[0]
16
+ }
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ require_relative 'filter.rb'
2
+
3
+ module FindLast
4
+ extend Filter
5
+
6
+ def find_last
7
+ find_last_proc = -> (collection, *rest_args) {
8
+ filter_proc = self.head[rest_args] || self.identity
9
+ filtered_arr = self.filter[collection, filter_proc]
10
+
11
+ filtered_arr[filtered_arr.length - 1]
12
+ }
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ module Flip
2
+ def flip
3
+ flip_proc = -> (a_proc) {
4
+ flipped_proc = -> (*args) {
5
+ reveresed_args = args.reverse
6
+
7
+ a_proc[*reveresed_args]
8
+ }
9
+ }
10
+ end
11
+ end
data/lib/rudash/get.rb ADDED
@@ -0,0 +1,42 @@
1
+ require_relative 'reduce.rb'
2
+
3
+ module Get
4
+ extend Reduce
5
+
6
+ def get
7
+ get_proc = -> (hash, path, *rest_args) {
8
+ return nil if !path.is_a?(String) and !path.is_a?(Array)
9
+ return nil if !hash.is_a?(Array) and !hash.is_a?(Hash)
10
+
11
+ transform_path = -> (path) {
12
+ normalized_path = path
13
+
14
+ if normalized_path.is_a?(Array)
15
+ normalized_path = normalized_path.join('.')
16
+ end
17
+
18
+ normalized_path = normalized_path.gsub /[\[\]]/, "."
19
+ normalized_path = normalized_path.gsub /\.\./, "."
20
+ splitted_hash = normalized_path.split('.')
21
+ }
22
+
23
+ splitted_hash = transform_path[path]
24
+
25
+ get_reducer = -> (acc, current) {
26
+ return nil if acc.nil?
27
+
28
+ if acc.is_a?(Array) and current.match(/^(\d)+$/)
29
+ acc[current.to_i]
30
+ elsif acc.is_a?(Array) and !current.match(/^(\d)+$/)
31
+ nil
32
+ elsif acc.is_a?(Hash)
33
+ acc[current.to_sym]
34
+ else
35
+ nil
36
+ end
37
+ }
38
+
39
+ self.reduce[splitted_hash, get_reducer, hash]
40
+ }
41
+ end
42
+ end
@@ -0,0 +1,12 @@
1
+ module Head
2
+ def head
3
+ head_proc = -> (array) {
4
+ return nil if !array.is_a?(Array)
5
+ array.first
6
+ }
7
+ end
8
+
9
+ def first
10
+ self.head
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module Identity
2
+ def identity
3
+ identity_proc = -> (first_arg, *rest_args) { first_arg }
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module IsArray
2
+ def is_array?
3
+ is_array_proc = -> (value) { value.is_a?(Array) }
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ module IsEmpty
2
+ def is_empty?
3
+ is_empty_proc = -> (value) {
4
+ case value
5
+ when Hash, Array
6
+ value.empty?
7
+ else
8
+ true
9
+ end
10
+ }
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module IsEqual
2
+ def is_equal?
3
+ is_equal_proc = -> (value, other) { value == other }
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module IsHash
2
+ def is_hash?
3
+ is_hash_proc = -> (value) { value.is_a?(Hash) }
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module IsNil
2
+ def is_nil?
3
+ is_nil_proc = -> (object) { object.nil? }
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module IsNumber
2
+ def is_number?
3
+ is_number_proc = -> (value) { value.is_a?(Numeric) }
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module IsProc
2
+ def is_proc?
3
+ is_proc_proc = -> (value) { value.is_a?(Proc) }
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ module Keys
2
+ def keys
3
+ keys_proc = -> (value) {
4
+ case value
5
+ when Hash
6
+ value.map { |key, value| "#{key}" }
7
+ when Array
8
+ value.map.with_index { |value, index| "#{index}" }
9
+ else
10
+ []
11
+ end
12
+ }
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ module Last
2
+ def last
3
+ last_proc = -> (array) {
4
+ return nil if !array.is_a?(Array)
5
+ array.last
6
+ }
7
+ end
8
+ end
data/lib/rudash/map.rb ADDED
@@ -0,0 +1,22 @@
1
+ module Map
2
+ def map
3
+ map_proc = -> (collection, *rest_args) {
4
+ mapper_proc = self.head[rest_args] || self.identity
5
+
6
+ col = collection.is_a?(String) ? collection.split('') : collection
7
+
8
+ if col.is_a?(Array)
9
+ return mapper_proc.arity == 1 ?
10
+ col.map { |value| mapper_proc[value] } :
11
+ col.map.with_index { |value, index| mapper_proc[value, index] }
12
+
13
+ elsif col.is_a?(Hash)
14
+ return mapper_proc.arity == 1 ?
15
+ col.map { |k,v| mapper_proc[v] } :
16
+ col.map { |k,v| mapper_proc[v, k] }
17
+ else
18
+ return []
19
+ end
20
+ }
21
+ end
22
+ end
@@ -0,0 +1,35 @@
1
+ module Reduce
2
+ def reduce
3
+ reduce_proc = -> (collection, *rest_args) {
4
+ reducer = rest_args[0]
5
+ initial_state = rest_args[1]
6
+
7
+ col = collection.is_a?(String) ? collection.split('') : collection
8
+
9
+ case rest_args.size
10
+ when 1
11
+ return col.reduce { |acc, current|
12
+ if col.is_a?(Hash)
13
+ reducer.arity == 2 ?
14
+ reducer[acc, current[1]] :
15
+ reducer[acc, current[1], current[0]]
16
+ else
17
+ reducer[acc, current]
18
+ end
19
+ }
20
+ when 2
21
+ return col.reduce(initial_state) { |acc, current|
22
+ if col.is_a?(Hash)
23
+ reducer.arity == 2 ?
24
+ reducer[acc, current[1]] :
25
+ reducer[acc, current[1], current[0]]
26
+ else
27
+ reducer[acc, current]
28
+ end
29
+ }
30
+ else
31
+ return nil
32
+ end
33
+ }
34
+ end
35
+ end
@@ -0,0 +1,19 @@
1
+ require_relative 'reduce.rb'
2
+
3
+ module ReduceRight
4
+ extend Reduce
5
+
6
+ def reduce_right
7
+ reduce_right_proc = -> (collection, *rest_args) {
8
+ reversed_collection =
9
+ case collection
10
+ when Array then collection.reverse
11
+ when Hash then collection.reverse_each.to_h
12
+ when String then collection.reverse
13
+ else []
14
+ end
15
+
16
+ self.reduce[reversed_collection, *rest_args]
17
+ }
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ module Reverse
2
+ def reverse
3
+ reverse_proc = -> (value) {
4
+ case value
5
+ when Array, String
6
+ value.reverse
7
+ else
8
+ value
9
+ end
10
+ }
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ require_relative 'is_nil.rb'
2
+
3
+ module Size
4
+ extend IsNil
5
+
6
+ def size
7
+ size_proc = -> (something) {
8
+ return 0 if self.is_nil?[something]
9
+
10
+ something.size
11
+ }
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ require_relative 'filter.rb'
2
+
3
+ module Some
4
+ extend Filter
5
+
6
+ def some?
7
+ some_proc = -> (array, filter) {
8
+ filtered_arr = self.filter[array, filter]
9
+ filtered_arr.length != 0
10
+ }
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ module Tail
2
+ def tail
3
+ tail_proc = -> (array) {
4
+ return [] if !array.is_a?(Array)
5
+ array[1..-1] || []
6
+ }
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ module Uniq
2
+ def uniq
3
+ uniq_proc = -> (value) {
4
+ case value
5
+ when String
6
+ uniq_proc[value.split('')]
7
+ when Array
8
+ value.uniq
9
+ else
10
+ []
11
+ end
12
+ }
13
+ end
14
+ end
data/lib/rudash.rb CHANGED
@@ -1,6 +1,63 @@
1
- $LOAD_PATH << '.'
2
- require 'map.rb'
1
+ require_relative './rudash/map.rb'
2
+ require_relative './rudash/is_nil.rb'
3
+ require_relative './rudash/filter.rb'
4
+ require_relative './rudash/some.rb'
5
+ require_relative './rudash/every.rb'
6
+ require_relative './rudash/find.rb'
7
+ require_relative './rudash/concat.rb'
8
+ require_relative './rudash/find_last.rb'
9
+ require_relative './rudash/reduce.rb'
10
+ require_relative './rudash/reduce_right.rb'
11
+ require_relative './rudash/compact.rb'
12
+ require_relative './rudash/head.rb'
13
+ require_relative './rudash/last.rb'
14
+ require_relative './rudash/tail.rb'
15
+ require_relative './rudash/size.rb'
16
+ require_relative './rudash/flip.rb'
17
+ require_relative './rudash/identity.rb'
18
+ require_relative './rudash/get.rb'
19
+ require_relative './rudash/is_array.rb'
20
+ require_relative './rudash/is_equal.rb'
21
+ require_relative './rudash/is_hash.rb'
22
+ require_relative './rudash/is_empty.rb'
23
+ require_relative './rudash/is_proc.rb'
24
+ require_relative './rudash/reverse.rb'
25
+ require_relative './rudash/curry.rb'
26
+ require_relative './rudash/is_number.rb'
27
+ require_relative './rudash/keys.rb'
28
+ require_relative './rudash/each.rb'
29
+ require_relative './rudash/uniq.rb'
30
+ require_relative './rudash/difference.rb'
3
31
 
4
- class Rudash
32
+ class R_
5
33
  extend Map
34
+ extend IsNil
35
+ extend Filter
36
+ extend Some
37
+ extend Every
38
+ extend Find
39
+ extend Concat
40
+ extend FindLast
41
+ extend Reduce
42
+ extend ReduceRight
43
+ extend Compact
44
+ extend Head
45
+ extend Last
46
+ extend Tail
47
+ extend Size
48
+ extend Flip
49
+ extend Identity
50
+ extend Get
51
+ extend IsArray
52
+ extend IsEqual
53
+ extend IsHash
54
+ extend IsEmpty
55
+ extend IsProc
56
+ extend Reverse
57
+ extend Curry
58
+ extend IsNumber
59
+ extend Keys
60
+ extend Each
61
+ extend Uniq
62
+ extend Difference
6
63
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rudash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Islam Attrash
@@ -16,11 +16,41 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
- - lib/map.rb
20
19
  - lib/rudash.rb
21
- homepage:
20
+ - lib/rudash/compact.rb
21
+ - lib/rudash/concat.rb
22
+ - lib/rudash/curry.rb
23
+ - lib/rudash/difference.rb
24
+ - lib/rudash/each.rb
25
+ - lib/rudash/every.rb
26
+ - lib/rudash/filter.rb
27
+ - lib/rudash/find.rb
28
+ - lib/rudash/find_last.rb
29
+ - lib/rudash/flip.rb
30
+ - lib/rudash/get.rb
31
+ - lib/rudash/head.rb
32
+ - lib/rudash/identity.rb
33
+ - lib/rudash/is_array.rb
34
+ - lib/rudash/is_empty.rb
35
+ - lib/rudash/is_equal.rb
36
+ - lib/rudash/is_hash.rb
37
+ - lib/rudash/is_nil.rb
38
+ - lib/rudash/is_number.rb
39
+ - lib/rudash/is_proc.rb
40
+ - lib/rudash/keys.rb
41
+ - lib/rudash/last.rb
42
+ - lib/rudash/map.rb
43
+ - lib/rudash/reduce.rb
44
+ - lib/rudash/reduce_right.rb
45
+ - lib/rudash/reverse.rb
46
+ - lib/rudash/size.rb
47
+ - lib/rudash/some.rb
48
+ - lib/rudash/tail.rb
49
+ - lib/rudash/uniq.rb
50
+ homepage: https://github.com/Attrash-Islam/rudash
22
51
  licenses: []
23
- metadata: {}
52
+ metadata:
53
+ source_code_uri: https://github.com/Attrash-Islam/rudash
24
54
  post_install_message:
25
55
  rdoc_options: []
26
56
  require_paths:
data/lib/map.rb DELETED
@@ -1,9 +0,0 @@
1
- module Map
2
- def map
3
- mapProc = -> (array, mapperFn) {
4
- array.map { |value| mapperFn[value] }
5
- }
6
-
7
- mapProc
8
- end
9
- end