-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
My enumerable #5
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,66 +10,143 @@ def map | |
end | ||
|
||
def filter | ||
# Your code goes here | ||
Array.new.tap do |arr| | ||
each do |element| | ||
arr << element if (yield element) == true | ||
end | ||
end | ||
end | ||
|
||
def reject | ||
# Your code goes here | ||
Array.new.tap do |arr| | ||
each do |element| | ||
arr << element if (yield element) == false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
end | ||
end | ||
end | ||
|
||
def reduce(initial = nil) | ||
# Your code goes here | ||
each do |element| | ||
if initial.nil? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Защо не изнесеш тази проверка извън |
||
initial = element | ||
else | ||
initial = yield element, initial | ||
end | ||
end | ||
initial | ||
end | ||
|
||
def any? | ||
# Your code goes here | ||
each do |element| | ||
if (yield element) == false | ||
false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Този false на кого го връщаш? |
||
else | ||
return true | ||
end | ||
end | ||
end | ||
|
||
def all? | ||
# Your code goes here | ||
each do |element| | ||
if (yield element) == false | ||
return false | ||
else | ||
true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Това |
||
end | ||
end | ||
end | ||
|
||
def include?(element) | ||
# Your code goes here | ||
each do |item| | ||
if item == element | ||
return true | ||
else | ||
false | ||
end | ||
end | ||
end | ||
|
||
def count(element = nil) | ||
# Your code goes here | ||
counter = 0 | ||
each do |item| | ||
counter += 1 if element.nil? | ||
counter += 1 if item == element | ||
end | ||
counter | ||
end | ||
|
||
def size | ||
# Your code goes here | ||
counter = 0 | ||
each do | ||
counter += 1 | ||
end | ||
counter | ||
end | ||
|
||
def min | ||
# Your code goes here. | ||
reduce do |element, accumulator| | ||
accumulator > element ? element : accumulator | ||
end | ||
end | ||
|
||
def min_by | ||
# Your code goes here. | ||
reduce do |element, accumulator| | ||
(yield accumulator) > element.size ? element : accumulator | ||
end | ||
end | ||
|
||
def max | ||
# Your code goes here. | ||
reduce do |element, accumulator| | ||
accumulator < element ? element : accumulator | ||
end | ||
end | ||
|
||
def max_by | ||
# Your code goes here. | ||
reduce do |element, accumulator| | ||
(yield accumulator) < element.size ? element : accumulator | ||
end | ||
end | ||
|
||
def take(n) | ||
# Your code goes here. | ||
counter = 0 | ||
Array.new.tap do |arr| | ||
each do |element| | ||
return Array.new if n == 0 | ||
arr << element | ||
counter += 1 | ||
break if counter == n | ||
end | ||
end | ||
end | ||
|
||
def take_while | ||
# Your code goes here. | ||
Array.new.tap do |arr| | ||
each do |element| | ||
if (yield element) == true | ||
arr << element | ||
else | ||
break | ||
end | ||
end | ||
return [] if arr.size == 0 | ||
end | ||
end | ||
|
||
def drop(n) | ||
# Your code goes here. | ||
counter = 0 | ||
Array.new.tap do |arr| | ||
each do |element| | ||
arr << element if counter >= n | ||
counter += 1 | ||
end | ||
end | ||
end | ||
|
||
def drop_while | ||
# Your code goes here. | ||
counter = 0 | ||
each do |element| | ||
counter += 1 if (yield element) == true | ||
return drop(counter) if (yield element) == false | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Няма нужда от сравнението
== true
В Ruby всичко е
true
, освенfasle
, иnil