Last active
January 31, 2019 16:09
-
-
Save LATBauerdick/337e699e3c45587fa9002c7c8b67000a to your computer and use it in GitHub Desktop.
example code for purescript-native ffi code, using a purescript call-back function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// foreign import countPrefix :: (Char -> Boolean) -> String -> Int | |
// | |
exports["countPrefix"] = [](const boxed& f_) -> boxed { | |
return [=](const boxed& s_) -> boxed { | |
const string& s = unbox<string>(s_); | |
size_t i = 0; | |
while ( i<s.length() && unbox<bool>(f_(boxed(s.substr(i, 1)))) ) i++; | |
return i; | |
}; | |
}; | |
// then call this from purescript: | |
// logShow $ countPrefix (_ /= 'c') "aaaxxxcccxxx" | |
// should print 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment