Skip to content

Instantly share code, notes, and snippets.

@LATBauerdick
Last active January 31, 2019 16:09
Show Gist options
  • Save LATBauerdick/337e699e3c45587fa9002c7c8b67000a to your computer and use it in GitHub Desktop.
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
// 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