Skip to content

Commit

Permalink
replace .map(&block).sum with more idiomatic .sum(&block)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Lerner committed Apr 14, 2024
1 parent 41b4b11 commit f8b59b0
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions app/controllers/grader_allocations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def patch
end

def update
total_weight = params[:weight].values.map(&:to_f).sum
total_weight = params[:weight].values.sum(&:to_f)
if total_weight == 0
redirect_back fallback_location: edit_course_assignment_grader_allocations_path(@course, @assignment, @grader),
alert: "Total weight for all graders is zero; cannot allocate any work!"
Expand Down Expand Up @@ -260,7 +260,7 @@ def stats
@grader_info = GraderAllocation.where(course: @course).group_by(&:who_grades_id).map do |g, gas|
notdone, finished = gas.partition{|ga| ga.abandoned? || ga.grading_completed.nil?}
abandoned, incomplete = notdone.partition{|ga| ga.abandoned?}
avg_grading_time = finished.map{|ga| (ga.grading_completed - ga.grading_assigned) / 1.day.seconds }.sum
avg_grading_time = finished.sum{|ga| (ga.grading_completed - ga.grading_assigned) / 1.day.seconds }
if finished.count > 0
avg_grading_time = avg_grading_time / finished.count
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def rescind_lateness
end

def edit_plagiarism
@max_points = @assignment.graders.map(&:avail_score).sum
@max_points = @assignment.graders.sum(&:avail_score)
end

def update_plagiarism
Expand All @@ -226,7 +226,7 @@ def update_plagiarism
return
end

@max_points = @assignment.graders.map(&:avail_score).sum
@max_points = @assignment.graders.sum(&:avail_score)
guilty_students.each do |id, penalty|
penaltyPct = (100.0 * penalty.to_f) / @max_points.to_f
student = guilty_users[id]
Expand Down
6 changes: 3 additions & 3 deletions app/helpers/course_spreadsheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def create_exams(sheet)
sheet.columns.push(Col.new("", "Number"), Col.new("", "Percent"), Col.new("", "Percent"), Col.new("", "Percent"))
labels.push(Cell.new("Total"), Cell.new("Computed%"), Cell.new("Curved"), Cell.new("OnServer%"))
normal_questions_and_indices = questions.zip(0..(questions.count - 1)).delete_if do |q, idx| q["extra"] end
tot_weight = normal_questions_and_indices.map{|q, idx| q["weight"]}.sum()
tot_weight = normal_questions_and_indices.sum{|q, idx| q["weight"]}
weight.push(Cell.new(nil, Formula.new(tot_weight, "SUM",
*(normal_questions_and_indices.map do |q, idx|
CellRef.new(nil,
Expand Down Expand Up @@ -310,7 +310,7 @@ def create_hws(sheet, exams)
sheet.columns.push(Col.new("", "Number"), Col.new("", "Percent"), Col.new("", "Percent"), Col.new("", "Percent"))
labels.push(Cell.new("Total"), Cell.new("Lateness"), Cell.new("Computed%"), Cell.new("OnServer%"))
graders = grades.graders.to_a
tot_weight = graders.reject(&:extra_credit).map(&:avail_score).sum()
tot_weight = graders.reject(&:extra_credit).sum(&:avail_score)
grader_weights = graders.zip(0..graders.count).reject{|g, _| g.extra_credit}.map do |g, i|
CellRef.new(nil, Spreadsheet.col_name(weight.count - @graders_count + i), true, 3, true, g.avail_score)
end
Expand Down Expand Up @@ -362,7 +362,7 @@ def create_hws(sheet, exams)
if sub[:sub].ignore_late_penalty
sheet.push_row(i, "<ignore>")
elsif plagiarism_status
penalty = plagiarism_status.pluck(:weight).sum
penalty = plagiarism_status.sum(&:weight)
sheet.push_row(i, "Plagiarized (-#{penalty} pts)")
# (SumGrade - PlagiarismPenalty) / MaxPoints
sum_grade = Formula.new(nil, "MAX", 0,
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/graph_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def self.assign_graders(submissions, graders, weights, prohibitions)
fractional_capacities << [k, fc]
end

allocated_capacity = fractional_capacities.map { |arr| arr.second.floor }.sum
allocated_capacity = fractional_capacities.sum { |arr| arr.second.floor }
# Fractional capacity is index 1. Compare syntax will sort in desc. order.
fractional_capacities.sort { |a, b| b.second <=> a.second }
current_index = 0
Expand Down
2 changes: 1 addition & 1 deletion app/models/assignments/exam.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def set_exam_graders
sc.check(questions).each{|e| self.errors.add(:base, e)}
return false if self.errors.count > 0
questions = sc.convert(questions)
@total_weight = flattened_questions(questions).reject{|q| q["extra"]}.map{|q| q["weight"]}.sum
@total_weight = flattened_questions(questions).reject{|q| q["extra"]}.sum{|q| q["weight"]}
grader = self.graders.first
if grader.nil?
grader = Grader.new(type: "ExamGrader", assignment: self)
Expand Down
6 changes: 3 additions & 3 deletions app/models/graders/exam_grader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ def upload_file=(data)
end

def normal_weight
self.assignment.flattened_questions.reject{|q| q["extra"]}.map{|q| q["weight"]}.sum
self.assignment.flattened_questions.reject{|q| q["extra"]}.sum{|q| q["weight"]}
end

def extra_credit_weight
self.assignment.flattened_questions.select{|q| q["extra"]}.map{|q| q["weight"]}.sum
self.assignment.flattened_questions.select{|q| q["extra"]}.sum{|q| q["weight"]}
end

def export_data
Expand Down Expand Up @@ -268,7 +268,7 @@ def do_raw_grading(sub, grade, grades, num_questions)
def do_grading(assignment, sub, grade = nil)
g = grade || self.grade_for(sub)
comments = sub.inline_comments.filter{|c| c.grade_id == g.id && !c.suppressed && c.line < @num_questions}
score = comments.pluck(:weight).sum
score = comments.sum(&:weight)

g.out_of = self.avail_score
g.score = [0, score].max # can get extra credit above max score
Expand Down
2 changes: 1 addition & 1 deletion app/models/submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def compute_grade!
end
plagiarism = self.plagiarism_status
if plagiarism.count > 0
penalty = plagiarism.pluck(:weight).sum
penalty = plagiarism.sum(:weight)
log += "Plagiarism penalty => #{penalty}"
score -= penalty
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/grades/_exam_curve_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<% flat_questions = @assignment.flattened_questions %>
<% col_count = flat_questions.count %>
<% any_parts = (questions.count != col_count) %>
<% total_score = flat_questions.reject{|q| q["extra"]}.map{|q| q["weight"]}.sum %>
<% total_score = flat_questions.reject{|q| q["extra"]}.sum{|q| q["weight"]} %>
<% used_subs = @used_subs.group_by(&:user_id) %>

<% if @student_info.length >= 1 %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/submissions/show_exam.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
<tr>
<td>Problem <%= q_num + 1 %>: <%= q["name"] %><% if q["extra"] %> (Extra Credit)<% end %></td>
<% if q["parts"] %>
<% q_score = (grades[i .. (i - 1 + q["parts"].length)]).map do |g|
<% q_score = (grades[i .. (i - 1 + q["parts"].length)]).sum do |g|
((g && g["weight"].to_f) || 0.0)
end.sum %>
end %>
<td>
<%= show_score(q_score, @assignment, cur_reg_staff) %> / <%= show_score(q["weight"]) %></td>
<% else %>
Expand All @@ -45,7 +45,7 @@
<% end %>
<tr>
<td><h4>Raw score:</h4></td>
<td><h4><%= show_score(grades[0..@assignment.flattened_questions.count - 1].map(&:weight).map(&:to_f).sum,
<td><h4><%= show_score(grades[0..@assignment.flattened_questions.count - 1].map(&:weight).sum(&:to_f),
@assignment, cur_reg_staff) %> / <%= show_score(@grader.avail_score) %></h4></td>
</tr>
<% if grades[@assignment.flattened_questions.count] && !grades[@assignment.flattened_questions.count].new_record? %>
Expand Down

0 comments on commit f8b59b0

Please sign in to comment.