Skip to content

Commit

Permalink
simpler, awesomer median function
Browse files Browse the repository at this point in the history
  • Loading branch information
shashi committed Aug 22, 2014
1 parent 4f920bf commit 66e6ab6
Showing 1 changed file with 1 addition and 10 deletions.
11 changes: 1 addition & 10 deletions src/widgets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,7 @@ import Base.convert, Base.median
export Slider, ToggleButton, Button, Options, Checkbox, Textbox,
Textarea, RadioButtons, Dropdown, Select, ToggleButtons

function median{T}(r::Range{T})
mid = (first(r) + last(r)) / 2
# snap to the nearest value in range
# This is subject to floating point calculation errors
# but should never throw an InexactError if T is integral
st = step(r)
multiple = ((mid - first(r)) / st)
offset = multiple - round(multiple)
convert(T, mid - offset * st)
end
median(r::Range) = r[(1+length(r))>>1]

### Input widgets

Expand Down

2 comments on commit 66e6ab6

@stevengj
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you change this to Base.median so that you don't shadow the Base function?

@stevengj
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, probably better to leave things as-is, i.e. shadow Base.median. The function here is actually distinct from the Base function for the case of an even number of elements (e.g. median([1:4]) == 2.5), and we probably want to keep it that way to make sure that the initial value is one of the elements of the range.

Please sign in to comment.