Skip to content
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 #2

Closed
sergiors opened this issue Jul 3, 2016 · 1 comment
Closed

takewhile and dropwhile #2

sergiors opened this issue Jul 3, 2016 · 1 comment

Comments

@sergiors
Copy link
Owner

sergiors commented Jul 3, 2016

No description provided.

@sergiors sergiors changed the title takewhile and dropwhile takewhile and dropwhile Jul 3, 2016
@haskellcamargo
Copy link
Collaborator

The implementation logic is such simple:

takeWhile               :: (a -> Bool) -> [a] -> [a]
takeWhile _ []          =  []
takeWhile p (x:xs)
            | p x       =  x : takeWhile p xs
            | otherwise =  []

A gambiarra and mutable way to do it:

<?php

function takewhile($p, $xss)
{
  if (empty($xss)) {
    return [];
  }

  $head = $xss[0];

  array_shift($xss);

  return $p($head)
    ? array_merge([$head], takewhile($p, $xss))
    : [];
}

$list = [2, 4, 6, 8, 7, 10, 12];
$even = function ($x) { return $x % 2 === 0; };

var_dump(takewhile($even, $list));

Forgive me for the mutable and imperative way to do it. It's a 30 seconds code.

sergiors added a commit that referenced this issue Jul 3, 2016
Remove unnecessary partial
@sergiors sergiors closed this as completed Jul 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants