A representative but thoroughly useless and unscientific way to see how people write short pieces of code.
Write a function, method, or other similar construct in your favorite language that:
- accepts an array
arr
and a valuev
- returns the element in
arr
whose successor isv
, if there is such an element; otherwise return nothing
Test cases:
find_before([5, 6, 7], 6)
# => 5
find_before([5, 6, 7], 7)
# => 6
find_before([5, 6, 7], 5)
# => nothing
find_before([5, 6, 7], 999)
# => nothing
@jonpryor Very nice! The API with the
Maybe<T>
return value variant feels really fluent too.If you could keep the intent of the method, but change the specification -- for example, to do something else instead of returning null in the corner cases -- would you make an adjustment that would make it more similar to how you'd expect C# methods to work? (For example, would you expect an exception to be thrown instead?)