3 ms·
The first example: -- split at whitespace -- "hello world" -> ["hello","world"] words unfoldr (\b -> fmap (const . (second $ drop 1) . break (==' ') $ b
by scrame 15y ago
The first example:
-- split at whitespace
-- "hello world" -> ["hello","world"]
words
unfoldr (\b -> fmap (const . (second $ drop 1) . break (==' ') $ b) . listToMaybe $ b)
takeWhile (not . null) . evalState (repeatM $ modify (drop 1) >> State (break (== ' '))) . (' ' :)
where repeatM = sequence . repeat
fix (\f l -> if null l then [] else let (s,e) = break (==' ') l in s:f (drop 1 e))
Not really in the same spirit at all.
- stouset 15y agoThat blows my mind, but certainly not in a good way. Ruby: $ echo 'hello world' | ruby -e 'p STDIN.read.split'
- nemetroid 15y agoBe aware that those are four separate examples made to showcase features of the language, the simplest of which being just `words`. split.hs: main = interact (show . words) $ echo hello world | runhaskell split.hs
- scrame 15y agoOK, thats not really clear from the layout. Your example is clearer, but if you have to put it in a script and pipe stuff to it, its not really a 1-liner.