turtle icon indicating copy to clipboard operation
turtle copied to clipboard

Implement something like `drop`

Open Gabriella439 opened this issue 10 years ago • 12 comments
trafficstars

This is a reminder to myself to implement a function I mentioned on Stack Overflow:

http://stackoverflow.com/questions/28421469/how-to-drop-lines-when-streaming-from-a-file-using-haskell-and-the-turtle-librar/28422201#28422201

Here is the implementation:

import Data.IORef
import Turtle

drop :: Int -> Shell a -> Shell a
drop n s = Shell (\(FoldM step begin done) -> do
    ref <- newIORef 0
    let step' x a = do
            n' <- readIORef ref
            writeIORef ref (n' + 1)
            if n' < n then return x else step x a
    foldIO s (FoldM step' begin done) )

Gabriella439 avatar Feb 11 '15 17:02 Gabriella439

Note that I'd probably use a name other than drop to avoid a name clash with the Prelude

Gabriella439 avatar Feb 11 '15 17:02 Gabriella439

In that vein you might also want something like tail, no?

3noch avatar Feb 11 '15 18:02 3noch

Elliot, yesterday I was actually thinking about using Turtle along with Frames in IHaskell to do stuff like head/tail files. Then I wondered what I'd name tail to prevent a name clash, since importing qualified didn't seem very appealing to me.

On Wed, Feb 11, 2015 at 12:11 PM, Elliot Cameron [email protected] wrote:

In that vein you might also want something like tail, no?

— Reply to this email directly or view it on GitHub https://github.com/Gabriel439/Haskell-Turtle-Library/issues/22#issuecomment-73932263 .

codygman avatar Feb 11 '15 18:02 codygman

Yeah, tail conflicts with the Prelude, too.

Gabriella439 avatar Feb 11 '15 19:02 Gabriella439

Would it be possible (or even a good idea) to hide the Haskell prelude completely by default ?

PierreR avatar Feb 11 '15 19:02 PierreR

@codygman Yes it's always nice not to need qualified imports. That being said, it's possible to simply import with an alias so that you can use it when ambiguities do arise. So you could still import Turtle as T (which still imports all of Turtle unqualified) and then only when there is an ambiguity use T.tail or something like that.

3noch avatar Feb 11 '15 19:02 3noch

I'm reluctant to conflict with the prelude, mainly because I'd like this library to smoothly transition into learning Haskell in general.

Gabriella439 avatar Feb 11 '15 20:02 Gabriella439

In that case I'm confident you'll come up with good names. :) dropLines, tailLines come to mind...

3noch avatar Feb 11 '15 22:02 3noch

The duplication of names is really what kills beginners. My feeling only from personal experience ;-)

PierreR avatar Feb 11 '15 22:02 PierreR

How about skip?

stschiff avatar Mar 14 '18 15:03 stschiff

Yeah, I like skip

Gabriella439 avatar Mar 14 '18 15:03 Gabriella439

The name skip is now taken. http://hackage.haskell.org/package/turtle-1.5.21/docs/Turtle-Pattern.html#v:skip

Perhaps shellDrop, dropShell or discard?

jchia avatar Oct 13 '20 03:10 jchia