Bożydar Sobczak avatar Bożydar Sobczak committed be2a437

More functional

Comments (0)

Files changed (1)

 end
 
 def min(*list)
-  acc = nil
-  list.each do |item|
-    acc = item if acc.nil? || item < acc 
+  list.inject(nil) do |acc, item|
+    if acc.nil? || item < acc 
+      item
+    else
+      acc
+    end
   end
-  acc
 end
 
 def max(*list)
-  acc = nil
-  list.each do |item|
-    acc = item if acc.nil? || item > acc 
+  list.inject(nil) do |acc, item|
+    if acc.nil? || item > acc 
+      item
+    else
+      acc
+    end
   end
-  acc
 end
 
 def factorial_classic(arg)
   elsif arg == 0
     1
   else
-    acc = 1
-    (1..arg).each { |item| 
-      acc *= item
-    }
-    acc
+    (1..arg).inject(1) do |acc, item| 
+      acc * item
+    end
   end
 end
 
 end
 
 def tree(arg)
-  (1..arg).each { |item|
+  (1..arg).each do |item|
     puts " " * (arg - item) + ("##" * item)
-  }
+  end
   puts " " * (arg - 1) + "##"
 end
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.