fp-library 0.17.0

A functional programming library for Rust featuring your favourite higher-kinded types and type classes.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! The `Const` functor, which ignores its second type parameter. The corresponding brand is [`ConstBrand`](crate::brands::ConstBrand).

#[fp_macros::document_module]
mod inner {
	use {
		crate::{
			Apply,
			brands::ConstBrand,
			classes::{
				apply_first::ApplyFirst,
				apply_second::ApplySecond,
				clone_fn::CloneFn,
				functor::Functor,
				lift::Lift,
				monoid::Monoid,
				pointed::Pointed,
				semiapplicative::Semiapplicative,
				semigroup::Semigroup,
			},
			impl_kind,
			kinds::*,
		},
		fp_macros::*,
		std::marker::PhantomData,
	};

	/// The `Const` functor.
	///
	/// `Const<R, A>` stores a value of type `R` and ignores the type `A`.
	#[document_type_parameters(
		"The lifetime of the values.",
		"The stored type.",
		"The ignored type."
	)]
	#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
	#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
	pub struct Const<'a, R, A>(pub R, pub PhantomData<&'a A>);

	#[document_type_parameters(
		"The lifetime of the values.",
		"The stored type.",
		"The ignored type."
	)]
	#[document_parameters("The `Const` instance.")]
	impl<'a, R, A> Const<'a, R, A> {
		/// Creates a new `Const` instance.
		#[document_signature]
		#[document_parameters("The value to store.")]
		#[document_returns("A new `Const` instance.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::const_val::Const;
		///
		/// let c: Const<i32, String> = Const::new(42);
		/// assert_eq!(c.0, 42);
		/// ```
		pub fn new(r: R) -> Self {
			Const(r, PhantomData)
		}

		/// Maps over the phantom type parameter, preserving the stored value.
		///
		/// Since `Const` ignores its second type parameter, the function is never called.
		/// This is the inherent method form of [`Functor::map`](crate::classes::functor::Functor::map).
		#[document_signature]
		#[document_type_parameters("The new phantom type.")]
		#[document_parameters("The function to map (ignored).")]
		#[document_returns(
			"A new `Const` instance with the same stored value but a different phantom type."
		)]
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::const_val::Const;
		///
		/// let c: Const<i32, String> = Const::new(42);
		/// let mapped: Const<i32, bool> = c.map(|s: String| s.is_empty());
		/// assert_eq!(mapped.0, 42);
		/// ```
		pub fn map<B>(
			self,
			_f: impl FnOnce(A) -> B,
		) -> Const<'a, R, B> {
			Const::new(self.0)
		}

		/// Combines two `Const` values by appending their stored values, discarding the phantom types.
		///
		/// This is the inherent method form of [`Lift::lift2`](crate::classes::lift::Lift::lift2).
		#[document_signature]
		#[document_type_parameters("The second phantom type.", "The result phantom type.")]
		#[document_parameters("The other `Const` instance.", "The function to lift (ignored).")]
		#[document_returns("A new `Const` instance with the appended stored values.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::const_val::Const;
		///
		/// let c1: Const<String, i32> = Const::new("Hello".to_string());
		/// let c2: Const<String, i32> = Const::new(" World".to_string());
		/// let lifted = c1.lift2(c2, |a: i32, b: i32| a + b);
		/// assert_eq!(lifted.0, "Hello World");
		/// ```
		pub fn lift2<B, C>(
			self,
			other: Const<'a, R, B>,
			_f: impl FnOnce(A, B) -> C,
		) -> Const<'a, R, C>
		where
			R: Semigroup, {
			Const::new(R::append(self.0, other.0))
		}

		/// Combines the stored values of two `Const` instances, keeping the phantom type of the first.
		///
		/// This is the inherent method form of [`ApplyFirst::apply_first`](crate::classes::apply_first::ApplyFirst::apply_first).
		#[document_signature]
		#[document_type_parameters("The phantom type of the second `Const` instance.")]
		#[document_parameters("The second `Const` instance.")]
		#[document_returns("A new `Const` instance with the appended stored values.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::const_val::Const;
		///
		/// let c1: Const<String, i32> = Const::new("Hello".to_string());
		/// let c2: Const<String, bool> = Const::new(" World".to_string());
		/// let result = c1.apply_first(c2);
		/// assert_eq!(result.0, "Hello World");
		/// ```
		pub fn apply_first<B>(
			self,
			other: Const<'a, R, B>,
		) -> Const<'a, R, A>
		where
			R: Semigroup, {
			Const::new(R::append(self.0, other.0))
		}

		/// Combines the stored values of two `Const` instances, keeping the phantom type of the second.
		///
		/// This is the inherent method form of [`ApplySecond::apply_second`](crate::classes::apply_second::ApplySecond::apply_second).
		#[document_signature]
		#[document_type_parameters("The phantom type of the second `Const` instance.")]
		#[document_parameters("The second `Const` instance.")]
		#[document_returns("A new `Const` instance with the appended stored values.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::const_val::Const;
		///
		/// let c1: Const<String, i32> = Const::new("Hello".to_string());
		/// let c2: Const<String, bool> = Const::new(" World".to_string());
		/// let result = c1.apply_second(c2);
		/// assert_eq!(result.0, "Hello World");
		/// ```
		pub fn apply_second<B>(
			self,
			other: Const<'a, R, B>,
		) -> Const<'a, R, B>
		where
			R: Semigroup, {
			Const::new(R::append(self.0, other.0))
		}

		/// Creates a `Const` with the monoidal identity, ignoring the given value.
		///
		/// This is the inherent method form of [`Pointed::pure`](crate::classes::pointed::Pointed::pure).
		#[document_signature]
		#[document_parameters("The value to wrap (ignored).")]
		#[document_returns("A new `Const` instance with the empty value of the stored type.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::const_val::Const;
		///
		/// let c: Const<String, i32> = Const::pure(42);
		/// assert_eq!(c.0, "".to_string());
		/// ```
		pub fn pure(_a: A) -> Self
		where
			R: Monoid, {
			Const::new(R::empty())
		}
	}

	impl_kind! {
		impl<R: 'static> for ConstBrand<R> {
			type Of<'a, A: 'a>: 'a = Const<'a, R, A>;
		}
	}

	#[document_type_parameters("The stored type.")]
	impl<R: 'static> Functor for ConstBrand<R> {
		#[document_signature]
		#[document_type_parameters(
			"The lifetime of the values.",
			"The input type.",
			"The output type."
		)]
		#[document_parameters(
			"The function to map (ignored).",
			"The `Const` instance to map over."
		)]
		#[document_returns("A new `Const` instance with the same stored value.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::ConstBrand,
		/// 	classes::functor::Functor,
		/// 	types::const_val::Const,
		/// };
		///
		/// let c: Const<i32, String> = Const::new(42);
		/// let mapped = ConstBrand::map(|s: String| s.len(), c);
		/// assert_eq!(mapped.0, 42);
		/// ```
		fn map<'a, A: 'a, B: 'a>(
			_f: impl Fn(A) -> B + 'a,
			fa: Apply!(<Self as Kind!( type Of<'b, T: 'b>: 'b; )>::Of<'a, A>),
		) -> Apply!(<Self as Kind!( type Of<'b, T: 'b>: 'b; )>::Of<'a, B>) {
			fa.map(_f)
		}
	}

	#[document_type_parameters("The stored type.")]
	impl<R: 'static + Semigroup> Lift for ConstBrand<R> {
		#[document_signature]
		#[document_type_parameters(
			"The lifetime of the values.",
			"The first input type.",
			"The second input type.",
			"The output type."
		)]
		#[document_parameters(
			"The function to lift (ignored).",
			"The first `Const` instance.",
			"The second `Const` instance."
		)]
		#[document_returns("A new `Const` instance with the combined stored values.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::ConstBrand,
		/// 	classes::lift::Lift,
		/// 	types::const_val::Const,
		/// };
		///
		/// let c1: Const<String, i32> = Const::new("Hello".to_string());
		/// let c2: Const<String, i32> = Const::new(" World".to_string());
		/// let lifted = ConstBrand::lift2(|a: i32, b: i32| a + b, c1, c2);
		/// assert_eq!(lifted.0, "Hello World");
		/// ```
		fn lift2<'a, A, B, C>(
			_func: impl Fn(A, B) -> C + 'a,
			fa: Apply!(<Self as Kind!( type Of<'b, T: 'b>: 'b; )>::Of<'a, A>),
			fb: Apply!(<Self as Kind!( type Of<'b, T: 'b>: 'b; )>::Of<'a, B>),
		) -> Apply!(<Self as Kind!( type Of<'b, T: 'b>: 'b; )>::Of<'a, C>)
		where
			A: Clone + 'a,
			B: Clone + 'a,
			C: 'a, {
			fa.lift2(fb, _func)
		}
	}

	#[document_type_parameters("The stored type.")]
	impl<R: 'static + Semigroup> Semiapplicative for ConstBrand<R> {
		#[document_signature]
		#[document_type_parameters(
			"The lifetime of the values.",
			"The function brand.",
			"The input type.",
			"The output type."
		)]
		#[document_parameters(
			"The `Const` instance containing a function.",
			"The `Const` instance containing a value."
		)]
		#[document_returns("A new `Const` instance with the combined stored values.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::{
		/// 		ConstBrand,
		/// 		RcFnBrand,
		/// 	},
		/// 	classes::{
		/// 		clone_fn::CloneFn,
		/// 		semiapplicative::Semiapplicative,
		/// 	},
		/// 	types::const_val::Const,
		/// };
		///
		/// let c1 = Const::<String, _>::new("Hello".to_string());
		/// let c2 = Const::<String, i32>::new(" World".to_string());
		/// let applied = ConstBrand::<String>::apply::<RcFnBrand, i32, i32>(c1, c2);
		/// assert_eq!(applied.0, "Hello World");
		/// ```
		fn apply<'a, FnBrand: 'a + CloneFn, A: 'a + Clone, B: 'a>(
			ff: Apply!(<Self as Kind!( type Of<'b, T: 'b>: 'b; )>::Of<'a, <FnBrand as CloneFn>::Of<'a, A, B>>),
			fa: Apply!(<Self as Kind!( type Of<'b, T: 'b>: 'b; )>::Of<'a, A>),
		) -> Apply!(<Self as Kind!( type Of<'b, T: 'b>: 'b; )>::Of<'a, B>) {
			Const::new(R::append(ff.0, fa.0))
		}
	}

	#[document_type_parameters("The stored type.")]
	impl<R: 'static + Semigroup> ApplyFirst for ConstBrand<R> {
		#[document_signature]
		#[document_type_parameters(
			"The lifetime of the values.",
			"The first type.",
			"The second type."
		)]
		#[document_parameters("The first `Const` instance.", "The second `Const` instance.")]
		#[document_returns("A new `Const` instance with the combined stored values.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::ConstBrand,
		/// 	classes::apply_first::ApplyFirst,
		/// 	types::const_val::Const,
		/// };
		///
		/// let c1: Const<String, i32> = Const::new("Hello".to_string());
		/// let c2: Const<String, i32> = Const::new(" World".to_string());
		/// let applied = ConstBrand::apply_first(c1, c2);
		/// assert_eq!(applied.0, "Hello World");
		/// ```
		fn apply_first<'a, A: 'a, B: 'a>(
			fa: Apply!(<Self as Kind!( type Of<'b, T: 'b>: 'b; )>::Of<'a, A>),
			fb: Apply!(<Self as Kind!( type Of<'b, T: 'b>: 'b; )>::Of<'a, B>),
		) -> Apply!(<Self as Kind!( type Of<'b, T: 'b>: 'b; )>::Of<'a, A>) {
			fa.apply_first(fb)
		}
	}

	#[document_type_parameters("The stored type.")]
	impl<R: 'static + Semigroup> ApplySecond for ConstBrand<R> {
		#[document_signature]
		#[document_type_parameters(
			"The lifetime of the values.",
			"The first type.",
			"The second type."
		)]
		#[document_parameters("The first `Const` instance.", "The second `Const` instance.")]
		#[document_returns("A new `Const` instance with the combined stored values.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::ConstBrand,
		/// 	classes::apply_second::ApplySecond,
		/// 	types::const_val::Const,
		/// };
		///
		/// let c1: Const<String, i32> = Const::new("Hello".to_string());
		/// let c2: Const<String, i32> = Const::new(" World".to_string());
		/// let applied = ConstBrand::apply_second(c1, c2);
		/// assert_eq!(applied.0, "Hello World");
		/// ```
		fn apply_second<'a, A: 'a, B: 'a>(
			fa: Apply!(<Self as Kind!( type Of<'b, T: 'b>: 'b; )>::Of<'a, A>),
			fb: Apply!(<Self as Kind!( type Of<'b, T: 'b>: 'b; )>::Of<'a, B>),
		) -> Apply!(<Self as Kind!( type Of<'b, T: 'b>: 'b; )>::Of<'a, B>) {
			fa.apply_second(fb)
		}
	}

	#[document_type_parameters("The stored type.")]
	impl<R: 'static + Monoid> Pointed for ConstBrand<R> {
		#[document_signature]
		#[document_type_parameters("The lifetime of the values.", "The type to wrap (ignored).")]
		#[document_parameters("The value to wrap (ignored).")]
		#[document_returns("A new `Const` instance with the empty value of the stored type.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::ConstBrand,
		/// 	classes::pointed::Pointed,
		/// 	types::const_val::Const,
		/// };
		///
		/// let c: Const<String, i32> = ConstBrand::pure(42);
		/// assert_eq!(c.0, "".to_string());
		/// ```
		fn pure<'a, A: 'a>(_a: A) -> Apply!(<Self as Kind!( type Of<'b, T: 'b>: 'b; )>::Of<'a, A>) {
			Const::pure(_a)
		}
	}
}
pub use inner::*;

// `A` is only `PhantomData<&'a A>` - always Clone/Copy - so we only need `R: Clone`/`R: Copy`.
impl<'a, R: Clone, A> Clone for Const<'a, R, A> {
	fn clone(&self) -> Self {
		Const(self.0.clone(), std::marker::PhantomData)
	}
}
impl<'a, R: Copy, A> Copy for Const<'a, R, A> {}