-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add foldable-traversable, as well as some deps needed for its tests
- Loading branch information
1 parent
a62a288
commit 9727d27
Showing
10 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
|
||
*.d | ||
*.o | ||
*.bak | ||
.vscode | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include "purescript.h" | ||
|
||
// Tested with package v4.0.0 | ||
|
||
FOREIGN_BEGIN( Data_Foldable ) | ||
|
||
|
||
exports["foldrArray"] = [](const boxed& f) -> boxed { | ||
return [=](const boxed& init) -> boxed { | ||
return [=](const boxed& xs_) -> boxed { | ||
auto acc = init; | ||
const auto& xs = unbox<array_t>(xs_); | ||
for (auto it = xs.crbegin(), end = xs.crend(); it != end ; it++) { | ||
acc = f(*it)(acc); | ||
} | ||
return acc; | ||
}; | ||
}; | ||
}; | ||
|
||
exports["foldlArray"] = [](const boxed& f) -> boxed { | ||
return [=](const boxed& init) -> boxed { | ||
return [=](const boxed& xs_) -> boxed { | ||
auto acc = init; | ||
const auto& xs = unbox<array_t>(xs_); | ||
for (auto it = xs.cbegin(), end = xs.cend(); it != end ; it++) { | ||
acc = f(acc)(*it); | ||
} | ||
return acc; | ||
}; | ||
}; | ||
}; | ||
|
||
FOREIGN_END |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include "purescript.h" | ||
|
||
// Tested with package v4.0.0 | ||
|
||
FOREIGN_BEGIN( Data_FunctorWithIndex ) | ||
|
||
exports["mapWithIndexArray"] = [](const boxed& f) -> boxed { | ||
return [=](const boxed& xs_) -> boxed { | ||
const auto& xs = unbox<array_t>(xs_); | ||
array_t result; | ||
int i = 0; | ||
for (auto it = xs.cbegin(), end = xs.cend(); it != end ; it++, i++) { | ||
result.push_back(f(i)(*it)); | ||
} | ||
return result; | ||
}; | ||
}; | ||
|
||
FOREIGN_END |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include <cmath> | ||
#include "purescript.h" | ||
|
||
// Tested with package v4.0.0 | ||
|
||
using namespace purescript; | ||
|
||
static auto array1(const boxed& a) -> boxed { | ||
return array_t{ a }; | ||
} | ||
|
||
static auto array2(const boxed& a) -> boxed { | ||
return [=](const boxed& b) { | ||
return array_t{ a, b }; | ||
}; | ||
} | ||
|
||
static auto array3(const boxed& a) -> boxed { | ||
return [=](const boxed& b) { | ||
return [=](const boxed& c) { | ||
return array_t{ a, b, c }; | ||
}; | ||
}; | ||
} | ||
|
||
static auto concat2(const boxed& xs) -> boxed { | ||
return [=](const boxed& ys_) -> boxed { | ||
array_t result(unbox<array_t>(xs)); | ||
const auto& ys = unbox<array_t>(ys_); | ||
result.insert(result.end(), ys.cbegin(), ys.cend()); | ||
return result; | ||
}; | ||
} | ||
|
||
static auto go(const long bot, | ||
const long top, | ||
const fn_t& apply, | ||
const fn_t& map, | ||
const fn_t& pure, | ||
const fn_t& f, | ||
const array_t& array) -> boxed { | ||
switch (top - bot) { | ||
case 0: return pure(array_t{}); | ||
case 1: return map(array1)(f(array[bot])); | ||
case 2: return apply(map(array2)(f(array[bot])))(f(array[bot + 1])); | ||
case 3: return apply(apply(map(array3)(f(array[bot])))(f(array[bot + 1])))(f(array[bot + 2])); | ||
default: | ||
// This slightly tricky pivot selection aims to produce two | ||
// even-length partitions where possible. | ||
long pivot = bot + static_cast<long>(std::floor((top - bot) / 4)) * 2; | ||
return apply(map(concat2)(go(bot, pivot, apply, map, pure, f, array)))(go(pivot, top, apply, map, pure, f, array)); | ||
} | ||
} | ||
|
||
FOREIGN_BEGIN( Data_Traversable ) | ||
|
||
exports["traverseArrayImpl"] = [](const boxed& apply) -> boxed { | ||
return [=](const boxed& map) -> boxed { | ||
return [=](const boxed& pure) -> boxed { | ||
return [=](const boxed& f) -> boxed { | ||
return [=](const boxed& array_) -> boxed { | ||
const auto& array = unbox<array_t>(array_); | ||
return go(0, | ||
array.size(), | ||
unbox<fn_t>(apply), | ||
unbox<fn_t>(map), | ||
unbox<fn_t>(pure), | ||
unbox<fn_t>(f), | ||
array); | ||
}; | ||
}; | ||
}; | ||
}; | ||
}; | ||
|
||
FOREIGN_END |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <cmath> | ||
#include <limits> | ||
#include "purescript.h" | ||
|
||
// Tested with package v4.0.0 | ||
|
||
FOREIGN_BEGIN( Data_Int ) | ||
|
||
exports["toNumber"] = [](const boxed& n) -> boxed { | ||
return static_cast<double>(unbox<int>(n)); | ||
}; | ||
|
||
exports["pow"] = [](const boxed& n_) -> boxed { | ||
const auto n = unbox<int>(n_); | ||
return [=](const boxed& p) -> boxed { | ||
const auto r = std::lround(std::pow(n, unbox<int>(p))); | ||
assert(r >= std::numeric_limits<int>::min() && | ||
r <= std::numeric_limits<int>::max()); | ||
return static_cast<int>(r); | ||
}; | ||
}; | ||
|
||
FOREIGN_END |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <cmath> | ||
#include "purescript.h" | ||
|
||
// Tested with package v2.1.1 | ||
|
||
FOREIGN_BEGIN( Math ) | ||
|
||
exports["abs"] = [](const boxed& x) -> boxed { | ||
return std::fabs(unbox<double>(x)); | ||
}; | ||
|
||
exports["pow"] = [](const boxed& n_) -> boxed { | ||
const auto n = unbox<double>(n_); | ||
return [=](const boxed& p) -> boxed { | ||
return std::pow(n, unbox<double>(p)); | ||
}; | ||
}; | ||
|
||
FOREIGN_END |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include "purescript.h" | ||
|
||
// Tested with package v4.0.0 | ||
|
||
FOREIGN_BEGIN( Control_Apply ) | ||
|
||
|
||
exports["arrayApply"] = [](const boxed& fs_) -> boxed { | ||
return [=](const boxed& xs_) -> boxed { | ||
const auto& fs = unbox<array_t>(fs_); | ||
const auto& xs = unbox<array_t>(xs_); | ||
array_t result; | ||
for (auto f = fs.cbegin(), fend = fs.cend(); f != fend; f++) { | ||
for (auto x = xs.cbegin(), xend = xs.cend(); x != xend; x++) { | ||
result.emplace_back((*f)(*x)); | ||
} | ||
} | ||
return result; | ||
}; | ||
}; | ||
|
||
FOREIGN_END |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "purescript.h" | ||
|
||
// Tested with package v4.0.0 | ||
|
||
FOREIGN_BEGIN( Unsafe_Coerce ) | ||
|
||
exports["unsafeCoerce"] = [](const boxed& x) -> boxed { | ||
return x; | ||
}; | ||
|
||
FOREIGN_END |