functo 0.1.4 → 0.1.5

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
  SHA1:
3
- metadata.gz: 1aacfe82c8c6c9ae6d012f3d8e6fdf0a76e95624
4
- data.tar.gz: b214a8b047a2e3dfcfa825f407a59c85f8815706
3
+ metadata.gz: 4645a4ef7a0da1efc4fa29037062e0b3e718fa98
4
+ data.tar.gz: 7224895cc9ec9038f7bd043ff0f587eca401e57b
5
5
  SHA512:
6
- metadata.gz: f913c2170fe113976ee62b1e3c4c3d89a6b9293db56c8612073b7e3f8d490bc859465ff11e8362e6380cd5ad14798ae06679211c6ffe65c5f418600cb85bd611
7
- data.tar.gz: 6ef2cb2b623d76ce701fdbaeff494fb0966aced469551f4cd5bf0c4bd98c08fa4a12efb4c3c0085bdaefd5b6209f264a909206fdd7088dda76c16af92b1fa2bb
6
+ metadata.gz: 97498aa8f11632c9537bc2e775cbd7f3a5e9c408fc7bf2409483d7c7156e7f6e0e74ce5f02f1596f0db6dd03a62db6ff74a0f3406f80cc346286658d2ea84126
7
+ data.tar.gz: 58f1a5f18d234fd1b81da2b39839798131c954b47061ca3098affd22c30c2420fb6bfac9630c3088def547895cb483165731317b9f51215b0ac1bdb33e7e0803
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  /README.html
11
+ /TODO.txt
data/README.md CHANGED
@@ -13,7 +13,7 @@ class AddsOne
13
13
  end
14
14
  end
15
15
 
16
- AddsOne[1]
16
+ AddsOne[1] # `.call` or `.()` also work
17
17
  # => 2
18
18
 
19
19
  class Multiplies
@@ -35,6 +35,13 @@ Functo objects can be used in place of a `Proc`.
35
35
  # => [2, 3, 4]
36
36
  ```
37
37
 
38
+ Use `slurp` to splat array inputs.
39
+
40
+ ```ruby
41
+ [[1, 2], [3, 4], [5, 6]].map(&Multiplies.slurp)
42
+ # => [2, 12, 30]
43
+ ```
44
+
38
45
  ### Composition
39
46
 
40
47
  ```ruby
@@ -72,7 +79,7 @@ SumsDigits2[1066]
72
79
  # => 13
73
80
  ```
74
81
 
75
- Any object that responds to `call` can be made composable.
82
+ Any object that responds to `to_proc` can be made composable.
76
83
 
77
84
  ```ruby
78
85
  SquareRoots = Functo.wrap ->(n) { Math.sqrt(n) }
@@ -80,6 +87,10 @@ SquareRoots = Functo.wrap ->(n) { Math.sqrt(n) }
80
87
  SquareRootsAddsOne = SquareRoots >> AddsOne
81
88
  SquareRootsAddsOne[16]
82
89
  # => 5.0
90
+
91
+ AddsOneStringifies = AddsOne >> Functo.wrap(&:to_s)
92
+ AddsOneStringifies[3]
93
+ # => "4"
83
94
  ```
84
95
 
85
96
  ### Filters
@@ -95,9 +106,20 @@ end
95
106
 
96
107
  DividesTwo['4']
97
108
  # => 0.5
109
+
110
+ class DividesThree
111
+ include Functo.call :divide, number: :to_f
112
+
113
+ def divide
114
+ 3 / number
115
+ end
116
+ end
117
+
118
+ DividesThree['4']
119
+ # => 0.75
98
120
  ```
99
121
 
100
- A filter can be any object that responds to `call` or `[]`.
122
+ A filter can be any object that responds to `to_proc`, `call`, or `[]`.
101
123
 
102
124
  For example using [dry-types](https://github.com/dry-rb/dry-types).
103
125
 
data/lib/functo.rb CHANGED
@@ -8,19 +8,15 @@ class Functo < Module
8
8
  class << self
9
9
  private :new
10
10
 
11
- def define_method_object(&block)
11
+ def wrap(function = nil, &block)
12
+ function ||= block if block_given?
13
+
12
14
  Class.new.tap do |klass|
13
- klass.define_singleton_method(:call, &block)
15
+ klass.define_singleton_method(:call, &function)
14
16
  klass.extend(Functo::Compose)
15
17
  end
16
18
  end
17
19
 
18
- def wrap(obj)
19
- define_method_object do |*args|
20
- obj.call(*args)
21
- end
22
- end
23
-
24
20
  def pass
25
21
  PASS
26
22
  end
@@ -121,12 +117,14 @@ class Functo < Module
121
117
  args.zip(@filters).map do |arg, filter|
122
118
  if filter.equal?(Functo.pass)
123
119
  arg
124
- elsif filter.respond_to?(:[])
125
- filter[arg]
120
+ elsif filter.respond_to?(:to_proc)
121
+ filter.to_proc.call(arg)
126
122
  elsif filter.respond_to?(:call)
127
123
  filter.call(arg)
124
+ elsif filter.respond_to?(:[])
125
+ filter[arg]
128
126
  else
129
- raise ArgumentError.new("filters must respond to `[]` or `call`")
127
+ raise ArgumentError.new("filters must respond to `to_proc`, `call`, or `[]`")
130
128
  end
131
129
  end
132
130
  end
@@ -10,7 +10,7 @@ module Functo::Compose
10
10
  def compose(outer, splat: false)
11
11
  inner = self
12
12
 
13
- Functo.define_method_object do |*args|
13
+ Functo.wrap do |*args|
14
14
  if splat
15
15
  outer.call(*inner.call(*args))
16
16
  else
@@ -26,4 +26,12 @@ module Functo::Compose
26
26
  def >>(outer)
27
27
  compose(outer, splat: true)
28
28
  end
29
+
30
+ def slurp
31
+ inner = self
32
+
33
+ Functo.wrap do |arr|
34
+ inner.call(*arr)
35
+ end
36
+ end
29
37
  end
@@ -1,3 +1,3 @@
1
1
  class Functo < Module
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: functo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Scully
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-16 00:00:00.000000000 Z
11
+ date: 2017-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler