Skip to content

Commit

Permalink
init js polars; #83
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Apr 14, 2021
1 parent 09a2a11 commit 057a2da
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 3 deletions.
7 changes: 7 additions & 0 deletions js-polars/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/target
**/*.rs.bk
Cargo.lock
bin/
pkg/
node_modules/
wasm-pack.log
37 changes: 37 additions & 0 deletions js-polars/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "polars"
version = "0.0.1"
authors = ["Ritchie Vink"]
edition = "2018"
repository = "https://github.com/ritchie46/polars"
license = "MIT"
documentation = "https://ritchie46.github.io/polars-book/"
homepage = "https://github.com/ritchie46/polars"

[lib]
crate-type = ["cdylib", "rlib"]

[features]

[dependencies]
wasm-bindgen = "0.2.34"

wee_alloc = { version = "0.4.5" }

[dependencies.polars-core]
path = "../polars/polars-core"
default-features = false
features = [
"strings",
"temporal",
"performant",
]

[dev-dependencies]
wasm-bindgen-test = "0.3.13"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

[workspace]
6 changes: 6 additions & 0 deletions js-polars/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

build-dev:
wasm-pack build --dev --target nodejs

build-prod:
wasm-pack build --target nodejs
6 changes: 6 additions & 0 deletions js-polars/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const pl = require("./pkg")
const assert = require("assert")

let s = new pl.Series("a", [1, 2, 3])
assert(s.mean() === 2)
console.log(s.mean())
5 changes: 5 additions & 0 deletions js-polars/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod series;
use wasm_bindgen::prelude::*;

#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
104 changes: 104 additions & 0 deletions js-polars/src/series.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use polars_core::prelude::{
Series as PSeries, Utf8Chunked, NewChunkedArray, Float64Chunked, IntoSeries
};
use std::ops::{BitAnd, BitOr};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct Series {
series: PSeries,
}

impl From<PSeries> for Series {
fn from(series: PSeries) -> Self {
Self { series }
}
}

#[wasm_bindgen]
impl Series {
#[wasm_bindgen(constructor)]
pub fn new(name: &str, values: Box<[JsValue]>) -> Series {
let first = &values[0];
if first.as_f64().is_some() {
let series = Float64Chunked::new_from_opt_iter(name, values.iter().map(|v| v.as_f64())).into_series();
Series {
series
}
} else if first.as_string().is_some() {
let series = Utf8Chunked::new_from_opt_iter(name, values.iter().map(|v| v.as_string())).into_series();
Series {
series
}
} else {
unimplemented!()
}
}

pub fn rechunk(&mut self, in_place: bool) -> Option<Series> {
let series = self.series.rechunk();
if in_place {
self.series = series;
None
} else {
Some(series.into())
}
}

pub fn bitand(&self, other: &Series) -> Self {
let s = self
.series
.bool()
.expect("boolean")
.bitand(other.series.bool().expect("boolean"))
.into_series();
s.into()
}

pub fn bitor(&self, other: &Series) -> Self {
let s = self
.series
.bool()
.expect("boolean")
.bitor(other.series.bool().expect("boolean"))
.into_series();
s.into()
}

pub fn cum_sum(&self, reverse: bool) -> Self {
self.series.cum_sum(reverse).into()
}

pub fn cum_max(&self, reverse: bool) -> Self {
self.series.cum_max(reverse).into()
}

pub fn cum_min(&self, reverse: bool) -> Self {
self.series.cum_min(reverse).into()
}

pub fn chunk_lengths(&self) -> Vec<usize> {
self.series.chunk_lengths().clone()
}

pub fn name(&self) -> String {
self.series.name().into()
}

pub fn rename(&mut self, name: &str) {
self.series.rename(name);
}

pub fn mean(&self) -> Option<f64> {
self.series.mean()
}

pub fn n_chunks(&self) -> usize {
self.series.n_chunks()
}

pub fn limit(&self, num_elements: usize) -> Self {
let series = self.series.limit(num_elements);
series.into()
}
}
13 changes: 13 additions & 0 deletions js-polars/tests/web.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! Test suite for the Web and headless browsers.

#![cfg(target_arch = "wasm32")]

extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
fn pass() {
assert_eq!(1 + 1, 2);
}
4 changes: 2 additions & 2 deletions py-polars/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions py-polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ features = [
"random",
"object",
"ipc",
"csv-file",
"pretty_fmt",
"mimalloc",
"performant",
Expand Down
2 changes: 1 addition & 1 deletion py-polars/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl PySeries {
self.series = series;
None
} else {
Some(PySeries::new(series))
Some(series.into())
}
}

Expand Down

0 comments on commit 057a2da

Please sign in to comment.