fp_library/types/
string.rs1#[fp_macros::document_module]
6mod inner {
7 use {
8 crate::{
9 classes::{
10 Monoid,
11 Semigroup,
12 },
13 impl_kind,
14 kinds::*,
15 },
16 fp_macros::*,
17 };
18
19 impl_kind! {
20 for String {
21 type Of<'a> = String;
22 }
23 }
24
25 impl Semigroup for String {
26 #[document_signature]
30 #[document_parameters("The first string.", "The second string.")]
32 #[document_returns("The combined string.")]
34 #[document_examples]
36 fn append(
46 a: Self,
47 b: Self,
48 ) -> Self {
49 a + &b
50 }
51 }
52
53 impl Monoid for String {
54 #[document_signature]
58 #[document_returns("The identity element.")]
60 #[document_examples]
62 fn empty() -> Self {
70 String::new()
71 }
72 }
73}
74
75#[cfg(test)]
76mod tests {
77 use {
78 crate::classes::{
79 monoid::Monoid,
80 semigroup::append,
81 },
82 quickcheck_macros::quickcheck,
83 };
84
85 #[quickcheck]
89 fn semigroup_associativity(
90 a: String,
91 b: String,
92 c: String,
93 ) -> bool {
94 append(a.clone(), append(b.clone(), c.clone())) == append(append(a, b), c)
95 }
96
97 #[quickcheck]
101 fn monoid_left_identity(a: String) -> bool {
102 append(String::empty(), a.clone()) == a
103 }
104
105 #[quickcheck]
107 fn monoid_right_identity(a: String) -> bool {
108 append(a.clone(), String::empty()) == a
109 }
110}