// Generic Monad structure
class Monad {
constructor(value) {
this.value = value;
}
// Bind: chains computations that return a Monad
bind(fn) {
return fn(this.value);
}
// Unit: wraps a value in the Monad
static of(value) {
return new Monad(value);
}
// For debugging
get() {
return this.value;
}
}
// Example: Maybe Monad
class Maybe extends Monad {
bind(fn) {
if (this.value == null) {
return new Maybe(null);
}
return fn(this.value);
}
static of(value) {
return new Maybe(value);
}
getOrElse(defaultValue) {
return this.value == null ? defaultValue : this.value;
}
}
// Demo: chaining computations with Monad
const double = x => Monad.of(x * 2);
const increment = x => Monad.of(x + 1);
const result = Monad.of(5)
.bind(double) // 10
.bind(increment); // 11
console.log(result.get()); // Output: 11
// Demo: chaining with Maybe Monad
const safeDivide = x => x === 0 ? Maybe.of(null) : Maybe.of(10 / x);
const maybeResult = Maybe.of(2)
.bind(safeDivide) // 10 / 2 = 5
.bind(x => Maybe.of(x + 3)); // 8
console.log(maybeResult.getOrElse("fail")); // Output: 8
const maybeFail = Maybe.of(0)
.bind(safeDivide) // null
.bind(x => Maybe.of(x + 3));
console.log(maybeFail.getOrElse("fail")); // Output: "fail"
月度归档: 2025 年 8 月
maybe functor
class Maybe {
constructor(value) {
this.value = value;
}
// Functor map: applies a function if value is not null/undefined
map(fn) {
if (this.value === null) {
return new Maybe(null);
}
return new Maybe(fn(this.value));
}
// getOrElse: safely extract value with a fallback
getOrElse(defaultValue) {
return this.value === null ? defaultValue : this.value;
}
// static helpers for convenience
static of(value) {
return new Maybe(value);
}
static nothing() {
return new Maybe(null);
}
}
// Example usage:
const maybeNumber = Maybe.of(5)
.map(n => n * 2)
.map(n => n + 1);
console.log(maybeNumber.getOrElse(0)); // Output: 11
const maybeNull = Maybe.of(null)
.map(n => n * 2);
console.log(maybeNull.getOrElse(0)); // Output: 0
想了一个简单的面试题
我们有一个数组 ` [5,1,6] `,我们要生成所有的子组合 ` [ [ 5 ], [ 5, 1 ], [ 5, 1, 6 ], [ 5, 6 ], [ 1 ], [ 1, 6 ], [ 6 ] ] `,针对任意数组,该如何求出它的所有子组合呢?
继续阅读“想了一个简单的面试题”追回来了
2025-07-31
大端序,小端序
Examples with the number 0x12345678 (i.e., 305 419 896 in decimal):
little-endian: 0x78 0x56 0x34 0x12
big-endian: 0x12 0x34 0x56 0x78
mixed-endian (historic and very rare): 0x34 0x12 0x78 0x56
https://developer.mozilla.org/en-US/docs/Glossary/Endianness
2025-07-30
zig 这门语言似乎是和 rust 产生竞争,抢占 c 语言的市场
The primary goal of Zig is to be a better solution to the sorts of tasks that are currently solved with C. A primary concern in that respect is readability; Zig attempts to use existing concepts and syntax wherever possible, avoiding the addition of different syntax for similar concepts. Further, it is designed for “robustness, optimality and maintainability”, including a variety of features to improve safety, optimization, and testing.
继续阅读“追回来了”宝回老家了
2025-06-30
2025-06-26
“The boolean trap” is just one example of how seemingly simple modeling decisions can have unexpected consequences as systems grow. While booleans are perfect for their intended purpose – representing true/false technical states – they often fall short when applied to a domain. By choosing enums and enum sets we create code that is better prepared for the “real world”‘ of the problem domain.
Sometimes it’s better to rethink how we represent the state instead of blindly adding another boolean flag.
https://katafrakt.me/2024/11/09/booleans-are-a-trap
乱语:这位老哥说在领域模型内,使用布尔值不是很好,得用枚举或者是状态机。
继续阅读“宝回老家了”菜鸟面试官的一点思考
这几个月开始面试,里面就涉及到了怎么面试的问题,有人说你面试能力不行,那我的面试方法真的有问题吗?
继续阅读“菜鸟面试官的一点思考”