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

  1. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

  2. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

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

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

  5. Functional programming

    In computer science, functional programming is a programming paradigm, a style of building the struc ...

  6. Java 中的函数式编程(Functional Programming):Lambda 初识

    Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...

  7. Functional programming idiom

    A functional programming function is like a mathematical function, which produces an output that typ ...

  8. 关于函数式编程(Functional Programming)

    初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...

  9. Functional Programming 资料收集

    书籍: Functional Programming for Java Developers SICP(Structure and Interpretation of Computer Program ...

随机推荐

  1. LPC43xx SGPIO I2C Implementation

    I²C SGPIO Configuration SGPIO is a hardware feature of LPC4300 series. There are 16 SGPIO pins calle ...

  2. ThinkPHP 模型方法 getField() 和 select() 使用技巧

    getField() 使用技巧 getField() 方法是 ThinkPHP 中用来获取字段值的方法,区别于 select() 和 find() 方法,通常仅用于获取个别字段的值.但是事实上并没有那 ...

  3. SQLPrompt_7.2.2.273〖含注册机〗(支持低版本和最高版本SQL2016+VS2015)

    SQLPrompt_7.4.1.564[含注册机](支持低版本和最高版本SQL2016+VS2015) http://download.csdn.net/detail/wozengcong/97601 ...

  4. hibernate 注解 联合主键映射

    联合主键用Hibernate注解映射方式主要有三种: 第一.将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode,再将 该类注解 ...

  5. Java 遍历类中的属性

    public static void testReflect(Object model) throws NoSuchMethodException, IllegalAccessException, I ...

  6. c++中 extern

    用例子给你示范 // 1.cpp int x = 10; // 2.cpp 注意没有包含1.cpp #include <iostream> using namespace std; ext ...

  7. 看书小记6(《C专家编程》)

    typedef不常见但值得一提的用途: 1. 用typedef来定义与平台无关的类型. 比方定义一个叫 REAL 的浮点类型.在目标平台一上.让它表示最高精度的类型为: typedef long do ...

  8. spring4 quartz2 集群动态任务

    实现定时任务的执行,而且要求定时周期是不固定的.测试地址:http://sms.reyo.cn 生产环境:nginx+tomcat+quartz2.2.1+spring4.2.1 集群. 实现功能:可 ...

  9. linux下生成https的crt和key证书

    今天在配置kibana权限设置时,kibana要求使用https链接. 于是总结了一下linux下openssl生成 签名的步骤: x509证书一般会用到三类文,key,csr,crt   Key 是 ...

  10. spring-boot:run启动时,指定spring.profiles.active

    Maven启动指定Profile通过-P,如mvn spring-boot:run -Ptest,但这是Maven的Profile. 如果要指定spring-boot的spring.profiles. ...