lab42_core 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +37 -0
- data/lib/lab42/core/fn/basic_object.rb +10 -2
- data/lib/lab42/core/fn/iterator_reimpl.rb +10 -0
- data/lib/lab42/core/kernel.rb +7 -0
- data/lib/lab42/core/object.rb +3 -0
- data/lib/lab42/core/version.rb +1 -1
- data/lib/lab42/core.rb +1 -0
- metadata +3 -3
- data/lib/lab42/core/fn/fm.rb +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 028aefd79743422aa1e099ba360099b8195080a4
|
4
|
+
data.tar.gz: fef7d4cd84cb13f157133ca2c71a0c9148a8479c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b17bebb308b74058fedcd8a4dee757058347ba1e968f089c23b1e8081f9cb09fec00f2bdff13fc68ef626ffe4a1bff1c4f8eea9035586eae67d31548b4e3c6b5
|
7
|
+
data.tar.gz: c73f4623f9af802c1cbf684a8f30bd20a6cd976392f3e6621152118f6a095bb56f6b289803b46a5eca812474b12a88594d065fd5c4a2a4e6edaab35ea8c447d3
|
data/README.md
CHANGED
@@ -26,6 +26,8 @@ Ruby Core Module Extensions (in the spirit of lab419/core)
|
|
26
26
|
|
27
27
|
Must be required specifically!
|
28
28
|
|
29
|
+
### Access to methods
|
30
|
+
|
29
31
|
```ruby
|
30
32
|
require 'lab42/core/fn'
|
31
33
|
|
@@ -37,3 +39,38 @@ Must be required specifically!
|
|
37
39
|
# It also extends some enumerable methods for easier execution
|
38
40
|
%w{hello world}.each printer
|
39
41
|
```
|
42
|
+
|
43
|
+
Thusly `fn` gives us access to methods of objects, but what about instance methods?
|
44
|
+
|
45
|
+
### Access to instance methods
|
46
|
+
|
47
|
+
Is realized via `fm`.
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
require 'lab42/core/fn'
|
51
|
+
|
52
|
+
(1..100).reduce Fixnum.fm.+ # ---> 5050
|
53
|
+
```
|
54
|
+
|
55
|
+
### Objects as constant procs
|
56
|
+
|
57
|
+
While `Object#self` will return the obvious, so will by extension `object.fn.self`.
|
58
|
+
|
59
|
+
This can be very useful, e.g. in this nice way to create an empty array ;)
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
(1..100).filter(false.fn.self)
|
63
|
+
```
|
64
|
+
|
65
|
+
If you hesitate to use this all, have a look into Kernel#const_lambda
|
66
|
+
|
67
|
+
### Partial Application
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
f = Array.fm.push :next
|
71
|
+
[[],[1].map( f ) # ---> [[:next], [1, :next]]
|
72
|
+
|
73
|
+
a=[]
|
74
|
+
f = a.fn.push :first
|
75
|
+
f.(1) # a ---> [:first, 1]
|
76
|
+
```
|
@@ -1,15 +1,23 @@
|
|
1
1
|
module Lab42
|
2
2
|
module Core
|
3
3
|
class Fn < BasicObject
|
4
|
+
def self;
|
5
|
+
->(*args,&blk){ @source } # This is *not* a recursive call
|
6
|
+
end
|
4
7
|
private
|
5
8
|
def initialize source
|
6
9
|
@source = source
|
7
10
|
end
|
8
11
|
|
9
12
|
def method_missing *args, &blk
|
10
|
-
@source.method args.first
|
13
|
+
return @source.method args.first if args.size == 1 && blk.nil?
|
14
|
+
s = @source
|
15
|
+
-> *a, &b do
|
16
|
+
s.method( args.first ).(*(args[1..-1]+a), &(blk||b))
|
17
|
+
end
|
11
18
|
end
|
12
19
|
end # class Fn
|
20
|
+
|
13
21
|
class Fm < BasicObject
|
14
22
|
private
|
15
23
|
def initialize source
|
@@ -19,7 +27,7 @@ module Lab42
|
|
19
27
|
def method_missing *args, &blk
|
20
28
|
m = @source.instance_method args.first
|
21
29
|
-> receiver, *a, &b do
|
22
|
-
m.bind( receiver ).(*a, &b)
|
30
|
+
m.bind( receiver ).(*(args[1..-1]+a), &(blk||b))
|
23
31
|
end
|
24
32
|
end
|
25
33
|
end # class Fn
|
@@ -2,11 +2,18 @@ module Lab42
|
|
2
2
|
module Core
|
3
3
|
module IteratorReimpl
|
4
4
|
def self.included into
|
5
|
+
# TODO: Try to DRY
|
5
6
|
into.module_eval do
|
6
7
|
alias_method :__lab42_core_iterator_map__, :map
|
7
8
|
def map behavior=nil, &blk
|
8
9
|
__lab42_core_iterator_map__(&(behavior||blk))
|
9
10
|
end
|
11
|
+
alias_method :__lab42_core_iterator_select__, :select
|
12
|
+
def select behavior=nil, &blk
|
13
|
+
__lab42_core_iterator_select__(&(behavior||blk))
|
14
|
+
end
|
15
|
+
alias_method :filter, :select
|
16
|
+
|
10
17
|
alias_method :__lab42_core_iterator_reduce__, :reduce
|
11
18
|
def reduce behavior=nil, &blk
|
12
19
|
if Symbol === behavior
|
@@ -24,6 +31,9 @@ module Lab42
|
|
24
31
|
__lab42_core_iterator_inject__(value, &(behavior||blk))
|
25
32
|
end
|
26
33
|
end
|
34
|
+
|
35
|
+
def filter *args, &blk
|
36
|
+
end
|
27
37
|
end
|
28
38
|
end
|
29
39
|
end
|
data/lib/lab42/core/kernel.rb
CHANGED
data/lib/lab42/core/version.rb
CHANGED
data/lib/lab42/core.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lab42_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Dober
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -48,13 +48,13 @@ files:
|
|
48
48
|
- lib/lab42/core/fn/array.rb
|
49
49
|
- lib/lab42/core/fn/basic_object.rb
|
50
50
|
- lib/lab42/core/fn/enumerator/lazy.rb
|
51
|
-
- lib/lab42/core/fn/fm.rb
|
52
51
|
- lib/lab42/core/fn/iterator_reimpl.rb
|
53
52
|
- lib/lab42/core/fn/enumerable.rb
|
54
53
|
- lib/lab42/core/dir.rb
|
55
54
|
- lib/lab42/core/version.rb
|
56
55
|
- lib/lab42/core/fn.rb
|
57
56
|
- lib/lab42/core/hash.rb
|
57
|
+
- lib/lab42/core/object.rb
|
58
58
|
- lib/lab42/core/kernel.rb
|
59
59
|
- lib/lab42/core/enumerable.rb
|
60
60
|
- LICENSE
|
data/lib/lab42/core/fn/fm.rb
DELETED
File without changes
|