[Functional Programming] Daggy
const daggy = require('daggy');
const {tagged, taggedSum} = daggy; const Coord = daggy.tagged('Coord', ['x', 'y', 'z'])
const Line = daggy.tagged('Line', ['from', 'to']) Coord.prototype.translate = function(x, y, z) {
return Coord(
this.x + x,
this.y + y,
this.z + z
)
}
const origin = Coord(,,)
console.log(origin); // { x: 1, y: 2, z: 3 }
const myLine = Line(
origin,
origin.translate(,,)
);
console.log(myLine) // { from: { x: 1, y: 2, z: 3 }, to: { x: 3, y: 6, z: 9 } } const Shape = taggedSum('Shape', {
Square: ['topleft', 'bottomright'],
Circle: ['centre', 'radius']
}) Shape.prototype.translate = function(x, y, z) {
return this.cata({
Square: (topleft, bottomright) => Shape.Square(
topleft.translate(x, y, z),
bottomright.translate(x, y, z)
),
Circle: (centre, radius) => Shape.Circle(
centre.translate(x, y, z),
radius
)
})
} console.log(Shape.Square(Coord(, , ), Coord(, , ))
.translate(, , ))
// Square(Coord(5, 5, 3), Coord(6, 6, 3)) console.log(Shape.Circle(Coord(, , ), )
.translate(, , ))
// Circle(Coord(7, 7, 7), 8)
All the tagged
function really does is give us a function to fill the given named properties on an object.
taggedSum is a combination of multi Typed class.
[Functional Programming] Daggy的更多相关文章
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- 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 ...
- a primary example for Functional programming in javascript
background In pursuit of a real-world application, let’s say we need an e-commerce web applicationfo ...
- Functional programming
In computer science, functional programming is a programming paradigm, a style of building the struc ...
- Java 中的函数式编程(Functional Programming):Lambda 初识
Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...
- Functional programming idiom
A functional programming function is like a mathematical function, which produces an output that typ ...
- 关于函数式编程(Functional Programming)
初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...
- Functional Programming 资料收集
书籍: Functional Programming for Java Developers SICP(Structure and Interpretation of Computer Program ...
随机推荐
- Consul功能简介
Consul 是 HashiCorp 公司的一个用于实现分布式系统的服务发现与配置工具.Consul内置了服务注册与发现框 架.分布一致性协议实现.健康检查.Key/Value存储.多数据中心方案.由 ...
- stm32f103串口实现映射功能
在实际开发中,常常遇到串口的默认输出IO口被其它模块占用了,所以我们要用到串口IO口映射功能.是指将原来实现功能的IO口映射到其它指定IO口,其它不变.详细操作例如以下: 先贴出默认下的串口初始化设置 ...
- echarts 怎样去掉白色边框线 和怎样去除背景中的网格
echarts怎样去掉白色边框线: echarts怎样去除背景中的网格线
- PostgreSQL代码分析,查询优化部分,canonicalize_qual
这里把规范谓词表达式的部分就整理完了.阅读的顺序例如以下: 一.PostgreSQL代码分析,查询优化部分,canonicalize_qual 二.PostgreSQL代码分析,查询优化部分,pull ...
- 在Delphi中操作快捷方式
快捷方式减少了系统的重复文件,是快速启动程序或打开文件或文件夹的方法,快捷方式对经常使用的程序.文件和文件夹非常有用.在Windows系统中,充斥着大量的快捷方式,那么如何操作这些快捷方式就是一个很头 ...
- 关闭Delphi的RTTI
{$IF CompilerVersion >= 21.0}{$WEAKLINKRTTI ON}{$RTTI EXPLICIT METHODS([]) PROPERTIES([]) FIELDS( ...
- lufylegend:图形变形1
HTML5中的几种变形 HTML5中的变形,共有以下几种方法 scale() 缩放 rotate() 旋转 translate() 平移 transform() 矩阵变形 setTransform() ...
- java的异常和java web容器的异常
一.java的异常,只要catch住异常了,程序就不会挂,依然会执行catch之后的语句 Java程序发生异常就挂了吗? 为了验证程序不会挂,我写了个例子给大家看看. 测试代码: import jav ...
- Java从网络批量读取图片并保存至本网站服务器后再插入文章中
先来看效果: 查看一下系统操作日志 删除
- SQLUnit 环境搭建
1 技术须要积累 前几天去一位笛友小赖家玩,才知道他的笛子吹得好不是偶然.是由于他真的用功了,电脑里面下载有超多的视频.教程等!这真的非常让我震撼!一直是在公司做技术,但资料考不出来,而我整理了多少技 ...