写点什么

ARTS-WEEK5-23.9.18~23.9.24

作者:EchoZhou
  • 2023-09-23
    上海
  • 本文字数:2966 字

    阅读完需:约 10 分钟

Algorithm 一道算法题

买股票的最佳时机,详见https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/description/?envType=study-plan-v2&envId=top-interview-150

function maxProfit(prices: number[]): number {    // 取左最小值,取右最大值 贪心    let low:number = Infinity;    let result:number = 0;    for(let i = 0; i<prices.length; i++) {        low = Math.min(low,prices[i]); // 取左最小值        result = Math.max(result,prices[i] - low); // 最大值    }    return result};
复制代码


Review 读一篇英文文章

Apply Functional Programming Principles(函数式编程的原则) https://97-things-every-x-should-know.gitbooks.io/97-things-every-programmer-should-know/content/en/thing_02/


Functional programming has recently enjoyed renewed interest from the mainstream programming community. Part of the reason is because emergent properties of the functional paradigm are well positioned to address the challenges posed by our industry's shift toward multi-core. However, while that is certainly an important application, it is not the reason this piece admonishes you to know thy functional programming.

函数式编程最近受到了主流编程社区的新一轮关注。部分原因是因为函数式范式的新兴特性很好地适应了我们行业向多核心转变所带来的挑战。然而,虽然这无疑是一个重要的应用领域,但并不是本文提醒你了解函数式编程的唯一原因


Mastery of the functional programming paradigm can greatly improve the quality of the code you write in other contexts. If you deeply understand and apply the functional paradigm, your designs will exhibit a much higher degree of referential transparency.

对函数式编程范式的精通可以极大地提高您在其他情境下编写代码的质量。如果您深刻理解并应用函数式范式,您的设计将表现出更高程度的引用透明度。


Referential transparency is a very desirable property: It implies that functions consistently yield the same results given the same input, irrespective of where and when they are invoked. That is, function evaluation depends less — ideally, not at all — on the side effects of mutable state.

引用透明性是一个非常理想的特性:它意味着在相同的输入情况下,不管在何处和何时调用,函数始终产生相同的结果。换句话说,函数的评估很少依赖于可变状态的副作用,理想情况下甚至不依赖于它们。


A leading cause of defects in imperative code is attributable to mutable variables. Everyone reading this will have investigated why some value is not as expected in a particular situation. Visibility semantics can help to mitigate these insidious defects, or at least to drastically narrow down their location, but their true culprit may in fact be the providence of designs that employ inordinate mutability.

命令式代码中缺陷的一个主要原因是可变变量。阅读此文的每个人都曾经调查过为什么在特定情况下某个值与预期不符。可见性语义可以帮助减轻这些隐蔽的缺陷,或者至少大大缩小它们的位置范围,但实际上真正的罪魁祸首可能是采用了不适度的可变性设计。


And we certainly don't get much help from industry in this regard. Introductions to object orientation tacitly promote such design, because they often show examples composed of graphs of relatively long-lived objects that happily call mutator methods on each other, which can be dangerous. However, with astute test-driven design, particularly when being sure to "Mock Roles, not Objects", unnecessary mutability can be designed away.

在这方面,我们确实没有从业界得到太多帮助。关于面向对象编程的介绍在默许地促进了这种设计,因为它们通常展示由相对长寿的对象图组成的示例,这些对象愉快地互相调用变异器方法,这可能是危险的。然而,通过敏锐的测试驱动设计,尤其是确保“模拟角色而不是对象”,不必要的可变性可以被设计消除。


The net result is a design that typically has better responsibility allocation with more numerous, smaller functions that act on arguments passed into them, rather than referencing mutable member variables. There will be fewer defects, and furthermore they will often be simpler to debug, because it is easier to locate where a rogue value is introduced in these designs than to otherwise deduce the particular context that results in an erroneous assignment. This adds up to a much higher degree of referential transparency, and positively nothing will get these ideas as deeply into your bones as learning a functional programming language, where this model of computation is the norm.

最终的结果是,这种设计通常具有更好的责任分配,具有更多且更小的函数,这些函数作用于传递给它们的参数,而不是引用可变成员变量。缺陷将更少,而且通常更容易调试,因为在这些设计中定位恶意值的引入要比推断导致错误赋值的特定上下文容易得多。这将导致更高程度的引用透明度,学习函数式编程语言将深入你的骨髓,因为在这种计算模型中,这是一种常态。


Of course, this approach is not optimal in all situations. For example, in object-oriented systems this style often yields better results with domain model development (i.e., where collaborations serve to break down the complexity of business rules) than with user-interface development.

当然,这种方法并不在所有情况下都是最佳选择。例如,在面向对象的系统中,这种风格通常在领域模型开发方面(即,协作用于拆分业务规则的复杂性)比用户界面开发方面表现更好。


Master the functional programming paradigm so you are able to judiciously apply the lessons learned to other domains. Your object systems (for one) will resonate with referential transparency goodness and be much closer to their functional counterparts than many would have you believe. In fact, some would even assert that the apex of functional programming and object orientation are merely a reflection of each other, a form of computational yin and yang.

掌握函数式编程范式,这样您就能明智地将所学到的经验应用到其他领域。您的对象系统(其中之一)将充满引用透明性的特性,比许多人所认为的更接近它们的函数式对应物。事实上,一些人甚至会断言,函数式编程和面向对象编程的巅峰只是彼此的反映,一种计算上的阴阳互补。

Technique/Tips 分享一个小技术

vue 组件之间的通信方式有哪些

  • 父子组件

  • prop/$emit/$parent/ref/$attrs(爷孙之间乔接)

  • 兄弟组件

  • $parent/$root/eventbus/vuex/pinia

  • 跨层级关系

  • eventbus/vuex/provide+inject(非响应式)

Share 分享一个观点


Talk 和 Code 同等重要----陈皓


用户头像

EchoZhou

关注

还未添加个人签名 2018-04-24 加入

还未添加个人简介

评论

发布
暂无评论
ARTS-WEEK5-23.9.18~23.9.24_EchoZhou_InfoQ写作社区