functional-programming

Is it possible to define an infix function?

荒凉一梦 提交于 2020-12-01 03:56:46
问题 Is it possible to define my own infix function/operator in CoffeeScript (or in pure JavaScript)? e.g. I want to call a foo b or a `foo` b instead of a.foo b or, when foo is global function, foo a, b Is there any way to do this? 回答1: ES6 enables a very Haskell/Lambda calculus way of doing things. Given a multiplication function: const multiply = a => b => (a * b) You can define a doubling function using partial application (you leave out one parameter): const double = multiply (2) And you can

Is it possible to define an infix function?

允我心安 提交于 2020-12-01 03:56:08
问题 Is it possible to define my own infix function/operator in CoffeeScript (or in pure JavaScript)? e.g. I want to call a foo b or a `foo` b instead of a.foo b or, when foo is global function, foo a, b Is there any way to do this? 回答1: ES6 enables a very Haskell/Lambda calculus way of doing things. Given a multiplication function: const multiply = a => b => (a * b) You can define a doubling function using partial application (you leave out one parameter): const double = multiply (2) And you can

Is it possible to define an infix function?

…衆ロ難τιáo~ 提交于 2020-12-01 03:55:54
问题 Is it possible to define my own infix function/operator in CoffeeScript (or in pure JavaScript)? e.g. I want to call a foo b or a `foo` b instead of a.foo b or, when foo is global function, foo a, b Is there any way to do this? 回答1: ES6 enables a very Haskell/Lambda calculus way of doing things. Given a multiplication function: const multiply = a => b => (a * b) You can define a doubling function using partial application (you leave out one parameter): const double = multiply (2) And you can

Is it possible to define an infix function?

假装没事ソ 提交于 2020-12-01 03:55:13
问题 Is it possible to define my own infix function/operator in CoffeeScript (or in pure JavaScript)? e.g. I want to call a foo b or a `foo` b instead of a.foo b or, when foo is global function, foo a, b Is there any way to do this? 回答1: ES6 enables a very Haskell/Lambda calculus way of doing things. Given a multiplication function: const multiply = a => b => (a * b) You can define a doubling function using partial application (you leave out one parameter): const double = multiply (2) And you can

Java streams sum values of a List of Maps

本秂侑毒 提交于 2020-11-29 19:15:32
问题 i want to determine the of the "columns" in "rows" or or better: Build sum of a list of maps like List> rows Is it somehow possible to sum all values of each distinct column? The function shall return a Map with the column as key and the sum of all values as value. summMap.get("columname") Let's assume i have the following list of maps: List<Map<String, Long>> mapList = new ArrayList(); Map<String, Object> map1 = new HashMap<>(); Map<String, Object> map2 = new HashMap<>(); Map<String, Object>

Java streams sum values of a List of Maps

感情迁移 提交于 2020-11-29 19:13:40
问题 i want to determine the of the "columns" in "rows" or or better: Build sum of a list of maps like List> rows Is it somehow possible to sum all values of each distinct column? The function shall return a Map with the column as key and the sum of all values as value. summMap.get("columname") Let's assume i have the following list of maps: List<Map<String, Long>> mapList = new ArrayList(); Map<String, Object> map1 = new HashMap<>(); Map<String, Object> map2 = new HashMap<>(); Map<String, Object>

How to create an instances for typeclass with dependent type using shapeless

旧城冷巷雨未停 提交于 2020-11-29 09:32:58
问题 I'm trying to derive a tuple instance for a type class with dependent type. I'm using shapeless to create summon the type class for the tuple elements. I'm having trouble matching tuple instance types: import shapeless.the import simulacrum.typeclass @typeclass trait Identifiable[M] { type K def identify(id: M): K } object Identifiable{ implicit def identifiableTuple[K1: Identifiable, K2: Identifiable]: Identifiable[(K1,K2)] = new Identifiable[(K1,K2)]{ val b = the[Identifiable[K2]] val a =

How to encode corecursion/codata in a strictly evaluated setting?

限于喜欢 提交于 2020-11-29 03:07:06
问题 Corecursion means calling oneself on data at each iteration that is greater than or equal to what one had before. Corecursion works on codata, which are recursively defined values. Unfortunately, value recursion is not possible in strictly evaluated languages. We can work with explicit thunks though: const Defer = thunk => ({get runDefer() {return thunk()}}) const app = f => x => f(x); const fibs = app(x_ => y_ => { const go = x => y => Defer(() => [x, go(y) (x + y)]); return go(x_) (y_)

How to encode corecursion/codata in a strictly evaluated setting?

无人久伴 提交于 2020-11-29 03:07:05
问题 Corecursion means calling oneself on data at each iteration that is greater than or equal to what one had before. Corecursion works on codata, which are recursively defined values. Unfortunately, value recursion is not possible in strictly evaluated languages. We can work with explicit thunks though: const Defer = thunk => ({get runDefer() {return thunk()}}) const app = f => x => f(x); const fibs = app(x_ => y_ => { const go = x => y => Defer(() => [x, go(y) (x + y)]); return go(x_) (y_)