Function Composition vs Object Composition
In functional programming, we create large functions by composing small functions; in object-oriented programming, we create large objects by composing small objects. They are different types of composition. They might sound similar, but there is something fundamentally different. Let me explain.
In the function world, there is a compose operator, which works for all functions. Say you have 2 functions, one converts an epoch represented by a long integer to a date, the other converts a date to its textual representation.
epochToDate :: Long -> Date
dateToString :: Date -> String
If you want to convert an epoch to the textual representation of its corresponding date, you just compose the 2 functions:
epochToString :: Long -> String
epochToString = compose(epochToDate, dateToString)
The point is that the compose operator (an higher-order function) is always there to serve. It is one works for all. When you write a new function, you automatically get the nice compose operator. With an analogy, think Unix pipe. If you write your program to get input from STDIN and print your output to STDOUT, you automatically get the nice pipe operator | which allows your program to work seamlessly with other programs in that ecosystem.
Is there such a thing in the object world? Not! You have to write your own code to express the composition, there is no one general compose operator which works for all object compositions.
In the function world, there are much more nice operators than the compose operator for you to leverage: functors, monads, monad transformer, a whole bunch of general operators at your disposal. Say we need a function which converts a list of epoch long integers to a list of textual representation of date.
epochListToStringList :: List<Long> -> List<String>
Do you need to implement this function with a for loop? No! Use List functor:
epochListToStringList = compose(List.map(epochToDate), List.map(dateToString))
List
is a functor whose map
function turns any function of type T -> U
into another function of type List<T> -> List<U>
. This is functor, mapping one category to another, the 2 categories are isomorphic. Is there such a nice thing in the object world? Not, again!
As you can see, functions are born in a harmonious ecosystem. Whenever you write a function, you automatically enjoy compose, functors, monads, monad transformers, bla bla...
Function Composition vs Object Composition的更多相关文章
- 如何解决Selenium中"Cannot find function addEventListener in object [object HTMLDocument]"的错误
project: blog target: how-to-resolve-cannot-find-function-addEventListener-error-in-selenium.md stat ...
- 为什么Object.prototype在Function的原型链上与Function.prototype在Object的原型链上都为true
关于javascript的原型链有一个问题我一直很疑惑:为什么 Function instanceof Object 与 Object instanceof Function都为true呢? Func ...
- Javascript中的函数(Function)与对象(Object)的关系
今天我们来尝试理解Function和Object.因为这个里面有些人前期可能会搞糊涂.他们之间到底是什么关系.当然也不除外当初的我. 注意:官方定义: 在Javascript中,每一个函数实际上都是一 ...
- JavaScript中Function函数与Object对象的关系
函数对象和其他内部对象的关系 除了函数对象,还有很多内部对象,比如:Object.Array.Date.RegExp.Math.Error.这些名称实际上表示一个 类型,可以通过new操作符返回一个对 ...
- Mybatis-Plus的Service方法使用 之 泛型方法default <V> List<V> listObjs(Function<? super Object, V> mapper)
首先 我们先看到的这个方法入参是:Function<? super Object , V> mapper ,这是jdk1.8为了统一简化书写格式引进的函数式接口 . 简单 解释一下我对Fu ...
- Functional Programming without Lambda - Part 1 Functional Composition
Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...
- UML中关联(Association)、聚合(Aggregation)和合成(Composition)之间的区别
本文为 Dennis Gao 原创技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载. 现在,我们需要设计一个项目管理系统,目前我们收集到了如下这些需求: REQ1:一个项目内有多名项目成 ...
- [Redux] Composition with Objects
For example, current we have those todos: { todos: [ { completed: true, id: 0, text: "Learn Red ...
- Windows Composition API 指南 - 认识 Composition API
微软在 Windows 10中 面向通用 Windows 应用 (Universal Windows Apps, UWA) 新引入了一套用于用户界面合成的 API:Composition API.Co ...
随机推荐
- 在windows下使用jenkins部署docker容器
在windows下使用jenkins部署docker容器最近在学习jenkins,docker部署来实现集成部署,所以想在windows下面实现测试,但是发现在windows下docker支持不是很好 ...
- 关于.idea未上传导致不能显示项目文件结构的问题
方法一: 关闭IDEA, 然后删除项目文件夹下的.idea文件夹 重新用IDEA工具打开项目 方法二: 菜单:File -> Invalidate Caches / Restart 方法三: 配 ...
- H5真机调试
为什么要做H5真机调试? 第一,样式调试.浏览器的效果和真机上的效果不一定相同,没有真机调试,我们都是先上传到服务器,然后再用手机访问,显然对于样式调试来说,这样效率很低. 第二,调用到底层.真机才拥 ...
- Docker代理设置方法
1.注意Docker版本(此处版本为docker-ce-18.06.1) docker version 2.编辑Docker服务配置文件 vim /usr/lib/systemd/system/doc ...
- Kubernetes集群升级(kubeadm升级方式)
1.升级前的版本确认(相同的大版本号下的小版本升级还是跨版本升级) 例如:从1.12.0升级到1.12.7 或者 从1.12.7升级到1.13.0 2.配置kubernetes安装源(已配置kuber ...
- skynet记录7:服务(c和lua)
稍后填坑 1.c服务的写法(第一个服务logger分析) 2.lua服务的写法(第二个服务bootstrap分析) 3.snlua包装模块
- IP报文头详解
IPv4报头: 报头长度:20-60字节bytes 白色部分为固定头部部分(20 bytes),绿色option选项部分为可选部分. 固定头部大小计算: 4bit + 4bit + 8bit + 16 ...
- oo第12次作业
一.规格化设计发展历史 在上世纪60年代,由于程序猿们难以忍受超级难懂的机器语言和稍微好懂一点点的汇编语言,便发明了结构化的程序语言,使得程序猿们能愉快地编写复杂程度适中的程序.但是需求是在不断增长的 ...
- VS 2017显示“高级保存选项”命令操作方法
Visual Studio提供“高级保存选项”功能,它能指定特定代码文件的编码规范和行尾所使用的换行符.在Visual Studio 2017中,该命令默认是没有显示在“文件”菜单中的.用户需要手工设 ...
- 每天五分钟,玩转Docker。-Day2
Day2 镜像仓库(Docker registry) Docker registry是存储容器镜像的仓库,用户可以通过Docker client 与Docker register 进行通信,以此来完成 ...