News for hackers. Stuff that matters.
Re:Imperative vs. Declarative by RyanMcGreal on Tuesday April 02, 2013 @01:48PM (#5480892)
Aside: Python is similar to Ruby, albeit using the sorted() function rather than the sort() method.

    sorted(vals)[-2]
Re:Imperative vs. Declarative by notaddicted on Tuesday April 02, 2013 @02:05PM (#5481017)
Also available in python:

  import heapq
  n = 2 
  heapq.nlargest(n, vals)[n-1]
Edit: and out of morbid curiosity, here is the same implemented by a scan. It could be arranged as a single statement.

  swapIfGt = lambda xs, q: xs if xs[0] >= q else sorted([q]+xs[1:])
  n = 2
  reduce(swapIfGt, vals, [None]*n)[0]
Edit2: rewrote swapIfGt
Re:Imperative vs. Declarative by textminer on Tuesday April 02, 2013 @02:09PM (#5481047)
The pedant in me squirms at seeing a quadratic-time solution to something so linear. Use a heap or a scan, sir!
Re:Imperative vs. Declarative by textminer on Tuesday April 02, 2013 @02:10PM (#5481051)
Oh, now I have to yell at myself. No doubt Python's sorted is some n*logn quicksort, and not actually in quadratic time. Crow for all!
Re:Imperative vs. Declarative by masklinn on Tuesday April 02, 2013 @02:12PM (#5481068)
It's an adaptive mergesort-based sort: http://en.wikipedia.org/wiki/Timsort
Re:Imperative vs. Declarative by gnuvince on Tuesday April 02, 2013 @02:31PM (#5481224)
Still, I'd rather have a loop and be linear than using a sort and being linearithmic.