Skip to content

Commit

Permalink
Add foldable-traversable, as well as some deps needed for its tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andyarvanitis committed Oct 7, 2018
1 parent a62a288 commit 9727d27
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

*.d
*.o
*.bak
.vscode

12 changes: 12 additions & 0 deletions effect/effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@ exports["bindE"] = [](const boxed& a) -> boxed {
};
};

exports["foreachE"] = [](const boxed& as_) -> boxed {
return [=](const boxed& f) -> boxed {
return [=]() -> boxed {
const auto& as = unbox<array_t>(as_);
for (auto it = as.cbegin(), end = as.cend(); it != end ; it++) {
f(*it)();
}
return boxed();
};
};
};

FOREIGN_END
34 changes: 34 additions & 0 deletions foldable-traversable/data-foldable.cpp
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
19 changes: 19 additions & 0 deletions foldable-traversable/data-foldablewithindex.cpp
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
76 changes: 76 additions & 0 deletions foldable-traversable/data-traversable.cpp
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
23 changes: 23 additions & 0 deletions integers/data-int.cpp
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
19 changes: 19 additions & 0 deletions math/math.cpp
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
22 changes: 22 additions & 0 deletions prelude/control-apply.cpp
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
8 changes: 8 additions & 0 deletions prelude/data-ring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,13 @@ exports["intSub"] = [](const boxed& x_) -> boxed {
};
};

exports["numSub"] = [](const boxed& x_) -> boxed {
const auto x = unbox<double>(x_);
return [=](const boxed& y_) -> boxed {
const auto y = unbox<double>(y_);
return x - y;
};
};


FOREIGN_END
11 changes: 11 additions & 0 deletions unsafe_coerce/unsafe-coerce.cpp
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

0 comments on commit 9727d27

Please sign in to comment.