[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 ...
随机推荐
- Modbus读写模拟量寄存器具体解释
读可读写模拟量寄存器: 发送命令(主机向从机)格式: [设备地址] [命令号03] [起始寄存器地址高8位] [低8位] [读取的寄存器数高8位] [低8位] [CRC校验的低8位] [CRC校验的高 ...
- 在ASP.NET MVC中使用Knockout实践06,自定义验证、异步验证
在上一篇中体验了Knockout.Validation的基本验证,本篇体验自定义验证和异步验证. 自定义验证规则 ko.validation有一个rules属性,专门用来存放验证规则,它是一个键值对集 ...
- Java知识回顾 (2) Java 修饰符
一.Java 修饰符 1.1 访问控制修饰符 Java中,可以使用访问控制符来保护对类.变量.方法和构造方法的访问.Java 支持 4 种不同的访问权限. default (即缺省,什么也不写): 在 ...
- 【Centos】centos查看磁盘使用情况
1.查看分区和磁盘 lsblk 查看分区和磁盘 2.查看空间使用情况 df -h 查看空间使用情况 3.分区工具查看分区信息 fdisk -l 分区工具查看分区信息 4.查看分区 cfdisk /de ...
- WordPress主题开发实例:显示最新发表文章
实现效果 制作网站的时候,想在页面左栏显示最新发表的文章,如: 一.先开启侧边栏工具,在functions.php加上 $args = array( 'name' => __( '分类侧边栏') ...
- HTTP协议--MyWebServer
HTTP协议 HTTP协议是一种Web通信协议,通过特定的规则来实现服务器跟客户端的通信.HTTP协议有这样几个特点: (1)面向无连接的,一次只能处理一个请求,HTTP1.0服务器解析完客户端请求并 ...
- Java 集合系列Stack详细介绍(源码解析)和使用示例
Stack简介 Stack是栈.它的特性是:先进后出(FILO, First In Last Out). java工具包中的Stack是继承于Vector(矢量队列)的,由于Vector是通过数组实现 ...
- 一步一步做出属于自己的Eclipse
本文将教大家一步一步打造属于自己的eclipse,涉及到地方,不完全之处请谅解. 一.下载 进入eclipse网站:http://www.eclipse.org/downloads/ 下载:Eclip ...
- Eclipse启动时提示fail to create the Java Virtual Machine问题的解决办法
是eclipse.ini文件的问题,打开eclipse安装目录下的eclipse.ini文件: 将其中的256m改为128m,512m改为256m,1024m改为512m即可 修改后如下: -star ...
- [Web 前端] React高级教程(es6)——(2)对于Refs最新变动的理解
cp : https://blog.csdn.net/liwusen/article/details/53384561 1.什么是ReactJS中的refs 在React中组件并不是真实的 DOM 节 ...