Skip to content

Instantly share code, notes, and snippets.

@colin-han
Last active May 10, 2022 06:32
Show Gist options
  • Save colin-han/711f3820887821089521f2563e08458a to your computer and use it in GitHub Desktop.
Save colin-han/711f3820887821089521f2563e08458a to your computer and use it in GitHub Desktop.
Functional Programming练习题

一、理解函数式编程

1. 有一个Web服务,为你提供了如下的数据结构:

难度:★

var data = [
  {
    name: "Jamestown",
    population: 2047,
    temperatures: [-34, 67, 101, 87]
  },
  {
    name: "Awesome Town",
    population: 3568,
    temperatures: [-3, 4, 9, 12]
  }
  {
    name: "Funky Town",
    population: 1000000,
    temperatures: [75, 75, 75, 75, 75]
  }
];

请使用函数式编程的思想,实现一个数据抽取的算法。将人口和平均温度之间的关系抽取出来。返回 如下的结果:

[
    [2047, 55.25],
    [3568, 5.5],
    [1000000, 75]
]

2. 使用 Ramda.js 的函数,实现练习一。

不能定义自己的函数。(即:不得使用function或“=>”定义函数。)
难度:★★

3. 使用 Ramda.js 实现isAuthor函数。

不能定义自己的函数。
难度:★★★

var articles = [
  {
    title: 'Everything Sucks',
    url: 'http://do.wn/sucks.html',
    author: {
      name: 'Debbie Downer',
      email: 'debbie@do.wn'
    }
  },
  {
    title: 'If You Please',
    url: 'http://www.geocities.com/milq',
    author: {
      name: 'Caspar Milquetoast',
      email: 'hello@me.com'
    }
  }
];
var isAuthor = _.identity; // change this
assertEqual(
  false,
  isAuthor('New Guy', articles)
);
assertEqual(
  true,
  isAuthor('Debbie Downer', articles)
);

二、理解Immutable

4. 学习并利用 ImmutableJS 实现一个简单的TodoList的Model。

难度:★★

接口如下:

var TodoModel = function() {
    this._immutableList = ImmutableJS.fromJS([]);
}
TodoModel.prototype = {
    add: function(caption) {},
    remove: function(caption) {},
    rename: function(oldCaption, newCaption) {},
    complate: function(caption) {},
    clearAllCompleted: function() {},
    getAll: function() {},
    getNonCompleted: function() {}
}

三、自己动手

5. 实现一个通用Curry函数,支持如下的调用方法

难度:★★★★

// 用户函数
function foo(a, b, c) {
    return a + b + c;
}

function curry(fn) {
    // TODO: 实现你的Curry函数
    // return fn;
}

var foo2 = curry(foo);
foo2(10)(20, 30); // return 60.
foo2(10, 20)(30); // return 60 too.

6. 实现一个ImmutableList,支持如下的接口

难度:★★★

var l0 = ImmutableList.fromJs([1, 2, 3]);
var v0 = l0.get(1); // v0 = 2;
var l1 = l0.push(4); // l1 = (ImmutableList)[1, 2, 3, 4];
var l2 = l0.set(1, 4); // l2 = (ImmutableList)[1, 4, 3];
var l3 = l0.pop() // l3 = (ImmutableList)[1, 2];
var l4 = l0.delete(1); // l4 = (ImmutableList)[1, 3];
var l5 = l0.insert(1, 5); // l5 = (ImmutableList)[1, 5, 2, 3];

学习资源

  • 作业答案

q2. https://jsbin.com/poniho/2/edit?js,console
q3. https://jsbin.com/kiguwi/4/edit?js,console
q4.
一般实现: https://jsbin.com/tecoyi/31/edit?js,console
使用ImmutableJS的实现: https://jsbin.com/tecoyi/7/edit?js,console
添加了Undo支持的实现: https://jsbin.com/tecoyi/22/edit?js,console
优化后的实现: https://jsbin.com/tecoyi/66/edit?js,console
q5. https://jsbin.com/derewab/6/edit?js,console
q6. https://jsbin.com/jiyoxa/2/edit?js,console

  • 刘毅介绍中使用的练习

http://jsbin.com/corawe/edit?js,console
http://jsbin.com/romun/edit?js,console
http://jsbin.com/jevag/edit?js,console

  • 给 JavaScript 开发者讲讲函数式编程

http://www.oschina.net/news/72990/functional-programming-for-javascript?from=20160501

  • Issam推荐的一些函数式编程的学习视频

\xafile\share\Training\WebTrainningResource\Hardcore Functional Progrmming in JavaScript

  • Redux作者介绍Redux和TimeTravel的视频
    这个视频可以帮助我们理解为什么需要函数式编程和Immutable

https://www.youtube.com/watch?v=xsSnOQynTHs

  • LaySer团队的培训指南

http://xa-wiki:8080/pages/viewpage.action?pageId=92602399

  • RamdaJS

http://devdocs.io/ramda/
http://ramdajs.com/0.21.0/index.html

  • ImmutableJS

https://facebook.github.io/immutable-js/

  • RxJS

https://github.com/Reactive-Extensions/RxJS

@chidaihuashi
Copy link

练习1的data[1]后面少个逗号.....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment