fp_library/types/string.rs
1//! Implementations for [`String`].
2//!
3//! This module provides implementations of functional programming traits for the standard library [`String`] type.
4
5use crate::{
6 classes::{monoid::Monoid, semigroup::Semigroup},
7 impl_kind,
8 kinds::*,
9};
10
11impl_kind! {
12 for String {
13 type Of<'a> = String;
14 }
15}
16
17impl Semigroup for String {
18 /// The result of combining two strings.
19 ///
20 /// This method combines two strings into a single string.
21 ///
22 /// ### Type Signature
23 ///
24 /// `Semigroup String => (String, String) -> String`
25 ///
26 /// ### Parameters
27 ///
28 /// * `a`: The first string.
29 /// * `b`: The second string.
30 ///
31 /// ### Returns
32 ///
33 /// The combined string.
34 ///
35 /// ### Examples
36 ///
37 /// ```
38 /// use fp_library::functions::*;
39 ///
40 /// let s1 = "Hello, ".to_string();
41 /// let s2 = "World!".to_string();
42 /// let result = append::<_>(s1, s2);
43 /// assert_eq!(result, "Hello, World!");
44 /// ```
45 fn append(
46 a: Self,
47 b: Self,
48 ) -> Self {
49 a + &b
50 }
51}
52
53impl Monoid for String {
54 /// The identity element.
55 ///
56 /// This method returns the identity element of the monoid.
57 ///
58 /// ### Type Signature
59 ///
60 /// `Monoid String => () -> String`
61 ///
62 /// ### Returns
63 ///
64 /// The identity element.
65 ///
66 /// ### Examples
67 ///
68 /// ```
69 /// use fp_library::functions::*;
70 ///
71 /// let empty_string = empty::<String>();
72 /// assert_eq!(empty_string, "");
73 /// ```
74 fn empty() -> Self {
75 String::new()
76 }
77}
78
79#[cfg(test)]
80mod tests {
81 use crate::classes::{monoid::Monoid, semigroup::append};
82 use quickcheck_macros::quickcheck;
83
84 // Semigroup Laws
85
86 /// Tests the associativity law for Semigroup.
87 #[quickcheck]
88 fn semigroup_associativity(
89 a: String,
90 b: String,
91 c: String,
92 ) -> bool {
93 append(a.clone(), append(b.clone(), c.clone())) == append(append(a, b), c)
94 }
95
96 // Monoid Laws
97
98 /// Tests the left identity law for Monoid.
99 #[quickcheck]
100 fn monoid_left_identity(a: String) -> bool {
101 append(String::empty(), a.clone()) == a
102 }
103
104 /// Tests the right identity law for Monoid.
105 #[quickcheck]
106 fn monoid_right_identity(a: String) -> bool {
107 append(a.clone(), String::empty()) == a
108 }
109}