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 +4 -4
- data/.rubocop.yml +3 -0
- data/Gemfile.lock +1 -1
- data/README.md +42 -4
- data/lib/daitai.rb +2 -0
- data/lib/daitai/abs.rb +1 -1
- data/lib/daitai/compose.rb +2 -18
- data/lib/daitai/pipe.rb +2 -18
- data/lib/daitai/sort.rb +1 -3
- data/lib/daitai/sort_by.rb +23 -0
- data/lib/daitai/sort_with.rb +11 -0
- data/lib/daitai/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '09d2dfb7f123eb7f0213eaf438aae0f88336afafd58416a66790751fb241978d'
|
4
|
+
data.tar.gz: 4d19c1d0ff42be462e63eba58c503f13d247bc3176b1514d54eed3a6b2512542
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2dab3b9141d8c25e5bcf8fe97d3c3e62d9fa614c6774776ac98d21780f2c031701df1fb379804c3ff9c466e0a354a1e3c4bf7b2f2ef9760dd50ed90c2e33ab00
|
7
|
+
data.tar.gz: ad94909fa77456c515db956c74e99a74958cbd7db2b97c9b96df3ef96adf4f6033a55459f4d4a3ff4b31411331df14dfd1a62a98a458245f5b11d9efd649d562
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
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
|
-
```
|
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 ::
|
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.
|
data/lib/daitai.rb
CHANGED
data/lib/daitai/abs.rb
CHANGED
data/lib/daitai/compose.rb
CHANGED
@@ -3,25 +3,9 @@
|
|
3
3
|
module Daitai
|
4
4
|
module Compose
|
5
5
|
def compose
|
6
|
-
lambda do
|
7
|
-
|
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
|
data/lib/daitai/pipe.rb
CHANGED
@@ -3,25 +3,9 @@
|
|
3
3
|
module Daitai
|
4
4
|
module Pipe
|
5
5
|
def pipe
|
6
|
-
lambda do
|
7
|
-
|
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
|
data/lib/daitai/sort.rb
CHANGED
@@ -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
|
data/lib/daitai/version.rb
CHANGED
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.
|
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-
|
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
|