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的更多相关文章

  1. 如何解决Selenium中"Cannot find function addEventListener in object [object HTMLDocument]"的错误

    project: blog target: how-to-resolve-cannot-find-function-addEventListener-error-in-selenium.md stat ...

  2. 为什么Object.prototype在Function的原型链上与Function.prototype在Object的原型链上都为true

    关于javascript的原型链有一个问题我一直很疑惑:为什么 Function instanceof Object 与 Object instanceof Function都为true呢? Func ...

  3. Javascript中的函数(Function)与对象(Object)的关系

    今天我们来尝试理解Function和Object.因为这个里面有些人前期可能会搞糊涂.他们之间到底是什么关系.当然也不除外当初的我. 注意:官方定义: 在Javascript中,每一个函数实际上都是一 ...

  4. JavaScript中Function函数与Object对象的关系

    函数对象和其他内部对象的关系 除了函数对象,还有很多内部对象,比如:Object.Array.Date.RegExp.Math.Error.这些名称实际上表示一个 类型,可以通过new操作符返回一个对 ...

  5. Mybatis-Plus的Service方法使用 之 泛型方法default <V> List<V> listObjs(Function<? super Object, V> mapper)

    首先 我们先看到的这个方法入参是:Function<? super Object , V> mapper ,这是jdk1.8为了统一简化书写格式引进的函数式接口 . 简单 解释一下我对Fu ...

  6. 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 ...

  7. UML中关联(Association)、聚合(Aggregation)和合成(Composition)之间的区别

    本文为 Dennis Gao 原创技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载. 现在,我们需要设计一个项目管理系统,目前我们收集到了如下这些需求: REQ1:一个项目内有多名项目成 ...

  8. [Redux] Composition with Objects

    For example, current we have those todos: { todos: [ { completed: true, id: 0, text: "Learn Red ...

  9. Windows Composition API 指南 - 认识 Composition API

    微软在 Windows 10中 面向通用 Windows 应用 (Universal Windows Apps, UWA) 新引入了一套用于用户界面合成的 API:Composition API.Co ...

随机推荐

  1. CentOS 7 lnmp环境配置laravel项目的问题总结!

    一.最常见的几个问题 1.部署好站点后,访问站点的时候始终是“File Not Found”!(nginx中的路由配置问题) 2.除了根目录可以访问其它的访问全是403问题!(权限问题) 3.除了根目 ...

  2. mysql学习5:数据库设计

    mysql学习5:数据库设计 本文转载:https://blog.51cto.com/9291927/2087925:原创为天山老妖S 一.数据库设计简介 按照规范设计,将数据库的设计过程分为六个阶段 ...

  3. go安装依赖包

    实例 比如先切换到 $GOPATH 的 src 目录,cd $GOPATH/src,然后按需要下载: git clone --depth 1 https://github.com/golang/too ...

  4. bootstrap-table 踩坑手记

    官方文档: http://bootstrap-table.wenzhixin.net.cn/zh-cn/documentation/ 渲染表格有两种方法: 1)data属性渲染表格 2)js代码渲染表 ...

  5. GUI学习之五——QPushbutton类学习笔记

    QPushButton是QAbstractButton类下使用最多的子类,它的用法在上一章中基本讲完了,这里还总结了几个别的用法. 一.创建按钮 我们在前面所有的案例中创建按钮都是用这样的方式 win ...

  6. php实现下载模板与上传模板解析

    <? //下载模板的请求 if(isset($_GET['action']) && $_GET['action'] =='down_group_excel'){ $code = ...

  7. IIS配置支持跨域请求

    对于初次在IIS部署网站的同学,很容易忽略或不知道如何配置使其网站支持跨域请求,这里介绍一个最基础的方式,配置HTTP响应标头. 在IIS上选择HTTP响应标头,选择添加自定义响应标头,通常我们会添加 ...

  8. 桌面小部件Wight父类AppWidgetProvider的三个方法

    onUpdate()这个方法会在每次更新App Widget的时候调用,数据更新的逻辑都写在这个方法里边.而且要注意的是:在用户添加小部件的时候,会首先调用这个方法,应该在这个方法里进行初始化操作,比 ...

  9. d3.js画折线图

    下载d3.zip,并解压到网页文件所在的文件夹 windows下,在命令行进入网页文件夹,输入 python -m http.server 在浏览器中输入127.0.0.1:8000/xxx.html ...

  10. SCUCTF2018web部分wp

    [签到] Web部分的签到题,打开连接后F12审查元素 可以看到有被隐藏起来的JSFuck密码,解码运行后可得flag [计算器] 打开后界面如上图,要求简单来说就是回答20道数学题目,每道题最多3s ...