daitai 0.1.5 → 0.1.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b580a7ada3ae9b6b21a4c8cc3aaa6768cbca06f267081dd2c53e8da0db8b82d0
4
- data.tar.gz: 495f5ed0f52a58a53247fae9d2f41795b915b7f33b4d60a530f203bea681eb82
3
+ metadata.gz: '09d2dfb7f123eb7f0213eaf438aae0f88336afafd58416a66790751fb241978d'
4
+ data.tar.gz: 4d19c1d0ff42be462e63eba58c503f13d247bc3176b1514d54eed3a6b2512542
5
5
  SHA512:
6
- metadata.gz: 5f708096ff66b8c837fe24bc24b0a41b4373ca9bcc85c7280fdec9ffb4b8967a2161bfdddaa3354cea01ff5bcf103a156371b839a396dbc28df7e778f4c0d9ba
7
- data.tar.gz: 3a7836b2adddc9ec27fa54261303996fe3f0999067bc8f6682123ae73983cc901d626c66643c8a369a08a13184a3395e8dfa01b4932daed1ae3b2a1146c8efdd
6
+ metadata.gz: 2dab3b9141d8c25e5bcf8fe97d3c3e62d9fa614c6774776ac98d21780f2c031701df1fb379804c3ff9c466e0a354a1e3c4bf7b2f2ef9760dd50ed90c2e33ab00
7
+ data.tar.gz: ad94909fa77456c515db956c74e99a74958cbd7db2b97c9b96df3ef96adf4f6033a55459f4d4a3ff4b31411331df14dfd1a62a98a458245f5b11d9efd649d562
@@ -13,3 +13,6 @@ Metrics/LineLength:
13
13
  Metrics/BlockLength:
14
14
  Exclude:
15
15
  - 'spec/**/*'
16
+
17
+ AllCops:
18
+ TargetRubyVersion: 2.5
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- daitai (0.1.5)
4
+ daitai (0.1.6)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -14,19 +14,19 @@ Daitai (代替, Japanese for "alternative") is a functional library for Ruby lan
14
14
 
15
15
  Add this line to your application's Gemfile:
16
16
 
17
- ```ruby
17
+ ```bash
18
18
  gem 'daitai'
19
19
  ```
20
20
 
21
21
  And then execute:
22
22
 
23
- ```
23
+ ```bash
24
24
  $ bundle
25
25
  ```
26
26
 
27
27
  Or install it yourself as:
28
28
 
29
- ```
29
+ ```bash
30
30
  $ gem install daitai
31
31
  ```
32
32
 
@@ -58,6 +58,8 @@ $ gem install daitai
58
58
  * [reverse](#reverse-definition)
59
59
  * [signum](#signum-definition)
60
60
  * [sort](#sort-definition)
61
+ * [sort_by](#sort_by-definition)
62
+ * [sort_with](#sort_with-definition)
61
63
  * [subtract](#subtract-definition)
62
64
  * [sum](#sum-definition)
63
65
  * [tail](#tail-definition)
@@ -181,6 +183,7 @@ Returns a list of all elements that satisfy the predicate.
181
183
  ```ruby
182
184
  greater_than_two = ->(x) { x > 2 }
183
185
  Daitai.filter.(greater_than_two, [1, 2, 3, 4]) # => [3, 4]
186
+ Daitai.filter.(greater_than_two, x: 2, y: 3, z: 5) # => { y: 3, z: 5 }
184
187
 
185
188
  only_even = Daitai.filter.(->(x) { x % 2 == 0 })
186
189
  only_even.([1, 2, 3, 4]) # => [2, 4]
@@ -249,6 +252,7 @@ Applies the function to all elements of the list and returns a new list of the r
249
252
  ```ruby
250
253
  triple = ->(x) { x * 3 }
251
254
  Daitai.map.(triple, [1, 2, 3, 4]) # => [3, 6, 9, 12]
255
+ Daitai.map.(triple, a: 10, b: 13) # => { a: 30, b: 39 }
252
256
 
253
257
  increment = Daitai.map.(->(x) { x + 1 })
254
258
  increment.([1, 2, 3, 4]) # => [2, 3, 4, 5]
@@ -392,6 +396,9 @@ Reduces the list using the function, from left to right, using the accumulator.
392
396
  add = ->(x, y) { x + y }
393
397
  Daitai.reduce.(add, 0, [1, 2, 3, 4]) # => 10
394
398
 
399
+ sum = ->(acc, (_, v)) { v + acc }
400
+ Daitai.reduce.(sum, 0, x: 2, y: 3, z: 5) # => 10
401
+
395
402
  concat = Daitai.reduce.(add, "")
396
403
  concat.(%w[l a m b d a]) # => "lambda"
397
404
  ```
@@ -426,7 +433,38 @@ Daitai.signum.(-8) # => -1
426
433
  - - -
427
434
 
428
435
  <h4 id='sort-definition'>
429
- <code>sort :: (a -> a -> Numeric) -> [a] -> [a]</code>
436
+ <code>sort :: [a] -> [a]</code>
437
+ </h4>
438
+
439
+ Returns a copy of the list sorted in the ascending order.
440
+
441
+ ```ruby
442
+ Daitai.sort.(diff, [2, 1, 4, 3]) # => [1, 2, 3, 4]
443
+ Daitai.sort.(%w[haskell ruby elixir]) # => ["elixir", "haskell", "ruby"]
444
+ ```
445
+
446
+ - - -
447
+
448
+ <h4 id='sort_by-definition'>
449
+ <code>sort_by :: a -> [a] -> [a]</code>
450
+ </h4>
451
+
452
+ Returns a copy of the list sorted by the provided property - either a key of a `Hash` or a name of an `Object`'s function.
453
+
454
+ ```ruby
455
+ apple = { colour: 'red', weight: 136 }
456
+ banana = { colour: 'yellow', weight: 118 }
457
+ pear = { colour: 'green', weight: 178 }
458
+ Daitai.sort_by.(:weight, [apple, banana, pear]) # => [banana, apple, pear]
459
+
460
+ sort_by_length = Daitai.sort_by.(:length)
461
+ sort_by_length.(%w[haskell ruby elixir] # => ["ruby", "elixir", "haskell"]
462
+ ```
463
+
464
+ - - -
465
+
466
+ <h4 id='sort_with-definition'>
467
+ <code>sort_with :: (a -> a -> Numeric) -> [a] -> [a]</code>
430
468
  </h4>
431
469
 
432
470
  Returns a sorted copy of the list according to the specified comparator function.
@@ -30,6 +30,8 @@ module Daitai
30
30
  extend Reverse
31
31
  extend Signum
32
32
  extend Sort
33
+ extend SortBy
34
+ extend SortWith
33
35
  extend Subtract
34
36
  extend Sum
35
37
  extend Tail
@@ -3,7 +3,7 @@
3
3
  module Daitai
4
4
  module Abs
5
5
  def abs
6
- ->(x) { x < 0 ? -x : x }
6
+ ->(x) { x < 0 ? -x : x } # rubocop:disable Style/NumericPredicate
7
7
  end
8
8
  end
9
9
  end
@@ -3,25 +3,9 @@
3
3
  module Daitai
4
4
  module Compose
5
5
  def compose
6
- lambda do |*functions|
7
- if functions.length > 2
8
- compose_n(functions)
9
- else
10
- compose_two(functions)
11
- end
6
+ lambda do |f, g|
7
+ ->(*args) { f.(g.(*args)) }
12
8
  end.curry
13
9
  end
14
-
15
- private
16
-
17
- def compose_two(functions)
18
- f, g = functions
19
- ->(*args) { f.(g.(*args)) }
20
- end
21
-
22
- def compose_n(functions)
23
- head, *tail = functions
24
- ->(*args) { head.(compose.(*tail).(*args)) }
25
- end
26
10
  end
27
11
  end
@@ -3,25 +3,9 @@
3
3
  module Daitai
4
4
  module Pipe
5
5
  def pipe
6
- lambda do |*functions|
7
- if functions.length > 2
8
- pipe_n(functions)
9
- else
10
- pipe_two(functions)
11
- end
6
+ lambda do |f, g|
7
+ ->(*args) { g.(f.(*args)) }
12
8
  end.curry
13
9
  end
14
-
15
- private
16
-
17
- def pipe_two(functions)
18
- f, g = functions
19
- ->(*args) { g.(f.(*args)) }
20
- end
21
-
22
- def pipe_n(functions)
23
- head, *tail = functions
24
- ->(*args) { pipe.(*tail).(head.(*args)) }
25
- end
26
10
  end
27
11
  end
@@ -3,9 +3,7 @@
3
3
  module Daitai
4
4
  module Sort
5
5
  def sort
6
- lambda do |comparator, list|
7
- list.sort(&comparator)
8
- end.curry
6
+ ->(sortable) { sortable.sort }
9
7
  end
10
8
  end
11
9
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Daitai
4
+ module SortBy
5
+ def sort_by
6
+ lambda do |property, sortable|
7
+ comparator = sort_elements(property)
8
+ sortable.sort_by(&comparator)
9
+ end.curry
10
+ end
11
+
12
+ private
13
+
14
+ def sort_elements(property)
15
+ lambda do |object|
16
+ case object
17
+ when Hash then object[property]
18
+ else object.send(property)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Daitai
4
+ module SortWith
5
+ def sort_with
6
+ lambda do |comparator, sortable|
7
+ sortable.sort(&comparator)
8
+ end.curry
9
+ end
10
+ end
11
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Daitai
4
- VERSION = '0.1.5'.freeze
4
+ VERSION = '0.1.6'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: daitai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Walerian Sobczak
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-17 00:00:00.000000000 Z
11
+ date: 2018-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -114,6 +114,8 @@ files:
114
114
  - lib/daitai/reverse.rb
115
115
  - lib/daitai/signum.rb
116
116
  - lib/daitai/sort.rb
117
+ - lib/daitai/sort_by.rb
118
+ - lib/daitai/sort_with.rb
117
119
  - lib/daitai/subtract.rb
118
120
  - lib/daitai/sum.rb
119
121
  - lib/daitai/tail.rb