From e1b1aec35cb2d627be78bd5d9513e10129cb256f Mon Sep 17 00:00:00 2001 From: David Vollbracht Date: Mon, 6 Jul 2015 09:30:59 -0400 Subject: [PATCH] Fix missing foreign `count` function. Also updated the documentation to more accurately reflect the function's behavior. I think the function could be given a better name to communicate its behavior too. Since it's an exported function, I didn't rename it as part of this fix. --- docs/Data/String.md | 3 ++- src/Data/String.js | 7 +++++++ src/Data/String.purs | 3 ++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/Data/String.md b/docs/Data/String.md index b0197f1..839ea08 100644 --- a/docs/Data/String.md +++ b/docs/Data/String.md @@ -178,7 +178,8 @@ Returns the string without the first `n` characters. count :: (Char -> Boolean) -> String -> Int ``` -Returns the number of characters in the string for which the predicate holds. +Returns the number of contiguous characters at the beginning +of the string for which the predicate holds. #### `split` diff --git a/src/Data/String.js b/src/Data/String.js index a2d910c..5e47ced 100644 --- a/src/Data/String.js +++ b/src/Data/String.js @@ -120,6 +120,13 @@ exports.drop = function (n) { }; }; +exports.count = function (p) { + return function (s) { + for (var i = 0; i < s.length && p(s.charAt(i)); i++); {} + return i; + }; +}; + exports.split = function (sep) { return function (s) { return s.split(sep); diff --git a/src/Data/String.purs b/src/Data/String.purs index f7af27f..3f737f2 100644 --- a/src/Data/String.purs +++ b/src/Data/String.purs @@ -172,7 +172,8 @@ foreign import take :: Int -> String -> String -- | Returns the string without the first `n` characters. foreign import drop :: Int -> String -> String --- | Returns the number of characters in the string for which the predicate holds. +-- | Returns the number of contiguous characters at the beginning +-- | of the string for which the predicate holds. foreign import count :: (Char -> Boolean) -> String -> Int -- | Returns the substrings of the first string separated along occurences