Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Elegant solutions

Name: Anonymous 2007-06-28 15:16 ID:lcs47ZU6

Im bored, show me some good code (any language whatsoever)
Post a problem, then a great solution.

1) Transpose a matrix
(apply #'mapcar #'list '(( 1  2  3  4)
                         ( 5  6  7  8)
                         ( 9 10 11 12)
                         (13 14 15 16)))
(( 1  5  9 13)
 ( 2  6 10 14)
 ( 3  7 11 15)
 ( 4  8 12 16))

Name: Anonymous 2007-06-28 15:24 ID:ERvjIcVI

2) kill a computation if it doesn't terminate after <limit> microseconds

compute_ :: Int -> IO a -> IO (Maybe a)
compute_ limit computation = do
    result <- atomically newEmptyTMVar
    runner <- forkIO $ do c <- computation
                          atomically $ putTMVar result $ Just c
    reader <- forkIO $ do threadDelay limit
                          killThread runner
                          atomically $ putTMVar result $ Nothing
    a <- atomically $ takeTMVar result
    killThread runner
    killThread reader
    return a


*Main> let loop = loop in compute_ 1000000 loop
Nothing

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List