functional-programming

JavaScript: How to pass extra parameters to a callback [duplicate]

余生颓废 提交于 2020-02-03 08:54:26
问题 This question already has answers here : Pass an extra argument to a callback function (4 answers) Closed 3 years ago . I have a question which has bugged me for a while now. Let's say I have the following array: var array = [1, 2, 3] Now I have a function similar to this: function print(num, str) { console.log(str + ": " + num); } Is it possible to call the forEach method and pass a string to it? // how do I pass "str"? array.forEach(print); Thanks! 回答1: You have two options here: Either you

How to use polymorphism in functional programming? [closed]

帅比萌擦擦* 提交于 2020-02-03 03:15:50
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . How to use polymorphism in functional programming (with dynamic type system)? Let's consider following example (first in OOP second in

Cartesian product over a list of lists in Haskell

自古美人都是妖i 提交于 2020-02-02 03:20:29
问题 Given a list of lists of length x where all the sublists have the same length y , output the y^x lists of length x that contain one item from each sublist. Example ( x = 3 , y = 2 ): [ [1, 2], [3, 4], [5, 6] ] Output ( 2^3 == 8 different outputs): [ [1, 3, 5], [1, 4, 5], [1, 3, 6], [1, 4, 6], [2, 3, 5], [2, 4, 5], [2, 3, 6], [2, 4, 6] ] My research / Work Ruby I wrote actual code to perform this task, but in Ruby, as it is the language I am most comfortable with. def all_combinations(lst) lst

scala - idiomatic way to change state of class

寵の児 提交于 2020-02-01 04:16:32
问题 I have several classes all extends the same trait and all share mutual functionality that should change their state. However I was wondering if is there a better way to implement the same functionality. e.g : trait Breed case object Pincher extends Breed case object Haski extends Breed trait Foox{ def age: Int def addToAge(i: Int): Foox } case class Dog(breed: Breed, age: Int) extends Foox case class Person(name: String, age: Int) extends Foox I want that addToAge will return the same object

scala - idiomatic way to change state of class

大城市里の小女人 提交于 2020-02-01 04:15:05
问题 I have several classes all extends the same trait and all share mutual functionality that should change their state. However I was wondering if is there a better way to implement the same functionality. e.g : trait Breed case object Pincher extends Breed case object Haski extends Breed trait Foox{ def age: Int def addToAge(i: Int): Foox } case class Dog(breed: Breed, age: Int) extends Foox case class Person(name: String, age: Int) extends Foox I want that addToAge will return the same object

Future[Either[AppError, Option[User]]] in Scala

无人久伴 提交于 2020-02-01 03:48:45
问题 As mentioned in the title, does it make sense to use such data structure? Let me explain one by one: Future - to represent async computation Either - to communicate known errors Option - to communicate that the value may not be present I am a little bit scared when looking at this. Is it a good practice to use such type combination? 回答1: Let's have a look at the solution space: Success(Right(Some(user))) => Everythig OK, got an user Success(Right(None)) => Everything OK, no user Success(Left

golang-style “defer” in C++ [duplicate]

落爺英雄遲暮 提交于 2020-02-01 01:38:46
问题 This question already has answers here : What is standard defer/finalizer implementation in C++? (8 answers) Closed 4 years ago . I was reading about the go language's defer statement. It allows you to specify an action to take when a function has ended. For example, if you have a file pointer or resource, instead of writing free/delete with every possible return path, you just need to specify the defer function once. It looks like an analogue might be coming to C++ eventually (What is

What is the best way to validate request in a Spring Webflux functional application

本小妞迷上赌 提交于 2020-01-30 04:51:52
问题 In a traditional web application it is easy to validate the request body in the controller method, eg. ResponseEntity create(@Valid @ResponseBody Post post) { } If it is a MVC application, we can gather the errors by injecting a BindingResult , and decide if there is some validation errors from the input form. In the pages, there are some helpers existed for Freemarker and Thymeleaf to display the messages. But when I come to Webflux and try to use RouterFunction to define the routing in the

Is this a pure function?

眉间皱痕 提交于 2020-01-28 17:36:53
问题 Most sources define a pure function as having the following two properties: Its return value is the same for the same arguments. Its evaluation has no side effects. It is the first condition that concerns me. In most cases, it's easy to judge. Consider the following JavaScript functions (as shown in this article) Pure: const add = (x, y) => x + y; add(2, 4); // 6 Impure: let x = 2; const add = (y) => { return x += y; }; add(4); // x === 6 (the first time) add(4); // x === 10 (the second time)

Is this a pure function?

会有一股神秘感。 提交于 2020-01-28 17:36:25
问题 Most sources define a pure function as having the following two properties: Its return value is the same for the same arguments. Its evaluation has no side effects. It is the first condition that concerns me. In most cases, it's easy to judge. Consider the following JavaScript functions (as shown in this article) Pure: const add = (x, y) => x + y; add(2, 4); // 6 Impure: let x = 2; const add = (y) => { return x += y; }; add(4); // x === 6 (the first time) add(4); // x === 10 (the second time)