New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

takeWhile and dropWhile #244

Closed
srid opened this Issue May 15, 2015 · 1 comment

Comments

Projects
None yet
1 participant
@srid

srid commented May 15, 2015

I was somewhat surprised that these functions were missing from the standard library (I needed them to implement groupBy). Any reason these functions (with better implementation than mine) cannot be added to the standard library?

-- Take a sorted list and group it by some key function
groupBy : (List a) -> (a -> String) -> (List (String, (List a)))
groupBy list f =
  case list of
    []       -> []
    hd::tl   -> let
                  key = f hd
                  chunk = takeWhile (\x -> (f x) == key) tl
                  rest = dropWhile (\x -> (f x) == key) tl
                in
                  (key, hd :: chunk) :: groupBy rest f

takeWhile : (a -> Bool) -> (List a) -> (List a)
takeWhile predicate list =
  case list of
    []       -> []
    hd::tl   -> if | (predicate hd) -> hd :: takeWhile predicate tl
                   | otherwise -> []

dropWhile : (a -> Bool) -> (List a) -> (List a)
dropWhile predicate list =
  case list of
    []       -> []
    hd::tl   -> if | (predicate hd) -> dropWhile predicate tl
                   | otherwise -> tl
@srid

This comment has been minimized.

Show comment
Hide comment
@srid

srid May 15, 2015

Oops, apologies of being lazy and not having read the contribution guidelines.

srid commented May 15, 2015

Oops, apologies of being lazy and not having read the contribution guidelines.

@srid srid closed this May 15, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment