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. AFNetworking Delete请求,报参数为空的错误

    使用AFNetWorking进行网络请求的时候,AFNetWorking会默认把get head delete这三个方法的请求参数拼到了url的后面,然后造成body为空,一行代码解决: manage ...

  2. 解决overflow: hidden在移动端失效问题

    1.问题:移动端出现弹窗后,滑动页面,页面底部出现空白 二.原因 经过分析,发现overflow: hidden;在移动端失效,导致弹窗出现时,滑动页面,页面底部出现空白. 三.解决 参考网址:htt ...

  3. java注解小随笔

    注解分类 编码期间:SOURCE 编译期间:CLASS 运行期间:RUNTIME 自定义注解 //CLASS/RUNTIME/SOURCE 生效时机 @Retention(RetentionPolic ...

  4. 微信支付遇到的坑---缺少参数total_fee

    今天在做微信砍价成功后支付,出现了这个报错 看到报错后,去找total_fee这个参数,调试了半天,total_fee是确定有值的 微信支付的步骤 ① 预支付 商户号,商户秘钥,appid,appse ...

  5. 【转】RabbitMQ基础——和——持久化机制

    这里原来有一句话,触犯啦天条,被阉割!!!! 首先不去讨论我的日志组件怎么样.因为有些日志需要走网络,有的又不需要走网路,也是有性能与业务场景的多般变化在其中,就把他抛开,我们只谈消息RabbitMQ ...

  6. xx星空面试题

    一面 1.什么是多态 实现原理? 2.什么是重载?class加载原理? 3.常用设计模式,简单介绍 4.看过哪些java的书?android的书? 5.动态注册静态注册 优缺点?静态注册函数如何被调用 ...

  7. PHPEXCEL读出数据是PHPExcel_RichText类型

    今天在做导入EXCEL数据时,而且单元格里的数据类型改成文本类型后,在PHPEXCEL读出来的是PHPExcel_RichText类型的,这类型使getValue()是不管用了,因为这时候getVal ...

  8. android 自定义title

    package com.xiangyu.su; import android.app.Activity; import android.os.Bundle; import android.view.V ...

  9. ubuntu 重启nginx遇到错误

    错误如下:Job for nginx.service failed because the control process exited with error code. See "syst ...

  10. 一些Debug时没整理的内容

    一.UShapeComponent组件的默认CollisionProfile为:OverlapAllDynamic.这会影响到由此派生的许多组件. 二.TwinStickShooter中绑定键盘的方式 ...