6kyu Unary function chainer

题目:

Your task is to write a higher order function for chaining together a list of unary functions. In other words, it should return a function that does a left fold on the given functions.

chained([a,b,c,d])(input)
Should yield the same result as

d(c(b(a(input))))

Sample Tests:
function f1(x){ return x*2 }
function f2(x){ return x+2 }
function f3(x){ return Math.pow(x,2) }

function f4(x){ return x.split("").concat().reverse().join("").split(" ")}
function f5(xs){ return xs.concat().reverse() }
function f6(xs){ return xs.join("_") }

Test.assertEquals( chained([f1,f2,f3])(0), 4 )
Test.assertEquals( chained([f1,f2,f3])(2), 36 )
Test.assertEquals( chained([f3,f2,f1])(2), 12 )

Test.assertEquals(chained([f4,f5,f6])("lorem ipsum"), "merol_muspi")

时间: 2024-07-29 10:13:05

6kyu Unary function chainer的相关文章

【codewar】Unary function chainer

## 题目描述: Your task is to write a higher order function for chaining together a list of unary functions. In other words, it should return a function that does a left fold on the given functions. chained([a,b,c,d])(input) Should yield the same result a

【codewar】

##题目描述 Function composition is a mathematical operation that mainly presents itself in lambda calculus and computability. It is explained well here, but this is my explanation, in simple mathematical notation: f3 = compose( f1 f2 ) Is equivalent to..

谓词函数、函数对象

从概念上讲,函数对象用作函数的对象:但是从实现上来说,函数对象时实现了 operate()的类的对象. 虽然函数和函数指针也可以归为函数对象,但实现了operate()的类的对象才能保存状态,才能用于STL. 我们直接看定义: 一元函数:接受一个参数的函数,如f(x). 一元谓词函数:如果一元函数返回一个BOOL类型的值,则该函数称为谓词. 二元函数:接受2个参数的函数,如f(x,y). 二元谓词函数:如果二元函数返回一个BOOL值,则该函数称为二元谓词. 之所以给返回布尔类型的函数对象专门命名

01 - Execise About Array.prototype.reduce()

Description: Write a generic function chainer Write a generic function chainer that takes a starting value, and an array of functions to execute on it (array of symbols for ruby). The input for each function is the output of the previous function (ex

【转】编程词汇

很实用的编程英语词库,共收录一千五百余条词汇. 第一部分: application 应用程式 应用.应用程序 application framework 应用程式框架.应用框架 应用程序框架 architecture 架构.系统架构 体系结构 argument 引数(传给函式的值).叁见 parameter 叁数.实质叁数.实叁.自变量 array 阵列 数组 arrow operator arrow(箭头)运算子 箭头操作符 assembly 装配件 assembly language 组合语

Decidability and Partial Decidability

1. Decidability A predicate is decidable iff its characteristic function is computable, otherwise it is undecidable. An algorithm to compute the characteristic function of a decidable predicate is a decision procedure. Theorem. Problem 'x∈Wx' (i.e. Φ

angularJS (2) angular.min.js

angular.min.js /* AngularJS v1.2.29 (c) 2010-2014 Google, Inc. http://angularjs.org License: MIT*/(function(V,W,v){'use strict';function z(b){return function(){var a=arguments[0],c,a="["+(b?b+":":"")+a+"] http://errors.a

IT软件开发常用英语词汇

A abstract 抽象的 abstract base class 抽象基类 abstract class 抽象类 abstraction 抽象.抽象物.抽象性 access 存取.访问 access function 访问函数 access level 访问级别 account 账户 action 动作 activate 激活 actual parameter 实参 adapter 适配器 add-in 插件 address 地址 address space 地址空间 ADO(ActiveX

英语单词--程序员专属

named parameter 命名参数 named pipe 命名管道 namespace 名字空间.命名空间 native 原生的.本地的 native code 本地码.本机码 Native ImageGenerator (NGEN)本地映像生成器 nested class 嵌套类 nested query 嵌套查询(for database) nested table 嵌套表(for database) network 网络 network card 网卡 nondependent na