// 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"
更多分享
这些都是公司内部的一些分享,逐渐积累后,希望有一天可以在社区、在这个行业分享一些东西。
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.
继续阅读“追回来了”简易优先队列(heap) js 版
这个优先队列使用数组存储。下面的代码是一个最大(不管是现实世界还是计算机的世界,什么是大什么是小,都是可以定义的)堆的实现。
基于堆的优先队列,出队,入队都是 logn 级别的时间复杂度。
提供 heapify 的实现。
继续阅读“简易优先队列(heap) js 版”宝回老家了
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
乱语:这位老哥说在领域模型内,使用布尔值不是很好,得用枚举或者是状态机。
继续阅读“宝回老家了”菜鸟面试官的一点思考
这几个月开始面试,里面就涉及到了怎么面试的问题,有人说你面试能力不行,那我的面试方法真的有问题吗?
继续阅读“菜鸟面试官的一点思考”收到起诉状
2025-05-31
A proof of concept (POC or PoC), also known as proof of principle, is an inchoate realization of a certain idea or method in order to demonstrate its feasibility[1] or viability.[2]
A proof of concept is usually small and may or may not be complete, but aims to demonstrate in principle that the concept has practical potential without needing to fully develop it.
乱语:简单翻译来看,就是对概念的证明。
2025-05-30
流转知何世,江山尚此亭。
登临皆旷士,丧乱有遗经。
已识乾坤大,犹怜草木青。
长空送鸟印,留幻与人灵。
【词语力量354】犹怜草木青。“已识乾坤大,犹怜草木青”,即便是经历世事沉浮、阅尽人间沧桑,当俯下身子看到草木生发……
草木之枯荣于我之人生有何增益?春秋之轮换于我之富贵能添几何?生活之重担已令人不堪负,又有何心力去关心草木是否变青、春花是否再发?
继续阅读“收到起诉状”想了一个面试题:树的过滤
给候选人做的题,应该好理解,如果讲半天题目,对方都不理解,我都怀疑是不是我的表达能力不行。题目足够简单,我讲解题目也省事,对方不理解或者不能做出来,则淘汰就好,我心里也没有负担。
继续阅读“想了一个面试题:树的过滤”