lab42_console 0.2.0 → 0.2.1

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: b6f95877f5a0906e4be07fcae52a0e773c286eeb2103af22bbf0d380127073e6
4
- data.tar.gz: ef58b9bcaa9ba85c03b5fc96b623b35c809aaaccb5bb6eae8aae08b109a0e97e
3
+ metadata.gz: 68f691d24424fdd172440f3dec3b1fa1eb808b80ca23f23f9cf02b7916fcf7fd
4
+ data.tar.gz: 918378b21433520963d40efeb8382357c6aded947e0c1d15f44191cc80a9065c
5
5
  SHA512:
6
- metadata.gz: d9ecc765cbe017dd929d2a61d0572bac1a11013f330a6e5510c46b9818889c4430ba90923b5571964dd68ce6ae8f979e0a795249d7beaadcc8288ccfc2789ed3
7
- data.tar.gz: e2f7369b408fca4207d7f2c2666cd3443b9f465d5be55aba942a39a876acaa58419f5294289ee1343ceb295381057da187b19b629795272ba32c0843c2260703
6
+ metadata.gz: e86355753644bbec516e1c3e337f4b63744174ee33fc9c6356b4e6061c3626620c4c300118b92a36416eab01d1ff6867feb75b5015199060b35653338f175f46
7
+ data.tar.gz: 72313880d880e58040c40624bd590511569a2be483a637103db3546798a90a667d2b80ea638cb19dd648435ee14c867e6f138b2ec705f95adb4cf9382d44f83f
@@ -0,0 +1,19 @@
1
+ module Lab42
2
+ class Console
3
+ class Cons
4
+
5
+ attr_reader :car, :cdr
6
+
7
+ def apply_to_cdr(fun)
8
+ self.class.new(car, fun.(cdr))
9
+ end
10
+
11
+ private
12
+
13
+ def initialize(car, cdr)
14
+ @car = car
15
+ @cdr = cdr
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,24 +1,24 @@
1
- require_relative 'runner'
1
+ require_relative 'cons'
2
+ require_relative 'wrapper'
2
3
  module Lab42
3
4
  class Console
4
5
  module Enum
5
- def by(*args)
6
- Lab42::Console::Runner.new(by: args, method: nil, subject: self)
6
+ def by(*args, &blk)
7
+ Lab42::Console::Wrapper.new(self, *args, &blk)
7
8
  end
8
- def fnd(*args)
9
- if args.size.zero?
10
- Lab42::Console::Runner.new(method: :find, subject: self)
11
- else
12
- Lab42::Console::Runner.new(*args, method: :find, subject: self).run
13
- end
9
+
10
+ def cars
11
+ map(&:car)
14
12
  end
15
- def sel(*args)
16
- if args.size.zero?
17
- Lab42::Console::Runner.new(method: :filter, subject: self)
18
- else
19
- Lab42::Console::Runner.new(*args, method: :filter, subject: self).run
20
- end
13
+
14
+ def cdrs
15
+ map(&:cdr)
21
16
  end
17
+
18
+ def duplicate
19
+ map{ |x| Cons.new(x, x) }
20
+ end
21
+
22
22
  end
23
23
  end
24
24
  end
@@ -0,0 +1,57 @@
1
+ module Lab42
2
+ class Console
3
+ class Function
4
+ Error = Class.new RuntimeError
5
+
6
+ attr_reader :functions, :sources
7
+
8
+ def add(*args, &blk)
9
+ if args.empty?
10
+ _add_blk(blk)
11
+ else
12
+ raise Error, "must not provide literal and block behaviour at the same time" if blk
13
+ _add_args(args)
14
+ end
15
+ self
16
+ end
17
+
18
+ def call(element)
19
+ functions
20
+ .inject(element) do |acc, fn|
21
+ fn.(acc)
22
+ end
23
+ end
24
+
25
+ def to_proc
26
+ -> x { call(x) }
27
+ end
28
+
29
+ private
30
+
31
+ def initialize(*args, &blk)
32
+ @functions = []
33
+ @sources = []
34
+ add(*args, &blk)
35
+ end
36
+
37
+ def _add_args(args)
38
+ functions << _add_args_fn(args)
39
+ end
40
+
41
+ def _add_args_fn(args)
42
+ sources << "send #{args.join(", ")}"
43
+ -> reciever do
44
+ reciever.send(*args)
45
+ rescue
46
+ nil
47
+ end
48
+ end
49
+
50
+ def _add_blk(blk)
51
+ sources << blk.to_s
52
+ functions << blk
53
+ end
54
+
55
+ end
56
+ end
57
+ end
@@ -1,5 +1,5 @@
1
1
  module Lab42
2
2
  class Console
3
- VERSION = '0.2.0'
3
+ VERSION = '0.2.1'
4
4
  end
5
5
  end
@@ -0,0 +1,57 @@
1
+ require_relative 'function'
2
+ module Lab42
3
+ class Console
4
+ class Wrapper
5
+
6
+ attr_reader :function, :subject
7
+
8
+ def add(*args, &blk)
9
+ @function = function.add(*args, &blk)
10
+ self
11
+ end
12
+
13
+
14
+ def fnd(*args, &blk)
15
+ _apply_function
16
+ .find{ |x| x.apply_to_cdr(_make_function(*args, &blk))&.cdr }
17
+ &.car
18
+ end
19
+
20
+ def sel(*args, &blk)
21
+ filter_function = _make_function(*args, &blk)
22
+ _apply_function
23
+ .filter{ |x| x.apply_to_cdr(filter_function)&.cdr }
24
+ .map(&:car)
25
+ end
26
+
27
+
28
+ private
29
+
30
+ def initialize(subject, *args, &blk)
31
+ _init_function(args, blk)
32
+ @subject = subject
33
+ end
34
+
35
+ def _apply_function
36
+ subject
37
+ .duplicate
38
+ .map{ |x| x.apply_to_cdr(function) }
39
+ end
40
+
41
+ def _init_function(args, blk)
42
+ @function = Function.new(*args, &blk)
43
+ end
44
+
45
+ def _make_function(*args, &blk)
46
+ case args.first
47
+ when Symbol
48
+ Function.new(*args, &blk)
49
+ when NilClass
50
+ Function.new([], &blk)
51
+ else
52
+ Function.new(*([:==]+args), &blk)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lab42_console
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-16 00:00:00.000000000 Z
11
+ date: 2020-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: forwarder2
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-doc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.1.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.1.0
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rspec
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -104,9 +118,11 @@ files:
104
118
  - LICENSE
105
119
  - README.md
106
120
  - lib/lab42/console.rb
121
+ - lib/lab42/console/cons.rb
107
122
  - lib/lab42/console/enum.rb
108
- - lib/lab42/console/runner.rb
123
+ - lib/lab42/console/function.rb
109
124
  - lib/lab42/console/version.rb
125
+ - lib/lab42/console/wrapper.rb
110
126
  homepage: https://github.com/RobertDober/lab42_console
111
127
  licenses:
112
128
  - Apache
@@ -1,62 +0,0 @@
1
- module Lab42
2
- class Console
3
- class Runner
4
-
5
- attr_reader :filter, :method, :subject
6
-
7
- def fnd(*args)
8
- @method = :find
9
- @filter = _make_filter(*args)
10
- run
11
- end
12
-
13
- def run
14
- subject
15
- .send(method, &filter)
16
- end
17
-
18
- def sel(*args)
19
- @method = :filter
20
- @filter = _make_filter(*args)
21
- run
22
- end
23
-
24
-
25
- private
26
-
27
- def initialize(*args, by: [], method:, subject:)
28
- @args = args
29
- @by = by
30
- @id = args.first
31
- @method = method
32
- @subject = subject
33
- @filter = _default_filter
34
- _init_by(by) unless by.empty?
35
- end
36
-
37
- def _default_filter
38
- -> element do
39
- element.id == @id
40
- rescue
41
- nil
42
- end
43
- end
44
-
45
- def _init_by(by)
46
- @args = by
47
- end
48
-
49
- def _make_filter(*args)
50
- if args.size == 1
51
- -> element do
52
- element.send(*@args) == args.first
53
- end
54
- else
55
- -> element do
56
- element.send(*@args).send(*args)
57
- end
58
- end
59
- end
60
- end
61
- end
62
- end