Skip to content

Commit

Permalink
Rename sculpted_convert_from into transform_from (#45)
Browse files Browse the repository at this point in the history
* Rename sculpted_convert_from into transform_from

- Shorter and conveys the idea of transforming, like Transformers :p
- Deprecate the old method

* Add example of LabelledGeneric conversion in module

* Module-level example for Generic as well

* More docs

* Bump core to 0.0.11

* Release Frunk 0.1.21

* Publish derives for core 0.0.11

* Upgrade Frunk for new derives

* Update core dev derive version

* Add docs badge
  • Loading branch information
lloydmeta committed Mar 22, 2017
1 parent 28f1dc8 commit 25ec4b2
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 100 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "frunk"
version = "0.1.20"
version = "0.1.22"
authors = ["Lloyd <lloydmeta@gmail.com>"]
description = "Frunk provides developers with a number of functional programming tools like HList, Generic, LabelledGeneric, Validated, Monoid, Semigroup and friends."
license = "MIT"
Expand All @@ -16,8 +16,8 @@ time = "0.1.36"

[dependencies.frunk_core]
path = "core"
version = "0.0.10"
version = "0.0.11"

[dependencies.frunk_derives]
path = "derives"
version = "0.0.11"
version = "0.0.12"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Frunk [![Crates.io](https://img.shields.io/crates/v/frunk.svg)](https://crates.io/crates/frunk) [![Build Status](https://travis-ci.org/lloydmeta/frunk.svg?branch=master)](https://travis-ci.org/lloydmeta/frunk) [![Gitter](https://badges.gitter.im/lloydmeta/frunk.svg)](https://gitter.im/lloydmeta/frunk?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
# Frunk [![Crates.io](https://img.shields.io/crates/v/frunk.svg)](https://crates.io/crates/frunk) [![Build Status](https://travis-ci.org/lloydmeta/frunk.svg?branch=master)](https://travis-ci.org/lloydmeta/frunk) [![Gitter](https://badges.gitter.im/lloydmeta/frunk.svg)](https://gitter.im/lloydmeta/frunk?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) [![Frunk](https://docs.rs/frunk/badge.svg)](https://docs.rs/frunk)

> **frunk** *frəNGk*
> * Functional programming toolbelt in Rust.
Expand Down Expand Up @@ -264,7 +264,7 @@ let d_user = <DeletedUser as LabelledGeneric>::convert_from(s_user);

// This will, however, work, because we make use of the Sculptor type-class
// to type-safely reshape the representations to align/match each other.
let d_user: DeletedUser = sculpted_convert_from(s_user);
let d_user: DeletedUser = transform_from(s_user);
```

For more information how Generic and Field work, check out their respective Rustdocs:
Expand Down
12 changes: 6 additions & 6 deletions benches/hlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ fn hlist_into_tuple2(b: &mut Bencher) {
fn hlist_into_tuple2_match(b: &mut Bencher) {
let h = hlist![1, 2, 3.3f32, "hi2", true];
b.iter(|| {
let (a, (b, (c, (d, e)))) = h.into_tuple2();
(a, b, c, d, e)
})
let (a, (b, (c, (d, e)))) = h.into_tuple2();
(a, b, c, d, e)
})
}


#[bench]
fn hlist_into_hlist_pat_match(b: &mut Bencher) {
let h = hlist![1, 2, 3.3f32, "hi2", true];
b.iter(|| {
let hlist_pat!(a, b, c, d, e) = h;
(a, b, c, d, e)
})
let hlist_pat!(a, b, c, d, e) = h;
(a, b, c, d, e)
})
}

#[bench]
Expand Down
2 changes: 1 addition & 1 deletion benches/labelled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn sculpted_conversion(b: &mut Bencher) {
last_name: "Schmoe",
age: 30,
};
<JumbledUser as LabelledGeneric>::sculpted_convert_from(n_u)
<JumbledUser as LabelledGeneric>::transform_from(n_u)
})
}

Expand Down
7 changes: 4 additions & 3 deletions benches/monoid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ fn std_add_all_i32(b: &mut Bencher) {
let v = vec![Some(1), Some(2), Some(3), Some(4), Some(5), Some(6), Some(7), Some(8), Some(9),
Some(10)];
b.iter(|| {
v.iter().fold(Some(0),
|maybe_acc, maybe_n| maybe_acc.and_then(|acc| maybe_n.map(|n| acc + n)))
})
v.iter().fold(Some(0), |maybe_acc, maybe_n| {
maybe_acc.and_then(|acc| maybe_n.map(|n| acc + n))
})
})
}
4 changes: 2 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "frunk_core"
version = "0.0.10"
version = "0.0.11"
authors = ["Lloyd <lloydmeta@gmail.com>"]
description = "Frunk core provides developers with HList and Generic"
license = "MIT"
Expand All @@ -13,4 +13,4 @@ travis-ci = { repository = "lloydmeta/frunk" }

[dev-dependencies.frunk_derives]
path = "../derives"
version = "0.0.11"
version = "0.0.12"
33 changes: 32 additions & 1 deletion core/src/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,37 @@
//!
//! It contains the Generic typeclass and some helper methods for using the Generic type class
//! without having to use universal function call syntax.
//!
//! # Examples
//!
//! ```rust
//! # #[allow(unused_imports)]
//! # #[macro_use] extern crate frunk_derives;
//! # #[macro_use] extern crate frunk_core;
//! # use frunk_core::hlist::*; fn main() {
//! # use frunk_core::hlist::*;
//! # use frunk_core::generic::*;
//! #[derive(Generic)]
//! struct ApiPerson<'a> {
//! FirstName: &'a str,
//! LastName: &'a str,
//! Age: usize,
//! }
//!
//! #[derive(Generic)]
//! struct DomainPerson<'a> {
//! first_name: &'a str,
//! last_name: &'a str,
//! age: usize,
//! }
//!
//! let a_person = ApiPerson {
//! FirstName: "Joe",
//! LastName: "Blow",
//! Age: 30,
//! };
//! let d_person: DomainPerson = convert_from(a_person); // done
//! # }

/// A trait that converts from a type to a generic representation
///
Expand All @@ -11,7 +42,7 @@
/// # Examples
///
/// ```rust
/// #[allow(unused_imports)]
/// # #[allow(unused_imports)]
/// # #[macro_use] extern crate frunk_derives;
/// # #[macro_use] extern crate frunk_core;
/// # use frunk_core::hlist::*; fn main() {
Expand Down
45 changes: 23 additions & 22 deletions core/src/hlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,23 +438,23 @@ impl<Source> Sculptor<HNil, HNil> for Source {
/// Indices is HCons<IndexHead, IndexTail> here because the compiler is being asked to figure out the
/// Index for Plucking the first item of type THead out of Self and the rest (IndexTail) is for the
/// Plucker's remainder induce.
impl<THead, TTail, SHead, STail, IndexHead, IndexTail> Sculptor<HCons<THead, TTail>, HCons<IndexHead, IndexTail>>
for HCons<SHead, STail>
where
HCons<SHead, STail>: Plucker<THead, IndexHead>,
<HCons<SHead, STail> as Plucker<THead, IndexHead>>::Remainder: Sculptor<TTail, IndexTail> {
impl<THead, TTail, SHead, STail, IndexHead, IndexTail> Sculptor<HCons<THead, TTail>,
HCons<IndexHead, IndexTail>>
for HCons<SHead, STail>
where HCons<SHead, STail>: Plucker<THead, IndexHead>,
<HCons<SHead, STail> as Plucker<THead, IndexHead>>::Remainder: Sculptor<TTail, IndexTail>
{
type Remainder = <<HCons<SHead, STail> as Plucker<THead, IndexHead>>::Remainder as Sculptor<TTail, IndexTail>>::Remainder;

fn sculpt(self) -> (HCons<THead, TTail>, Self::Remainder) {
let (p, r): (THead, <HCons<SHead, STail> as Plucker<THead, IndexHead>>::Remainder) = self.pluck();
let (p, r): (THead, <HCons<SHead, STail> as Plucker<THead, IndexHead>>::Remainder) =
self.pluck();
let (tail, tail_remainder): (TTail, Self::Remainder) = r.sculpt();
(
HCons {
head: p,
tail: tail
},
tail_remainder
)
(HCons {
head: p,
tail: tail,
},
tail_remainder)
}
}

Expand Down Expand Up @@ -497,10 +497,10 @@ impl<H, Tail> IntoReverse for HCons<H, Tail>

fn into_reverse(self) -> Self::Output {
self.tail.into_reverse() +
HCons {
head: self.head,
tail: HNil,
}
HCons {
head: self.head,
tail: HNil,
}
}
}

Expand Down Expand Up @@ -600,10 +600,11 @@ impl<F, Init> HFoldRightable<F, Init> for HNil {
}
}

impl<F, FolderHeadR, FolderTail, H, Tail, Init> HFoldRightable<HCons<F, FolderTail>, Init> for HCons<H, Tail>
where
Tail: HFoldRightable<FolderTail, Init>,
F: FnOnce(H, <Tail as HFoldRightable<FolderTail, Init>>::Output) -> FolderHeadR {
impl<F, FolderHeadR, FolderTail, H, Tail, Init> HFoldRightable<HCons<F, FolderTail>, Init>
for HCons<H, Tail>
where Tail: HFoldRightable<FolderTail, Init>,
F: FnOnce(H, <Tail as HFoldRightable<FolderTail, Init>>::Output) -> FolderHeadR
{
type Output = FolderHeadR;

fn foldr(self, folder: HCons<F, FolderTail>, init: Init) -> Self::Output {
Expand Down Expand Up @@ -653,7 +654,7 @@ impl<F, Acc> HFoldLeftable<F, Acc> for HNil {
}

impl<F, FolderHeadR, FolderTail, H, Tail, Acc> HFoldLeftable<HCons<F, FolderTail>, Acc>
for HCons<H, Tail>
for HCons<H, Tail>
where Tail: HFoldLeftable<FolderTail, FolderHeadR>,
F: FnOnce(Acc, H) -> FolderHeadR
{
Expand Down
Loading

0 comments on commit 25ec4b2

Please sign in to comment.