⚲
Project
General
Profile
Sign in
Register
Home
Projects
Help
Search
:
Ruby master
All Projects
Ruby
»
Ruby master
Overview
Activity
Roadmap
Issues
Repository
Like
Download (830 Bytes)
Feature #11090
ยป each_uniq.rb
Hanmac (Hans Mackowiak)
, 04/23/2015 07:37 AM
module
Enumerable
def
each_uniq
(
&
block
)
return
to_enum
:each_uniq
unless
block_given?
uniq_map
=
{}
each
do
|*
a
|
next
if
uniq_map
.
key?
(
a
)
uniq_map
[
a
]
=
true
block
.
call
(
*
a
)
end
end
# @return [Enumerator]
def
each_uniq_by
(
&
filter
)
Enumerator
.
new
do
|
y
|
uniq_map
=
{}
each
do
|*
a
|
key
=
filter
.
call
(
*
a
)
next
if
uniq_map
.
key?
(
key
)
uniq_map
[
key
]
=
true
y
.
yield
(
*
a
)
end
end
end
end
src
=
[
1
,
1
,
1
,
1
,
2
,
2
,
3
,
4
,
5
,
6
]
result
=
src
.
each_uniq
.
to_a
fail
'Dafuq happened there m8.'
unless
[
1
,
2
,
3
,
4
,
5
,
6
]
==
result
result
=
src
.
each_uniq_by
(
&
:even?
).
to_a
# actually I have no clue how to test this :x
fail
'each_uniq_by failed, epicly ;3'
unless
[
1
,
2
]
==
result
result
=
src
.
each_uniq_by
do
|
i
|
i
*
i
end
.
to_a
puts
result
.
to_a
(1-1/1)
Loading...